diff --git a/src/EditorFeatures/CSharpTest/UsePatternMatching/CSharpIsAndCastCheckTests.cs b/src/EditorFeatures/CSharpTest/UsePatternMatching/CSharpIsAndCastCheckTests.cs index a4049524f0ecfffb62ea9349cc9ea9e792fce9a4..ce6507dcc43b7200817e09831f06d4968eac9399 100644 --- a/src/EditorFeatures/CSharpTest/UsePatternMatching/CSharpIsAndCastCheckTests.cs +++ b/src/EditorFeatures/CSharpTest/UsePatternMatching/CSharpIsAndCastCheckTests.cs @@ -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; + } }"); } } diff --git a/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs b/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs index 21b7f0d0285bb38b1bc5d3c0eb03dd1abd0ca049..bcf30a30739849ade96af75323c96e6c5f635fc3 100644 --- a/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs +++ b/src/Features/CSharp/Portable/UsePatternMatching/CSharpIsAndCastCheckDiagnosticAnalyzer.cs @@ -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: