2
// UnreachableCodeIssue.cs
5
// Mansheng Yang <lightyang0@gmail.com>
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
29
using ICSharpCode.NRefactory.CSharp.Analysis;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
34
[IssueDescription ("Code is unreachable",
35
Description = "Code is unreachable.",
36
Category = IssueCategories.Redundancies,
37
Severity = Severity.Warning,
38
IssueMarker = IssueMarker.GrayOut)]
39
public class UnreachableCodeIssue : ICodeIssueProvider
41
public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
43
return new GatherVisitor (context).GetIssues ();
46
class GatherVisitor : GatherVisitorBase<UnreachableCodeIssue>
48
HashSet<AstNode> unreachableNodes;
50
public GatherVisitor (BaseRefactoringContext ctx)
53
unreachableNodes = new HashSet<AstNode> ();
56
public override void VisitMethodDeclaration (MethodDeclaration methodDeclaration)
58
StatementIssueCollector.Collect (this, methodDeclaration.Body);
60
base.VisitMethodDeclaration (methodDeclaration);
63
public override void VisitLambdaExpression (LambdaExpression lambdaExpression)
65
var blockStatement = lambdaExpression.Body as BlockStatement;
66
if (blockStatement != null)
67
StatementIssueCollector.Collect (this, blockStatement);
69
base.VisitLambdaExpression (lambdaExpression);
72
public override void VisitAnonymousMethodExpression (AnonymousMethodExpression anonymousMethodExpression)
74
StatementIssueCollector.Collect (this, anonymousMethodExpression.Body);
76
base.VisitAnonymousMethodExpression (anonymousMethodExpression);
79
public override void VisitConditionalExpression (ConditionalExpression conditionalExpression)
81
var resolveResult = ctx.Resolve (conditionalExpression.Condition);
82
if (resolveResult.ConstantValue is bool) {
83
var condition = (bool)resolveResult.ConstantValue;
84
Expression resultExpr, unreachableExpr;
86
resultExpr = conditionalExpression.TrueExpression;
87
unreachableExpr = conditionalExpression.FalseExpression;
89
resultExpr = conditionalExpression.FalseExpression;
90
unreachableExpr = conditionalExpression.TrueExpression;
92
unreachableNodes.Add (unreachableExpr);
94
AddIssue (unreachableExpr, ctx.TranslateString ("Remove unreachable code"),
95
script => script.Replace (conditionalExpression, resultExpr.Clone ()));
97
base.VisitConditionalExpression (conditionalExpression);
100
protected override void VisitChildren (AstNode node)
102
// skip unreachable nodes
103
if (!unreachableNodes.Contains (node))
104
base.VisitChildren (node);
107
class StatementIssueCollector : DepthFirstAstVisitor
109
GatherVisitor visitor;
110
ReachabilityAnalysis reachability;
111
HashSet<Statement> collectedStatements;
113
private StatementIssueCollector (GatherVisitor visitor, ReachabilityAnalysis reachability)
115
collectedStatements = new HashSet<Statement> ();
116
this.visitor = visitor;
117
this.reachability = reachability;
120
public static void Collect (GatherVisitor visitor, BlockStatement body)
124
var reachability = visitor.ctx.CreateReachabilityAnalysis (body);
126
var collector = new StatementIssueCollector (visitor, reachability);
127
collector.VisitBlockStatement (body);
130
protected override void VisitChildren (AstNode node)
132
var statement = node as Statement;
133
if (statement == null || !AddStatement (statement))
134
base.VisitChildren (node);
137
bool AddStatement(Statement statement)
139
if (reachability.IsReachable (statement))
141
if (collectedStatements.Contains (statement))
143
var prevEnd = statement.GetPrevNode (n => !(n is NewLineNode)).EndLocation;
145
// group multiple continuous statements into one issue
146
var start = statement.StartLocation;
147
collectedStatements.Add (statement);
148
visitor.unreachableNodes.Add (statement);
149
while (statement.GetNextSibling (s => s is Statement) != null) {
150
statement = (Statement)statement.GetNextSibling (s => s is Statement);
151
collectedStatements.Add (statement);
152
visitor.unreachableNodes.Add (statement);
154
var end = statement.EndLocation;
156
var removeAction = new CodeAction (visitor.ctx.TranslateString ("Remove unreachable code"),
159
var startOffset = script.GetCurrentOffset (prevEnd);
160
var endOffset = script.GetCurrentOffset (end);
161
script.RemoveText (startOffset, endOffset - startOffset);
162
}, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation);
163
var commentAction = new CodeAction(visitor.ctx.TranslateString("Comment unreachable code"),
166
var startOffset = script.GetCurrentOffset(prevEnd);
167
script.InsertText(startOffset, Environment.NewLine + "/*");
168
var endOffset = script.GetCurrentOffset(end);
169
script.InsertText(endOffset, Environment.NewLine + "*/");
170
}, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation);
171
var actions = new [] { removeAction, commentAction };
172
visitor.AddIssue (start, end, visitor.ctx.TranslateString ("Code is unreachable"), actions);
176
// skip lambda and anonymous method
177
public override void VisitLambdaExpression (LambdaExpression lambdaExpression)
181
public override void VisitAnonymousMethodExpression (
182
AnonymousMethodExpression anonymousMethodExpression)