diff --git a/docs/Rules/MA0202.md b/docs/Rules/MA0202.md index 8a3ddaf5..9ccbda2a 100644 --- a/docs/Rules/MA0202.md +++ b/docs/Rules/MA0202.md @@ -7,7 +7,9 @@ This rule reports duplicate code in a single `#if` / `#elif` / `#else` block. If a branch has the same code as any previous branch in the same conditional compilation block, the directive is redundant and should be simplified. -The comparison ignores trivia (such as comments and whitespace), so only the actual code structure is considered. +The comparison ignores trivia (such as comments and whitespace) when branches contain code, so only the actual code structure is considered. + +For comment-only branches, comments are compared textually to avoid false positives when comment content differs. ## Non-compliant code diff --git a/src/Meziantou.Analyzer/Rules/ConditionalCompilationBranchesAreIdenticalCommon.cs b/src/Meziantou.Analyzer/Rules/ConditionalCompilationBranchesAreIdenticalCommon.cs index f108603e..1e6b4b96 100644 --- a/src/Meziantou.Analyzer/Rules/ConditionalCompilationBranchesAreIdenticalCommon.cs +++ b/src/Meziantou.Analyzer/Rules/ConditionalCompilationBranchesAreIdenticalCommon.cs @@ -89,6 +89,9 @@ private static string ComputeBranchSignature(SourceText sourceText, TextSpan spa builder.Append(';'); } + if (builder.Length == 0) + return text.Trim(); + return builder.ToString(); } diff --git a/tests/Meziantou.Analyzer.Test/Rules/ConditionalCompilationBranchesAreIdenticalAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/ConditionalCompilationBranchesAreIdenticalAnalyzerTests.cs index e3b74b1a..d98058f8 100644 --- a/tests/Meziantou.Analyzer.Test/Rules/ConditionalCompilationBranchesAreIdenticalAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/ConditionalCompilationBranchesAreIdenticalAnalyzerTests.cs @@ -62,6 +62,40 @@ public Task SameCodeWithDifferentComments() => CreateProjectBuilder() """) .ValidateAsync(); + [Fact] + public Task DifferentXmlCommentsOnly() => CreateProjectBuilder() + .WithSourceCode(""" + class C + { + #if A + /// net8 + #else + /// net9 + #endif + void M() { } + } + + static class Program { static void Main() { } } + """) + .ValidateAsync(); + + [Fact] + public Task SameXmlCommentsOnly() => CreateProjectBuilder() + .WithSourceCode(""" + class C + { + #if A + /// text + {|MA0202:#else|} + /// text + #endif + void M() { } + } + + static class Program { static void Main() { } } + """) + .ValidateAsync(); + [Fact] public Task DifferentBranches() => CreateProjectBuilder() .WithSourceCode(""" diff --git a/tests/Meziantou.Analyzer.Test/Rules/UseStringComparerAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/UseStringComparerAnalyzerTests.cs index 0bedffb7..5b5edc64 100755 --- a/tests/Meziantou.Analyzer.Test/Rules/UseStringComparerAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/UseStringComparerAnalyzerTests.cs @@ -534,7 +534,7 @@ public void Test() } [Fact] - public async Task Dictionary_String_CollectionExpression_WithElements_ShouldReportDiagnostic() + public async Task HashSet_String_CollectionExpression_WithElements_Spread_ShouldReportDiagnostic() { await CreatePreviewProjectBuilder() .WithSourceCode("""