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

Fix S1121: NullReferenceException when while loop with assignment exp… #729

Merged
merged 1 commit into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ private static bool IsInStatementCondition(ExpressionSyntax expression)
{
var expressionOrParenthesizedParent = expression.GetSelfOrTopParenthesizedExpression();

return IsInStatementCondition<IfStatementSyntax>(expressionOrParenthesizedParent, expression, s => s.Condition) ||
IsInStatementCondition<ForStatementSyntax>(expressionOrParenthesizedParent, expression, s => s.Condition) ||
IsInStatementCondition<WhileStatementSyntax>(expressionOrParenthesizedParent, expression, s => s.Condition) ||
IsInStatementCondition<DoStatementSyntax>(expressionOrParenthesizedParent, expression, s => s.Condition);
return IsInStatementCondition<IfStatementSyntax>(expressionOrParenthesizedParent, expression, s => s?.Condition) ||
IsInStatementCondition<ForStatementSyntax>(expressionOrParenthesizedParent, expression, s => s?.Condition) ||
IsInStatementCondition<WhileStatementSyntax>(expressionOrParenthesizedParent, expression, s => s?.Condition) ||
IsInStatementCondition<DoStatementSyntax>(expressionOrParenthesizedParent, expression, s => s?.Condition);
}

private static bool IsDirectlyInStatementCondition<T>(ExpressionSyntax expressionParent, ExpressionSyntax originalExpression,
Func<T, ExpressionSyntax> conditionSelector) where T : SyntaxNode
private static bool IsDirectlyInStatementCondition<T>(ExpressionSyntax expressionParent,
ExpressionSyntax originalExpression, Func<T, ExpressionSyntax> conditionSelector) where T : SyntaxNode
{
var statement = expressionParent.Parent.FirstAncestorOrSelf<T>();
return statement != null &&
Expand All @@ -165,8 +165,9 @@ private static bool IsInStatementCondition<T>(ExpressionSyntax expressionParent,
Func<T, ExpressionSyntax> conditionSelector) where T : SyntaxNode
{
var statement = expressionParent.Parent.FirstAncestorOrSelf<T>();
return statement != null &&
conditionSelector(statement).Contains(originalExpression);
var condition = conditionSelector(statement);
return condition != null
&& condition.Contains(originalExpression);
}

private static readonly ISet<SyntaxKind> AllowedParentExpressionKinds = ImmutableHashSet.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ void Foo()
for (int j = 0; b &= false; j++) { } // Noncompliant
for (int j = 0; b == false; j++) { }

// Fix S1121: NullReferenceException when while loop with assignment expression is within a for loop with no condition (#725)
for (;;)
{
while ((b = GetBool()) == true) { }
}

while (b &= false) { } // Noncompliant

while ((i = 1) == 1) { } // Compliant
Expand Down Expand Up @@ -110,5 +116,7 @@ public void TestMethod1()
i = j =
k = 10;
}

public bool GetBool() => return true;
}
}