Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/Rules/MA0202.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ private static string ComputeBranchSignature(SourceText sourceText, TextSpan spa
builder.Append(';');
}

if (builder.Length == 0)
return text.Trim();

return builder.ToString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,40 @@ public Task SameCodeWithDifferentComments() => CreateProjectBuilder()
""")
.ValidateAsync();

[Fact]
public Task DifferentXmlCommentsOnly() => CreateProjectBuilder()
.WithSourceCode("""
class C
{
#if A
/// <summary>net8</summary>
#else
/// <summary>net9</summary>
#endif
void M() { }
}

static class Program { static void Main() { } }
""")
.ValidateAsync();

[Fact]
public Task SameXmlCommentsOnly() => CreateProjectBuilder()
.WithSourceCode("""
class C
{
#if A
/// <summary>text</summary>
{|MA0202:#else|}
/// <summary>text</summary>
#endif
void M() { }
}

static class Program { static void Main() { } }
""")
.ValidateAsync();

[Fact]
public Task DifferentBranches() => CreateProjectBuilder()
.WithSourceCode("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("""
Expand Down