Report MA0179 when the constant is on the left of the length comparison - #1512
Merged
Merged
Conversation
IsValidLengthComparisonPattern implements both orientations of the
comparison, but the two call sites hard-coded lengthIsOnLeft: true and
only ever passed (LeftOperand, RightOperand), so its else block was
unreachable and the reversed form was never reported:
member.GetCustomAttributes(typeof(ObsoleteAttribute), false).Length > 0 // reported
0 < member.GetCustomAttributes(typeof(ObsoleteAttribute), false).Length // not reported
The null comparison already handles both orientations, so the asymmetry
was an oversight. Try both operand orders and pass the flag through.
The code fixer needed no change: ShouldNegateLengthComparison already
implemented both orientations and searched both operands, and was dead
in the same way.
meziantou
deleted the
feature/ma0179-length-comparison-branch-79af79
branch
September 12, 2026 20:18
This was referenced Sep 12, 2026
Closed
This was referenced Oct 1, 2026
Closed
Open
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.291
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#586
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.291
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#565
Open
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.
What
UseAttributeIsDefinedAnalyzer(MA0179) only detectedGetCustomAttributes().Length/.Count()existence checks when the length was the left operand. The reversed form was silently missed:Why
IsValidLengthComparisonPatterntakes alengthIsOnLeftflag and carefully implements both orientations. But both call sites hard-codedlengthIsOnLeft: trueand only ever passed(operation.LeftOperand, operation.RightOperand)in that order, so the entireelseblock was unreachable dead code.IsGetCustomAttributeComparison(the== nullvariant) already handles both orientations, which makes the asymmetry an oversight rather than a deliberate scope decision — so this fixes it rather than deleting the branch.Changes
AnalyzeBinarynow tries both operand orders for theLengthandCount()comparisons, passing the orientation through toIsValidLengthComparisonPatterninstead of hard-codingtrue.left/rightparameters of the two helpers tolengthOperand/countOperandandotherOperand, since they no longer correspond to syntactic sides.docs/Rules/MA0179.md.Note for reviewers
The code fixer needed no change.
ShouldNegateLengthComparisonalready implemented both orientations and already searched both operands — it was dead in exactly the same way as the analyzer'selseblock, and simply becomes live now. That is why the diff touches only the analyzer.Tests
15 new cases in
UseAttributeIsDefinedAnalyzerTests, each asserting the diagnostic and the fixed code, so the negation direction is verified rather than just detection:Lengthforms added to the existing theory:0 == len→!IsDefined,0 != len→IsDefined,0 < len→IsDefined,1 <= len→IsDefined,1 > len→!IsDefined,0 >= len→!IsDefinedCount()forms0 > len,1 == len,2 <= len)Verification
dotnet test --max-parallel-test-modules 2across all five Roslyn versions: 22751 passed, 0 failed..trxoutput rather than relying on the pass count.dotnet run --project src/DocumentationGeneratorexits 0 with no further markdown changes.