Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 04a0769

Browse files
committed
Try to fix NullReferenceException in UnreachableCodeIssue.
1 parent b171c52 commit 04a0769

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp.Refactoring/CodeIssues/Custom/UnreachableCodeIssue.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ protected override void VisitChildren(AstNode node)
133133
if (statement == null || !AddStatement(statement))
134134
base.VisitChildren(node);
135135
}
136+
137+
static TextLocation GetPrevEnd(AstNode node)
138+
{
139+
var prev = node.GetPrevNode(n => !(n is NewLineNode));
140+
return prev != null ? prev.EndLocation : node.StartLocation;
141+
}
142+
143+
static TextLocation GetNextStart(AstNode node)
144+
{
145+
var next = node.GetNextNode(n => !(n is NewLineNode));
146+
return next != null ? next.StartLocation : node.EndLocation;
147+
}
136148

137149
bool AddStatement(Statement statement)
138150
{
@@ -147,7 +159,7 @@ bool AddStatement(Statement statement)
147159
}
148160

149161

150-
var prevEnd = statement.GetPrevNode(n => !(n is NewLineNode)).EndLocation;
162+
var prevEnd = GetPrevEnd(statement);
151163
TextLocation start;
152164
TextLocation end;
153165

@@ -159,12 +171,12 @@ bool AddStatement(Statement statement)
159171
collectedStatements.Add(statement);
160172
visitor.unreachableNodes.Add(statement);
161173
collectedStatements.Add(ife.ElseToken);
162-
end = ife.ElseToken.GetNextNode(n => !(n is NewLineNode)).StartLocation;
174+
end = GetNextStart(ife.ElseToken);
163175
} else if (statement.Role == IfElseStatement.FalseRole) {
164176
var ife = (IfElseStatement)statement.Parent;
165177
start = ife.ElseToken.StartLocation;
166178
collectedStatements.Add(ife.ElseToken);
167-
prevEnd = ife.ElseToken.GetPrevNode(n => !(n is NewLineNode)).EndLocation;
179+
prevEnd = GetPrevEnd(ife.ElseToken);
168180
collectedStatements.Add(statement);
169181
visitor.unreachableNodes.Add(statement);
170182
end = statement.EndLocation;

0 commit comments

Comments
 (0)