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

NUnit2021 ignores cast operation since NUnit 4.3.1 #811

Closed
manfred-brands opened this issue Dec 22, 2024 · 1 comment · Fixed by #812
Closed

NUnit2021 ignores cast operation since NUnit 4.3.1 #811

manfred-brands opened this issue Dec 22, 2024 · 1 comment · Fixed by #812
Assignees
Milestone

Comments

@manfred-brands
Copy link
Member

manfred-brands commented Dec 22, 2024

It seems that the changes in nunit/nunit#4899 caused my test code to have build error NUnit2021.
This occurs in some of the test codes like following. (Not all, it seems.)
My codes can build successfully at 36b8b1d37ce76795272b0df4b7b279f8c3cae93c.

[Test]
public void Test()
{
  MyLong l0 = new(0);

  Assert.That(l0, Is.EqualTo((MyLong)0));
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error NUnit2021: The EqualTo constraint always fails as the actual and the expected value cannot be equal (https://github.com/nunit/nunit.analyzers/tree/master/documentation/NUnit2021.md)
}

I will report again when the situation can be clearly reproduced.

Originally posted by @smdn in nunit/nunit#4899 (comment)

@manfred-brands manfred-brands changed the title @manfred-brands NUnit2021 ignores cast operation since NUnit 4.3.1 Dec 22, 2024
@manfred-brands manfred-brands self-assigned this Dec 22, 2024
@manfred-brands
Copy link
Member Author

In pre-4.3.1 Is.EqualTo(object?) for all but variables declared object causes an implicit cast.
The Analyzer is not interest in object but wants to check the original type. It therefore ignores the cast to get to the original type.

In NUnit 4.3.1 the addition of Is.EqualTo<T>(T) means that there is no implicit cast needed.
However if the operand had an explicit cast as in the code from the issue, the Analyzer doesn't use the resulting type but the original type defeating the intention of the writer.

The analyzer must only ignore an implicit cast, for pre-4.3.1 code, not an explicit one.

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 a pull request may close this issue.

2 participants