Skip to content

Commit

Permalink
Fix analyzer RCS1211 (#1377)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Jan 24, 2024
1 parent 94fa430 commit 7ebf415
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix analyzer [RCS1055](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1055) ([PR](https://github.com/dotnet/roslynator/pull/1361))
- Fix analyzer [RCS1261](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1261) ([PR](https://github.com/dotnet/roslynator/pull/1374))
- Fix analyzer [RCS0056](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0056) ([PR](https://github.com/dotnet/roslynator/pull/1373))
- Fix analyzer [RCS1211](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1211) ([PR](https://github.com/dotnet/roslynator/pull/1377))

## [4.9.0] - 2024-01-10

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ private static bool IsFixable(ElseClauseSyntax elseClause, SemanticModel semanti
if (ifStatementStatement is not BlockSyntax ifBlock)
return CSharpFacts.IsJumpStatement(ifStatementStatement.Kind());

if (elseClause.SpanContainsDirectives())
return false;

if (elseClause.Statement is BlockSyntax elseBlock)
{
if (LocalDeclaredVariablesOverlap(elseBlock, ifBlock, semanticModel))
Expand Down
24 changes: 24 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1211RemoveUnnecessaryElseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ int M(bool flag)
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveUnnecessaryElse)]
public async Task TestNoDiagnostic_PreprocessorDirectives()
{
await VerifyNoDiagnosticAsync(@"
#define FOO
class C
{
int M(bool flag)
{
if(flag)
{
return 1;
}
else
{
#if FOO
return 0;
#endif
}
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveUnnecessaryElse)]
public async Task TestNoDiagnostic_OverlappingLocalVariables()
{
Expand Down

0 comments on commit 7ebf415

Please sign in to comment.