Do not offer the MA0082 code fix when both operands are NaN - #1497
Merged
Merged
Conversation
The fixer rewrote `x == NaN` into `IsNaN(x)` by picking the other operand. When both operands are the NaN sentinel, the other operand is itself NaN, so `double.NaN == double.NaN` became `double.IsNaN(double.NaN)`, changing the result from false to true. The `!=` form was equally wrong: `!double.IsNaN(double.NaN)` is false while `NaN != NaN` is true. The analyzer still reports both operands, but no fix is registered when both of them are recognized NaN constants. The new test sets FixedCode, as Microsoft.CodeAnalysis.Testing skips the code fix verification when only TestCode is set.
This was referenced Sep 12, 2026
This was referenced Sep 25, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MA0082 reports both
NaNoperands. Applying the offered fix producesdouble.IsNaN(double.NaN), which changes the result of the expression fromFalsetoTrue.The fixer rewrote
x == NaNintoIsNaN(x)by picking the other operand of the comparison. When both operands are the NaN sentinel, "the other operand" is itselfNaN, so the rewrite loses the IEEE semantics of the comparison. The!=form was equally wrong:!double.IsNaN(double.NaN)isfalsewhileNaN != NaNistrue.Change
The fixer now returns before registering any action when both operands are recognized NaN constants. The analyzer still reports both operands — the comparison is still worth flagging — but no behavior-changing fix is offered.
I did not add a "replace the whole comparison with the constant result" alternative: MA0082 is a report-only design rule, and folding the expression to
false/trueis a different transformation than the one the fix advertises.Notes for the reviewer
The new test sets
FixedCodein addition toTestCode. This is needed:Microsoft.CodeAnalysis.Testingskips code fix verification entirely when onlyTestCodeis set, so a test written the usual way passes even while the fix is being applied. I verified the new test actually catches the bug by neutralizing the guard and re-running it — it fails with exactly the reported diff (_ = double.NaN == double.NaN;→_ = double.IsNaN(double.NaN);). Other analyzer tests in this repo likely have the same blind spot.Validation
DoNotNaNInComparisonsAnalyzerTestspass on every Roslyn version (4.8, 4.14, 5.0, 5.6, 5.9).dotnet run --project src/DocumentationGeneratorexits 0 with no markdown changes.