Skip to content

Comments

NUnit2055 should not fire on ClassicAssert#955

Merged
manfred-brands merged 5 commits intomasterfrom
Issue953
Nov 12, 2025
Merged

NUnit2055 should not fire on ClassicAssert#955
manfred-brands merged 5 commits intomasterfrom
Issue953

Conversation

@manfred-brands
Copy link
Member

Fixes #953

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 fixes issue #953 by preventing the NUnit2055 analyzer (InstanceOf) from firing on ClassicAssert calls. The key changes refactor constraint expression handling to properly distinguish between Assert.That calls (which should be analyzed) and ClassicAssert calls (which should not).

Key Changes:

  • Extracted constraint expression evaluation logic into reusable extension methods
  • Modified AssertHelper.TryGetActualAndConstraintOperations to handle assertions with omitted constraints (like Assert.That(condition))
  • Updated analyzers to use centralized constraint evaluation instead of duplicated logic

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
ConstraintExpressionPartExtensions.cs Adds IsTrueOrFalse() method to evaluate if a constraint part represents true/false, considering negation
ConstraintExpressionExtensions.cs Adds IsTrueOrFalse() method to evaluate constraint expressions, handling empty constraint lists
ConstraintExpression.cs Makes expression operation nullable and handles null cases for assertions without explicit constraints
InstanceOfAnalyzer.cs Refactors to use centralized constraint helper and removes duplicate constraint evaluation logic
AssertHelper.cs Updates to support assertions with omitted constraints and single-argument Assert.That calls
BaseConditionConstraintAnalyzer.cs Refactors constraint evaluation to use centralized helper, properly handling both modern and classic assertions
InstanceOfCodeFixTests.cs Adds test case for Is.Not.True constraint pattern
InstanceOfAnalyzerTests.cs Adds test for Is.Not.False and verifies ClassicAssert is not analyzed

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@mikkelbu mikkelbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I only had one comment which is a little unrelated to this PR, so I'm also happy to merge this as it is


public static bool? IsTrueOrFalse(this ConstraintExpressionPart constraintExpressionPart)
{
bool not = constraintExpressionPart.GetPrefix(NUnitFrameworkConstants.NameOfIsNot) is not null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rather count the number of Nots ? to handle people who have "odd" assert like

var value2 = 42;
Assert.That(value2 > 40, Is.Not.Not.True);

or is this too much work ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I thought it was far fetched to think about Is.Not.False, but Is.Not.Not.False is simply confusing.

I wish NUnit would not allow .Not.Not and as long as it does we might want to think about a rule to says that .Not.Not is the same as nothing.

@manfred-brands manfred-brands merged commit ad474f5 into master Nov 12, 2025
6 checks passed
@manfred-brands manfred-brands deleted the Issue953 branch November 12, 2025 05:15
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.

Code fix for NUnit2055 can generate invalid code for classic asserts

2 participants