Do not report MA0192 when the HasFlag rewrite reorders an effectful operand - #1442
Merged
Merged
Conversation
…perand value.HasFlag(flag) evaluates the value operand before the flag operand and drops the duplicated read of the flag. AreEquivalentOperands only proves that both reads target the same storage, not that the value is stable across the evaluation of the other operand, so `(flag & Change()) == flag` was rewritten to `Change().HasFlag(flag)` even when Change() assigns a new value to flag, which changes the result of the check. The pattern is now gated on CanRewriteToHasFlag, which accepts the rewrite only when the flag is a compile-time constant, when the value operand is already evaluated before every read of the flag, or when the value operand is side-effect free. Both the comparison operand reversal and the bitwise operand reversal are covered.
This was referenced Sep 8, 2026
Closed
Closed
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.231
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#566
Closed
This was referenced Sep 24, 2026
Open
Open
Open
Open
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
value.HasFlag(flag)evaluates the value operand before the flag operand, and drops the duplicated read of the flag.AreEquivalentOperandsonly proves that both flag reads target the same storage (parameter, local, field) — not that the value is stable across the evaluation of the other operand.So MA0192 reported, and its fixer actually applied, an unsound rewrite:
becomes
Both versions compile, so the fix silently changes behaviour.
Fix
The pattern is now gated on
UseHasFlagMethodCommon.CanRewriteToHasFlag, used by both the analyzer and the fixer. The rewrite is accepted only when one of these holds:(value & flag) == flag/(value & flag) is …, where the&is the first-evaluated side of the comparison and the flag is the right&operand;this/ field references, and built-in conversions and unary/binary operators over those.To support that,
TryGetEnumFlagReferencegained anisConstantFlagoutput andGetFromBitwiseAndacomparedOperandEvaluatedFirstparameter, so comparison operand reversal (flag == (… & …)) is covered as well as bitwise operand reversal ((flag & value)). The three unsafe shapes —(flag & value) == flag,flag == (value & flag)andflag == (flag & value)— now require a pure value operand.Constant-flag checks (
(Change() & E.A) == E.A, the== 0/!= 0forms, theispatterns) are unaffected, since reading a constant cannot observe a mutation.Notes for the reviewer
comparand == (Value & comparand)is no longer reported. That is a false negative when the getter is pure, but proving getter purity is not feasible here and the shape is unusual. False negatives are the safe side for a rule whose fixer rewrites code.IParenthesizedOperationis not part of the purity walk — the C# compiler does not produce it.docs/Rules/MA0192.mddocuments the new evaluation-order requirement.Tests
7 tests added to
UseHasFlagMethodAnalyzerTests:value | E.Flag2) in a reversed shape.I verified the 4 negative tests genuinely cover the bug by temporarily stubbing
CanRewriteToHasFlagtotrue: all 4 failed, then passed once restored.Verification
dotnet build— clean.dotnet run --project src/DocumentationGenerator— exits 0, no further markdown changes.