Fix MSTEST0037 to detect LINQ Enumerable.Contains on interface types#7487
Merged
Evangelink merged 2 commits intomainfrom Mar 6, 2026
Merged
Fix MSTEST0037 to detect LINQ Enumerable.Contains on interface types#7487Evangelink merged 2 commits intomainfrom
Evangelink merged 2 commits intomainfrom
Conversation
MSTEST0037 (UseProperAssertMethodsAnalyzer) was not detecting Assert.IsTrue(collection.Contains(item)) when .Contains() resolved to the LINQ extension method System.Linq.Enumerable.Contains<T>() instead of an instance method on the collection type. This affected interface types like IReadOnlyList<T>, IReadOnlyCollection<T>, and IEnumerable<T>. Root cause: RecognizeCollectionMethodCheck only handled instance methods (Parameters.Length == 1, ContainingType is the collection). For LINQ calls, Roslyn represents them with ContainingType == Enumerable and Arguments includes the 'this' parameter (Length == 2). Fix: Add a second detection path that matches Enumerable.Contains calls by checking ContainingType against the Enumerable type symbol, matching the pattern already used by TryMatchLinqMethod for Any()/Count(). Fixes #7486
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes MSTEST0037 (UseProperAssertMethodsAnalyzer) to detect Assert.IsTrue(collection.Contains(item)) when .Contains() resolves to the LINQ extension method System.Linq.Enumerable.Contains<T>() instead of an instance method. This was previously missed for interface types like IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, and custom non-BCL types that don't define their own Contains.
Changes:
- Adds a new LINQ detection path in
RecognizeCollectionMethodCheckthat matchesEnumerable.Containscalls by checkingContainingType == enumerableTypeSymbolandArguments.Length == 2 - Propagates
enumerableTypeSymbolinto theRecognizeCollectionMethodCheckmethod signature - Adds 6 new unit tests covering
IReadOnlyList<T>,IEnumerable<T>,IReadOnlyCollection<T>, custom non-BCL collections,IsFalsevariant, and a case with a message parameter
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Analyzers/MSTest.Analyzers/UseProperAssertMethodsAnalyzer.cs |
Adds LINQ Enumerable.Contains detection path in RecognizeCollectionMethodCheck by checking for ContainingType == enumerableTypeSymbol with 2-argument form |
test/UnitTests/MSTest.Analyzers.UnitTests/UseProperAssertMethodsAnalyzerTests.cs |
Adds 6 unit tests verifying the new LINQ Contains detection and code fix for various interface types and scenarios |
test/UnitTests/MSTest.Analyzers.UnitTests/UseProperAssertMethodsAnalyzerTests.cs
Outdated
Show resolved
Hide resolved
Youssef1313
approved these changes
Mar 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MSTEST0037 (UseProperAssertMethodsAnalyzer) was not detecting Assert.IsTrue(collection.Contains(item)) when .Contains() resolved to the LINQ extension method System.Linq.Enumerable.Contains() instead of an instance method on the collection type. This affected interface types like IReadOnlyList, IReadOnlyCollection, and IEnumerable.
Root cause: RecognizeCollectionMethodCheck only handled instance methods (Parameters.Length == 1, ContainingType is the collection). For LINQ calls, Roslyn represents them with ContainingType == Enumerable and Arguments includes the 'this' parameter (Length == 2).
Fix: Add a second detection path that matches Enumerable.Contains calls by checking ContainingType against the Enumerable type symbol, matching the pattern already used by TryMatchLinqMethod for Any()/Count().
Fixes #7486