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

Bail out on error type in CSharpTypeStyleDiagnosticAnalyzer

Fixes #29718
上级 fc965b58
......@@ -235,6 +235,27 @@ void Method()
}", new TestParameters(options: ExplicitTypeEverywhere()));
}
[WorkItem(29718, "https://github.com/dotnet/roslyn/issues/29718")]
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
public async Task NotOnErrorConvertedType()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
using System.Collections.Generic;
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.
foreach ([|var|] (key, value) in new Dictionary<int, int>())
{
}
}
}", new TestParameters(options: ExplicitTypeEverywhere()));
}
#endregion
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
......
......@@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle
......@@ -56,6 +57,15 @@ 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())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册