@@ -293,11 +293,6 @@ protected override bool AssignmentSupportsStylePreference(
293293 return false ;
294294 }
295295
296- if ( IsSwitchExpressionAndCannotUseVar ( typeName , initializer , semanticModel ) )
297- {
298- return false ;
299- }
300-
301296 // variables declared using var cannot be used further in the same initialization expression.
302297 if ( initializer . DescendantNodesAndSelf ( )
303298 . Where ( n => n is IdentifierNameSyntax id && id . Identifier . ValueText . Equals ( identifier . ValueText ) )
@@ -356,47 +351,5 @@ protected override bool ShouldAnalyzeDeclarationExpression(DeclarationExpression
356351 // The base analyzer may impose further limitations
357352 return base . ShouldAnalyzeDeclarationExpression ( declaration , semanticModel , cancellationToken ) ;
358353 }
359-
360- private static bool IsSwitchExpressionAndCannotUseVar ( TypeSyntax typeName , ExpressionSyntax initializer , SemanticModel semanticModel )
361- {
362- if ( initializer . IsKind ( SyntaxKind . SwitchExpression ) )
363- {
364- // We compare the variable declaration type to each arm's type to see if there is an exact match, or if the
365- // arm type inherits from the variable declaration type. If not, we must use the explicit type instead of var.
366- // Even if 'true' is returned from this method, it is not guaranteed that we can use var. Further checks should occur
367- // after this method is called, such as checking if multiple implicit coversions exist.
368- var declarationType = semanticModel . GetTypeInfo ( typeName ) . Type ;
369- var noValidTypeExpressions = true ;
370- if ( declarationType != null )
371- {
372- foreach ( var arm in ( ( SwitchExpressionSyntax ) initializer ) . Arms )
373- {
374- var expression = arm . Expression ;
375- if ( expression . IsKind ( SyntaxKind . ParenthesizedExpression , out ParenthesizedExpressionSyntax ? parenExpression ) )
376- {
377- expression = parenExpression . WalkDownParentheses ( ) ;
378- }
379-
380- if ( ! expression . IsKind ( SyntaxKind . ThrowExpression ) && ! expression . IsKind ( SyntaxKind . NullLiteralExpression ) && ! expression . IsKind ( SyntaxKind . DefaultLiteralExpression ) )
381- {
382- noValidTypeExpressions = false ;
383- var expressionType = semanticModel . GetTypeInfo ( expression ) . Type ;
384- if ( expressionType != null && ! expressionType . InheritsFromOrEquals ( declarationType ) )
385- {
386- return true ;
387- }
388- }
389- }
390- }
391-
392- // If all arms are either throw statements, null literal expressions, or default literal expressions, return true.
393- if ( noValidTypeExpressions )
394- {
395- return true ;
396- }
397- }
398-
399- return false ;
400- }
401354 }
402355}
0 commit comments