Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 11 additions & 17 deletions src/Analyzers/CSharp/Analysis/RemoveUnnecessaryElseAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ public static void Analyze(SyntaxNodeAnalysisContext context)

private static bool IsFixable(ElseClauseSyntax elseClause, SemanticModel semanticModel)
{
if (elseClause.Statement?.IsKind(SyntaxKind.IfStatement) != false)
return false;

if (elseClause.Parent is not IfStatementSyntax ifStatement)
return false;

Expand All @@ -61,22 +58,19 @@ private static bool IsFixable(ElseClauseSyntax elseClause, SemanticModel semanti
if (ifStatementStatement is not BlockSyntax ifBlock)
return CSharpFacts.IsJumpStatement(ifStatementStatement.Kind());

if (elseClause.Statement is BlockSyntax elseBlock)
Copy link
Contributor Author

@jamesHargreaves12 jamesHargreaves12 Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diffs from L64 onwards do not change behaviour. They are just reducing nesting / Moving the checks that require a semantic model later.

{
if (LocalDeclaredVariablesOverlap(elseBlock, ifBlock, semanticModel))
return false;

if (ifStatement.Parent is SwitchSectionSyntax { Parent: SwitchStatementSyntax switchStatement }
&& SwitchLocallyDeclaredVariablesHelper.BlockDeclaredVariablesOverlapWithOtherSwitchSections(elseBlock, switchStatement, semanticModel))
{
return false;
}
}

StatementSyntax lastStatementInIf = ifBlock.Statements.LastOrDefault();

return lastStatementInIf is not null
&& CSharpFacts.IsJumpStatement(lastStatementInIf.Kind());
if (lastStatementInIf is null || !CSharpFacts.IsJumpStatement(lastStatementInIf.Kind()))
return false;

if (elseClause.Statement is not BlockSyntax elseBlock)
return true;

if (LocalDeclaredVariablesOverlap(elseBlock, ifBlock, semanticModel))
return false;

return ifStatement.Parent is not SwitchSectionSyntax { Parent: SwitchStatementSyntax switchStatement }
|| !SwitchLocallyDeclaredVariablesHelper.BlockDeclaredVariablesOverlapWithOtherSwitchSections(elseBlock, switchStatement, semanticModel);
}

private static bool LocalDeclaredVariablesOverlap(BlockSyntax elseBlock, BlockSyntax ifBlock, SemanticModel semanticModel)
Expand Down
41 changes: 41 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1211RemoveUnnecessaryElseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,47 @@ int M(bool flag)
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveUnnecessaryElse)]
public async Task Test_UnnecessaryElseIf_Removed()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
int M(bool flag, bool flag2)
{
if (flag)
{
return 1;
}
[|else|] if (flag2)
{
return 0;
}

return 2;
}
}
", @"
class C
{
int M(bool flag, bool flag2)
{
if (flag)
{
return 1;
}

if (flag2)
{
return 0;
}

return 2;
}
}
");
}

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