public static bool IsExplicitThatCanBeImplicit( VariableDeclarationSyntax variableDeclaration, SemanticModel semanticModel, TypeAppearance typeAppearance, CancellationToken cancellationToken = default(CancellationToken)) { TypeSyntax type = variableDeclaration.Type; Debug.Assert(type != null); if (type == null) { return(false); } if (type.IsVar) { return(false); } switch (variableDeclaration.Parent.Kind()) { case SyntaxKind.FieldDeclaration: case SyntaxKind.EventFieldDeclaration: { return(false); } case SyntaxKind.LocalDeclarationStatement: { if (((LocalDeclarationStatementSyntax)variableDeclaration.Parent).IsConst) { return(false); } break; } } Debug.Assert(variableDeclaration.Variables.Any()); ExpressionSyntax expression = variableDeclaration .Variables .SingleOrDefault(shouldThrow: false)? .Initializer? .Value? .WalkDownParentheses(); if (expression == null) { return(false); } if (expression.IsKind(SyntaxKind.NullLiteralExpression, SyntaxKind.DefaultLiteralExpression)) { return(false); } ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, cancellationToken); if (typeSymbol == null) { return(false); } if (typeSymbol.IsKind(SymbolKind.ErrorType, SymbolKind.DynamicType)) { return(false); } if (!typeSymbol.Equals(semanticModel.GetTypeSymbol(expression, cancellationToken))) { return(false); } switch (typeAppearance) { case TypeAppearance.Obvious: return(IsObvious(expression, semanticModel, cancellationToken)); case TypeAppearance.NotObvious: return(!IsObvious(expression, semanticModel, cancellationToken)); } Debug.Assert(typeAppearance == TypeAppearance.None, typeAppearance.ToString()); return(true); }
public static bool IsImplicitThatCanBeExplicit( VariableDeclarationSyntax variableDeclaration, SemanticModel semanticModel, TypeAppearance typeAppearance, CancellationToken cancellationToken = default(CancellationToken)) { TypeSyntax type = variableDeclaration.Type; Debug.Assert(type != null); if (type == null) { return(false); } if (!type.IsVar) { return(false); } if (variableDeclaration.IsParentKind(SyntaxKind.FieldDeclaration, SyntaxKind.EventFieldDeclaration)) { return(false); } Debug.Assert(variableDeclaration.Variables.Any()); ExpressionSyntax expression = variableDeclaration .Variables .FirstOrDefault()? .Initializer? .Value? .WalkDownParentheses(); if (expression == null) { return(false); } ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, cancellationToken); if (typeSymbol == null) { return(false); } if (typeSymbol.IsKind(SymbolKind.ErrorType, SymbolKind.DynamicType)) { return(false); } if (!typeSymbol.SupportsExplicitDeclaration()) { return(false); } switch (typeAppearance) { case TypeAppearance.Obvious: return(IsObvious(expression, semanticModel, cancellationToken)); case TypeAppearance.NotObvious: return(!IsObvious(expression, semanticModel, cancellationToken)); } Debug.Assert(typeAppearance == TypeAppearance.None, typeAppearance.ToString()); return(true); }