提交 e6a3a162 编写于 作者: M Manish Vasani

Use an alternate approach to detect bail out case: presence of deconstruction...

Use an alternate approach to detect bail out case: presence of deconstruction info with null deconstruct method. The prior approach of bailing out on null type or converted type broke existing behavior (tests) that handle null type value.
上级 f0475231
......@@ -237,7 +237,7 @@ void Method()
[WorkItem(29718, "https://github.com/dotnet/roslyn/issues/29718")]
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
public async Task NotOnErrorConvertedType()
public async Task NotOnErrorConvertedType_ForEachVariableStatement()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
......@@ -248,7 +248,7 @@ class C
void M()
{
// Error CS1061: 'KeyValuePair<int, int>' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'KeyValuePair<int, int>' could be found (are you missing a using directive or an assembly reference?)
// Error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'KeyValuePair<int, int>', with 2 out parameters and a void return type.
// Error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'KeyValuePair<int, int>', with 2 out parameters and a void return type.
foreach ([|var|] (key, value) in new Dictionary<int, int>())
{
}
......@@ -256,6 +256,25 @@ void M()
}", new TestParameters(options: ExplicitTypeEverywhere()));
}
[WorkItem(29718, "https://github.com/dotnet/roslyn/issues/29718")]
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
public async Task NotOnErrorConvertedType_AssignmentExpressionStatement()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
using System.Collections.Generic;
class C
{
void M(C c)
{
// Error CS1061: 'C' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'C' could be found (are you missing a using directive or an assembly reference?)
// Error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'C', with 2 out parameters and a void return type.
[|var|] (key, value) = c;
}
}", new TestParameters(options: ExplicitTypeEverywhere()));
}
#endregion
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
......
......@@ -57,15 +57,6 @@ private void HandleVariableDeclaration(SyntaxNodeAnalysisContext context)
return;
}
var typeInfo = semanticModel.GetTypeInfo(declaredType, cancellationToken);
if (typeInfo.Type == null ||
typeInfo.Type.IsErrorType() ||
typeInfo.ConvertedType == null ||
typeInfo.ConvertedType.IsErrorType())
{
return;
}
var typeStyle = Helper.AnalyzeTypeName(
declaredType, semanticModel, optionSet, cancellationToken);
if (!typeStyle.IsStylePreferred || !typeStyle.CanConvert())
......
......@@ -121,6 +121,25 @@ protected virtual bool ShouldAnalyzeForEachStatement(ForEachStatementSyntax forE
=> true;
protected virtual bool ShouldAnalyzeDeclarationExpression(DeclarationExpressionSyntax declaration, SemanticModel semanticModel, CancellationToken cancellationToken)
=> true;
{
// Ensure that deconstruction assignment or foreach variable statement have a non-null deconstruct method.
DeconstructionInfo? deconstructionInfoOpt = null;
switch (declaration.Parent)
{
case AssignmentExpressionSyntax assignmentExpression:
if (assignmentExpression.IsDeconstruction())
{
deconstructionInfoOpt = semanticModel.GetDeconstructionInfo(assignmentExpression);
}
break;
case ForEachVariableStatementSyntax forEachVariableStatement:
deconstructionInfoOpt = semanticModel.GetDeconstructionInfo(forEachVariableStatement);
break;
}
return !deconstructionInfoOpt.HasValue || deconstructionInfoOpt.Value.Method != null;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册