提交 c66c1fb8 编写于 作者: J JieCarolHu

only make change to the ForEach statement, do not change the code for the...

only make change to the ForEach statement, do not change the code for the VariableDeclaration statement
上级 8589e25f
......@@ -134,12 +134,10 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
return true;
}
}
else if (typeName.Parent is ForEachStatementSyntax foreachStatement &&
IsExpressionSyntaxSameAfterVarConversion(foreachStatement.Expression, semanticModel, cancellationToken))
else if (typeName.Parent is ForEachStatementSyntax foreachStatement)
{
// Semantic check to see if the conversion changes expression
var foreachStatementInfo = semanticModel.GetForEachStatementInfo(foreachStatement);
if (foreachStatementInfo.ElementConversion.IsIdentityOrImplicitReference())
if (foreachStatementInfo.ElementConversion.IsIdentity)
{
issueSpan = candidateIssueSpan;
return true;
......@@ -249,7 +247,12 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
return false;
}
if (!IsExpressionSyntaxSameAfterVarConversion(expression, semanticModel, cancellationToken))
// Get the conversion that occurred between the expression's type and type implied by the expression's context
// and filter out implicit conversions. If an implicit conversion (other than identity) exists
// and if we're replacing the declaration with 'var' we'd be changing the semantics by inferring type of
// initializer expression and thereby losing the conversion.
var conversion = semanticModel.GetConversion(expression, cancellationToken);
if (conversion.Exists && conversion.IsImplicit && !conversion.IsIdentity)
{
return false;
}
......@@ -259,19 +262,6 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
return declaredType.Equals(initializerType);
}
private static bool IsExpressionSyntaxSameAfterVarConversion(
ExpressionSyntax expression,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
// Get the conversion that occurred between the expression's type and type implied by the expression's context
// and filter out implicit conversions. If an implicit conversion (other than identity) exists
// and if we're replacing the declaration with 'var' we'd be changing the semantics by inferring type of
// initializer expression and thereby losing the conversion.
var conversion = semanticModel.GetConversion(expression, cancellationToken);
return conversion.IsIdentity;
}
protected override bool ShouldAnalyzeDeclarationExpression(DeclarationExpressionSyntax declaration, SemanticModel semanticModel, CancellationToken cancellationToken)
{
if (declaration.Type.IsVar)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册