Skip to content

Commit 7ebf415

Browse files
authored
Fix analyzer RCS1211 (#1377)
1 parent 94fa430 commit 7ebf415

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
- Fix analyzer [RCS1055](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1055) ([PR](https://github.com/dotnet/roslynator/pull/1361))
2626
- Fix analyzer [RCS1261](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1261) ([PR](https://github.com/dotnet/roslynator/pull/1374))
2727
- Fix analyzer [RCS0056](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0056) ([PR](https://github.com/dotnet/roslynator/pull/1373))
28+
- Fix analyzer [RCS1211](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1211) ([PR](https://github.com/dotnet/roslynator/pull/1377))
2829

2930
## [4.9.0] - 2024-01-10
3031

src/Analyzers/CSharp/Analysis/RemoveUnnecessaryElseAnalyzer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ private static bool IsFixable(ElseClauseSyntax elseClause, SemanticModel semanti
6161
if (ifStatementStatement is not BlockSyntax ifBlock)
6262
return CSharpFacts.IsJumpStatement(ifStatementStatement.Kind());
6363

64+
if (elseClause.SpanContainsDirectives())
65+
return false;
66+
6467
if (elseClause.Statement is BlockSyntax elseBlock)
6568
{
6669
if (LocalDeclaredVariablesOverlap(elseBlock, ifBlock, semanticModel))

src/Tests/Analyzers.Tests/RCS1211RemoveUnnecessaryElseTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ int M(bool flag)
4646
");
4747
}
4848

49+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveUnnecessaryElse)]
50+
public async Task TestNoDiagnostic_PreprocessorDirectives()
51+
{
52+
await VerifyNoDiagnosticAsync(@"
53+
#define FOO
54+
class C
55+
{
56+
int M(bool flag)
57+
{
58+
if(flag)
59+
{
60+
return 1;
61+
}
62+
else
63+
{
64+
#if FOO
65+
return 0;
66+
#endif
67+
}
68+
}
69+
}
70+
");
71+
}
72+
4973
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveUnnecessaryElse)]
5074
public async Task TestNoDiagnostic_OverlappingLocalVariables()
5175
{

0 commit comments

Comments
 (0)