Do not offer the MA0202 code fix to merge non-adjacent branches - #1580
Merged
Merged
Conversation
meziantou
marked this pull request as ready for review
September 23, 2026 04:45
meziantou
commented
Sep 23, 2026
meziantou
enabled auto-merge (squash)
September 23, 2026 04:45
This was referenced Sep 23, 2026
Bump Meziantou.Analyzer from 3.0.139 to 3.0.283
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#582
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.283
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#563
Closed
Closed
Closed
Closed
Closed
Closed
Closed
This was referenced Sep 28, 2026
Merged
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
The MA0202 code fix merges an
#elifbranch with the first earlier branch that has the same code, even when the two branches aren't next to each other. When a branch in between can also be active, the merge changes which code is compiled:The fix produced
#if (A) || (C)/_ = 0;/#elif B/_ = 1;. WithBandCdefined, the original code compiles_ = 1;and the fixed code compiles_ = 0;.Fix
The code fix now merges a branch only with the branch right before it, when that branch has the same code. In the example above, no code fix is offered. For
#if A/#elif B/#elif CwhereBandChave the same code, the fix gives#elif (B) || (C). Before, a third identical branch was merged into the first one. It is now merged into the adjacent one, which gives the same result.The remove fix (
#if/#elsewith 2 identical branches) only applies to adjacent branches, so it doesn't change.BranchGroup.FindPreviousDuplicateBranchIndexwas only used by the fixer, so it is removed.The analyzer still reports non-adjacent duplicates. The docs say that a branch with the same code as any previous branch is reported, and the existing
NonAdjacentDuplicateBranchtest covers this. The duplicated code can still be simplified by hand. The MA0202 docs now have a "Code fix" section that explains when the fix is offered.I checked for other MA0202 work first: #1511 (nested conditionals) is merged and no MA0202 PR is open.
Tests
Fix_ElifElif_SameCode_MergesConditionsandFix_NonAdjacentElif_SameCode_NoCodeFix. The second one failed before the fix.ConditionalCompilationBranchesAreIdenticalAnalyzerTestson roslyn5.9 and roslyn4.8: 19/19 passed on each.