Skip to content

Conversation

@AtolagbeMuiz
Copy link
Contributor

This PR fixes #6585

This Implementation is to modify the DoNotNegateBoolean Analyzer 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 suggest Assert.IsFalse(condition)

Assert.IsFalse(!condition) Analyzer would suggest Assert.IsTrue(condition).

other edge cases were also considered, for example;
Assert.IsTrue(!(a && b)) Analyzer would suggest Assert.IsFalse(a && b)

Assert.IsFalse(!(x == y)), Analyzer would suggest Assert.IsTrue(x == y)

Assert.IsTrue(!!condition) Analyzer would suggest Assert.IsFalse(condition)

@Evangelink Evangelink requested a review from Copilot October 27, 2025 05:48
Copy link
Contributor

Copilot AI left a 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 DoNotNegateBooleanAssertionFixer that 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

Copy link
Member

@Evangelink Evangelink left a 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;
Copy link
Member

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

Copy link
Member

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?

Copy link
Contributor Author

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)
Copy link
Member

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.

Copy link
Member

@Youssef1313 Youssef1313 Oct 27, 2025

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.

Copy link
Contributor Author

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.

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)
Copy link
Member

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>
@AtolagbeMuiz
Copy link
Contributor Author

AtolagbeMuiz commented Oct 27, 2025

Let's replace all calls to VerifyAnalyzerAsync from existing tests please

you want me to replace all VerifyAnalyzerAsync from the test with VerifyCodeFixAsync ?... is this just for pre-exisitng test cases which are just two.. or including the test cases I wrote? @Evangelink

@Evangelink
Copy link
Member

Ideally everything should always use the code fix verifier API. This helps with ensuring we test as many cases as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement a codefix for MSTEST0023

3 participants