-
Couldn't load subscription status.
- Fork 287
Codefix for MSTEST0023 #6796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Codefix for MSTEST0023 #6796
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements a code fix for analyzer MSTEST0023, which suggests using the appropriate Assert method instead of negating boolean expressions in test assertions. For example, Assert.IsTrue(!condition) is replaced with Assert.IsFalse(condition).
Key Changes:
- Adds a new code fix provider
DoNotNegateBooleanAssertionFixerthat automatically corrects negated boolean assertions - Updates the analyzer to include diagnostic properties that guide the code fix
- Adds comprehensive test coverage for various negation scenarios including simple negations, double negations, complex expressions, and method calls
Reviewed Changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Analyzers/MSTest.Analyzers.CodeFixes/DoNotNegateBooleanAssertionFixer.cs | New code fix provider that removes negations and swaps Assert.IsTrue/IsFalse methods |
| src/Analyzers/MSTest.Analyzers/DoNotNegateBooleanAssertionAnalyzer.cs | Updated analyzer to include proper assert method name in diagnostic properties |
| src/Analyzers/MSTest.Analyzers/Resources.resx | Added localization strings for code fix titles |
| src/Analyzers/MSTest.Analyzers/xlf/Resources.*.xlf | Added localization entries for all supported languages |
| test/UnitTests/MSTest.Analyzers.UnitTests/DoNotNegateBooleanAssertionAnalyzerTests.cs | Added comprehensive test cases for code fix functionality |
src/Analyzers/MSTest.Analyzers.CodeFixes/DoNotNegateBooleanAssertionFixer.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's replace all calls to VerifyAnalyzerAsync from existing tests please
|
|
||
| /// <inheritdoc /> | ||
| public override FixAllProvider GetFixAllProvider() | ||
| => FixAll.Instance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment as to why you are not using the BatchFixer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think batch fixer should actually be fine here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the pull request to use BatchFixer. @Evangelink @Youssef1313
| // Walk down parentheses and negations until we find the core expression | ||
| ExpressionSyntax current = expression; | ||
|
|
||
| while (true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I personally prefer to avoid while (true) anf favor having explicit stop condition.
I'll let @Youssef1313 decides what he prefers here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not completely sure if this logic is correct on its current form, and I think the bug here is already demonstrated by the test WhenAssertIsTrueWithDoubleNegation_Diagnostic where we are changing semantics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not completely sure if this logic is correct on its current form, and I think the bug here is already demonstrated by the test
WhenAssertIsTrueWithDoubleNegation_Diagnosticwhere we are changing semantics.
my approach to resolving this is double negation is treeated just like a single negation.. so should be treated as one and get all the negations removed.. and fixes the code to Assert.IsFalse.. i.e. Assert.IsTrue(!!condition) ==> Assert.IsFalse(condition)...
similar to the same way Assert.IsTrue((!condition)) ==> Assert.IsFalse(condition)...
or is this wrong?? If so, what how should the double negation be treated? @Youssef1313
| return editor.GetChangedDocument(); | ||
| } | ||
|
|
||
| private static ExpressionSyntax RemoveAllNegations(ExpressionSyntax expression) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of figuring out the syntax node that we need here, it's a better idea IMO to modify the analyzer itself to add that node as an additionalLocation. The codefix can then retrieve the node via the diagnostic additional locations.
…ertionFixer.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
you want me to replace all |
|
Ideally everything should always use the code fix verifier API. This helps with ensuring we test as many cases as possible. |
This PR fixes #6585
This Implementation is to modify the
DoNotNegateBooleanAnalyzer code for to suggest better and cleaner code when developers negate boolean arguments.For example;
When a code is wrriten as
Assert.IsTrue(!condition), then the Analyzer would suggestAssert.IsFalse(condition)Assert.IsFalse(!condition)Analyzer would suggestAssert.IsTrue(condition).other edge cases were also considered, for example;
Assert.IsTrue(!(a && b))Analyzer would suggestAssert.IsFalse(a && b)Assert.IsFalse(!(x == y)), Analyzer would suggestAssert.IsTrue(x == y)Assert.IsTrue(!!condition)Analyzer would suggestAssert.IsFalse(condition)