Skip to content
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

[improvement] NUnit2010 should provide a different suggestion for null comparison #746

Open
Bartleby2718 opened this issue Jun 2, 2024 · 1 comment

Comments

@Bartleby2718
Copy link
Contributor

Suppose the following:

string? nullString = null;
string? nonNullString = "nonNullString";
int? nullInt = null;
int? nonNullInt = 42;

Then, NUnit2010 flags the following assertions and proposes some fixes (as of NUnit.Analyzers 4.2.0):

// Assert.That(nullString, Is.EqualTo(null)); is proposed for the following two:
Assert.That(nullString is null);
Assert.That(nullString == null);

// Assert.That(nonNullString, Is.Not.EqualTo(null)); is proposed for the following two:
Assert.That(nonNullString is not null);
Assert.That(nonNullString != null);

// Assert.That(nullInt, Is.EqualTo(null)); is proposed for the following two:
Assert.That(nullInt is null);
Assert.That(nullInt == null);

// Assert.That(nonNullInt, Is.Not.EqualTo(null)); is proposed for the following two:
Assert.That(nonNullInt is not null);
Assert.That(nonNullInt != null);

I don't know how Is.EqualTo is implemented under the hood, but I'm pretty sure Is.Null or Is.Not.Null should be used for either half or all of the fixes, depending on how Is.EqualTo works.

@manfred-brands manfred-brands changed the title [bug] NUnit2010 should provide a different suggestion for null comparison [improvement] NUnit2010 should provide a different suggestion for null comparison Jun 2, 2024
@manfred-brands
Copy link
Member

The actual generated code is correct, but indeed it could improve the code more by checking the actual argument tested and generated Is.Null for null and also Is.Zero for 0.

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

No branches or pull requests

2 participants