Fix MSTEST0049 false positives inside expression trees (#7585)#7592
Open
Evangelink wants to merge 1 commit intomainfrom
Open
Fix MSTEST0049 false positives inside expression trees (#7585)#7592Evangelink wants to merge 1 commit intomainfrom
Evangelink wants to merge 1 commit intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the MSTEST0049 analyzer to avoid reporting diagnostics for invocations that occur inside expression trees (e.g., Moq Setup lambdas), where applying the code fix would be inappropriate.
Changes:
- Add expression-tree detection (
IsInsideExpressionTree) to suppress MSTEST0049 diagnostics when the invocation is within anExpression<T>lambda. - Extend
WellKnownTypeNameswithSystem.Linq.Expressions.LambdaExpressionfor symbol lookup. - Add unit tests covering suppression inside expression trees and ensuring normal lambdas still produce diagnostics/fixes.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/UnitTests/MSTest.Analyzers.UnitTests/FlowTestContextCancellationTokenAnalyzerTests.cs | Adds new tests validating expression-tree suppression and non-expression lambda behavior. |
| src/Analyzers/MSTest.Analyzers/Helpers/WellKnownTypeNames.cs | Adds a well-known type name constant for LambdaExpression. |
| src/Analyzers/MSTest.Analyzers/FlowTestContextCancellationTokenAnalyzer.cs | Introduces expression-tree detection and skips MSTEST0049 reporting within expression trees. |
src/Analyzers/MSTest.Analyzers/FlowTestContextCancellationTokenAnalyzer.cs
Outdated
Show resolved
Hide resolved
Skip reporting MSTEST0049 diagnostics when the invocation is inside an expression tree (e.g. Moq Setup lambdas), where the code fix cannot be meaningfully applied. - Add IsInsideExpressionTree helper that walks up the operation tree to detect lambdas converted to Expression<T> - Add SystemLinqExpressionsLambdaExpression to WellKnownTypeNames - Add 3 tests covering expression tree suppression and regular lambda non-suppression
3663f68 to
31a2317
Compare
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.
Skip reporting MSTEST0049 diagnostics when the invocation is inside an expression tree (e.g. Moq Setup lambdas), where the code fix cannot be meaningfully applied.
Fixes #7585