Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify AvoidUsingRedundantElseAnalyzer #763

Merged
Merged
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
26 changes: 1 addition & 25 deletions src/Meziantou.Analyzer/Rules/AvoidUsingRedundantElseAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static void AnalyzeElseClause(SyntaxNodeAnalysisContext context)
if (controlFlowAnalysis is null || !controlFlowAnalysis.Succeeded)
return;

if (!controlFlowAnalysis.EndPointIsReachable || controlFlowAnalysis.ExitPoints.Any(ep => IsDirectAccess(ifStatement, ep)))
if (!controlFlowAnalysis.EndPointIsReachable)
{
context.ReportDiagnostic(Rule, elseClause.ElseKeyword);
}
Expand All @@ -91,28 +91,4 @@ private static IEnumerable<string> FindLocalIdentifiersIn(SyntaxNode node)
}
}
}

/// <summary>
/// Determines if a given 'if' statement's access to an exit point is straightforward.
/// For instance, access to an exit point in a nested 'if' would not be considered straightforward.
/// </summary>
/// <param name="ifStatementSyntax">The 'if' statement whose 'else' is currently under scrutiny</param>
/// <param name="exitPoint">A reachable exit point detected by the semantic model</param>
/// <returns>true if the exit point is directly accessible, false otherwise</returns>
private static bool IsDirectAccess(IfStatementSyntax ifStatementSyntax, SyntaxNode exitPoint)
{
var node = exitPoint.Parent;
while (node is not null)
{
if (node == ifStatementSyntax)
return true;

if (!node.IsKind(SyntaxKind.Block))
break;

node = node.Parent;
}

return false;
}
}
Loading