Skip to content

Commit 25cc3f3

Browse files
authored
Merge pull request #44581 from allisonchou/SwitchExpressionError
Fix for incorrect IDE0007 implicit type diagnostic being reported in switch expressions
2 parents 82aac2c + 988813b commit 25cc3f3

File tree

2 files changed

+30
-47
lines changed

2 files changed

+30
-47
lines changed

src/Analyzers/CSharp/Tests/UseImplicitOrExplicitType/UseImplicitTypeTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,5 +2735,35 @@ static void Main(string[] args)
27352735
}",
27362736
options: ImplicitTypeEverywhere());
27372737
}
2738+
2739+
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
2740+
[WorkItem(44507, "https://github.com/dotnet/roslyn/issues/44507")]
2741+
public async Task DoNotSuggestVarInAmbiguousSwitchExpression()
2742+
{
2743+
await TestMissingAsync(
2744+
@"using System;
2745+
2746+
class C
2747+
{
2748+
void M()
2749+
{
2750+
var i = 1;
2751+
[||]C x = i switch
2752+
{
2753+
0 => new A(),
2754+
1 => new B(),
2755+
_ => throw new ArgumentException(),
2756+
};
2757+
}
2758+
}
2759+
2760+
class A : C
2761+
{
2762+
}
2763+
2764+
class B : C
2765+
{
2766+
}", parameters: new TestParameters(options: ImplicitTypeEverywhere()));
2767+
}
27382768
}
27392769
}

src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Utilities/TypeStyle/CSharpUseImplicitTypeHelper.cs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)