提交 3860f1fe 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #19730 from CyrusNajmabadi/patternMatchingNullable

Don't offer to convert to pattern matching when type is nullable.
......@@ -516,6 +516,26 @@ static void N(ParameterSyntax parameter)
parent = parent.Parent;
}
}
}");
}
[WorkItem(429612, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/429612")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestMissingWithNullableType()
{
await TestMissingInRegularAndScriptAsync(
@"
class C
{
public object Convert(object value)
{
if (value is bool?)
{
[|bool?|] tmp = (bool?)value;
}
return null;
}
}");
}
}
......
......@@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.CSharp.UsePatternMatching
{
......@@ -144,6 +145,13 @@ private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
var semanticModel = syntaxContext.SemanticModel;
var localSymbol = (ILocalSymbol)semanticModel.GetDeclaredSymbol(declarator);
var isType = semanticModel.GetTypeInfo(castExpression.Type).Type;
if (isType.IsNullable())
{
// not legal to write "if (x is int? y)"
return;
}
if (!localSymbol.Type.Equals(isType))
{
// we have something like:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册