diff --git a/src/EditorFeatures/CSharpTest/UsePatternMatching/CSharpAsAndNullCheckTests.cs b/src/EditorFeatures/CSharpTest/UsePatternMatching/CSharpAsAndNullCheckTests.cs index 960a9b8640e58..9bca5bbba0260 100644 --- a/src/EditorFeatures/CSharpTest/UsePatternMatching/CSharpAsAndNullCheckTests.cs +++ b/src/EditorFeatures/CSharpTest/UsePatternMatching/CSharpAsAndNullCheckTests.cs @@ -114,6 +114,20 @@ void M() }"); } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)] + [WorkItem(25237, "https://github.com/dotnet/roslyn/issues/25237")] + public async Task TestMissingOnReturnStatement() + { + await TestMissingInRegularAndScriptAsync( +@"class C +{ + void M() + { + [|return;|] + } +}"); + } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)] public async Task TestMissingOnIsExpression() { diff --git a/src/Features/CSharp/Portable/UsePatternMatching/CSharpAsAndNullCheckDiagnosticAnalyzer.cs b/src/Features/CSharp/Portable/UsePatternMatching/CSharpAsAndNullCheckDiagnosticAnalyzer.cs index 7568d3a60a5fe..c07324f70ccd6 100644 --- a/src/Features/CSharp/Portable/UsePatternMatching/CSharpAsAndNullCheckDiagnosticAnalyzer.cs +++ b/src/Features/CSharp/Portable/UsePatternMatching/CSharpAsAndNullCheckDiagnosticAnalyzer.cs @@ -413,6 +413,11 @@ private static SyntaxNode GetLeftmostCondition(SyntaxNode node) { while (true) { + if (node == null) + { + return null; + } + switch (node.Kind()) { case SyntaxKind.WhileStatement: @@ -428,13 +433,7 @@ private static SyntaxNode GetLeftmostCondition(SyntaxNode node) var declarators = ((LocalDeclarationStatementSyntax)node).Declaration.Variables; // We require this to be the only declarator in the declaration statement // to simplify definitive assignment check and the code fix for now - var value = declarators.Count == 1 ? declarators[0].Initializer?.Value : null; - if (value == null) - { - return null; - } - - node = value; + node = declarators.Count == 1 ? declarators[0].Initializer?.Value : null; continue; case SyntaxKind.ParenthesizedExpression: node = ((ParenthesizedExpressionSyntax)node).Expression;