diff --git a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypeDiagnosticAnalyzer.cs b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypeDiagnosticAnalyzer.cs index 499f54429a2528ece98be26002757087e85720d6..712086d227e0e535d44d8fd12d3abd631ee468e0 100644 --- a/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypeDiagnosticAnalyzer.cs +++ b/src/Features/CSharp/Portable/Diagnostics/Analyzers/CSharpUseImplicitTypeDiagnosticAnalyzer.cs @@ -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)