Report MA0192 when the compared flag is not a constant - #1315
Merged
Merged
Conversation
MA0192 only detected bitwise flag checks whose flag was a constant enum member, so `(value & comparand) == comparand` was not reported when `comparand` was a parameter, a local, or a field. Add a shared `UseHasFlagMethodCommon.AreEquivalentOperands` helper that structurally compares both operands and only accepts side-effect free references (parameters, locals, non-volatile fields, `this`/`base`), so replacing the two evaluations by a single `HasFlag` call cannot change the behavior. Properties and method calls are not reported. Constant-zero operands are excluded from the new path so MA0192 does not overlap with MA0201. Fixes #1313
This was referenced Aug 20, 2026
Closed
Bump Meziantou.Analyzer from 3.0.103 to 3.0.176
Analogy-LogViewer/Analogy.LogViewer.OpenTelemetry#95
Closed
Closed
Closed
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.177
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#553
Closed
Merged
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.177
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#540
Closed
This was referenced Sep 21, 2026
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.270
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#581
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.270
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#562
Closed
Closed
Closed
Closed
Closed
Closed
Closed
Closed
chore(deps): Bump the nuget-minor-and-patch group with 2 updates
donislawdev/BetterWindowsServices#3
Merged
Closed
Closed
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.
Fixes #1313
What changed
MA0192 (Use HasFlag instead of bitwise checks) only recognized bitwise flag checks whose flag was a constant enum member.
(value & comparand) == comparandwas not reported whencomparandwas a parameter, a local variable, or a field.src/Meziantou.Analyzer/Rules/UseHasFlagMethodCommon.cs:AreEquivalentOperandsstructurally compares two operations and accepts only side-effect free references — parameters, locals, non-volatile fields (with an equivalent instance), andthis/base. It is picked up by the code fixers project through the existingRules/*Common.csglob, so the analyzer and the fixer stay in sync.UseHasFlagMethodAnalyzer.TryGetEnumFlagReferenceandUseHasFlagMethodFixer.TryGetEnumFlagReferencenow returnIOperationinstead ofIFieldReferenceOperationand fall back to that equivalence check when the flag is not a constant enum member.The issue repro is now reported and fixed to
value.HasFlag(comparand), as are the reversed-operand ((comparand & value) == comparand) and!=variants.Why the restriction to side-effect free references
The code fix replaces two evaluations of the flag expression by a single one. Properties, method calls, and
volatilefields may yield a different value on each evaluation, so they are not reported. Tests cover each of those cases.Note for reviewers
The existing test
HasFlagsExtensionZeroFlag_NoDiagnosticcontainedpublic static bool HasFlags(this MyEnum value, MyEnum flags) => (value & flags) == flags;, which is exactly the pattern this change is meant to report, so that line now expects MA0192. The subject of that test (no MA0201 onvalue.HasFlags(MyEnum.None)) is unchanged.Verification
UseHasFlagMethodAnalyzerTestspass on roslyn4.8, 4.14, 5.0, 5.6 and 5.9.dotnet run --project src/DocumentationGeneratorexits 0 with no further markdown change after updatingdocs/Rules/MA0192.md.