diff --git a/src/EditorFeatures/CSharpTest/UseThrowExpression/UseThrowExpressionTests.cs b/src/EditorFeatures/CSharpTest/UseThrowExpression/UseThrowExpressionTests.cs index 2c7b810d0b0f2..21a8ef5be42dd 100644 --- a/src/EditorFeatures/CSharpTest/UseThrowExpression/UseThrowExpressionTests.cs +++ b/src/EditorFeatures/CSharpTest/UseThrowExpression/UseThrowExpressionTests.cs @@ -324,6 +324,40 @@ void Foo() _s = s; }; } +}"); + } + + [WorkItem(404142, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=404142")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseThrowExpression)] + public async Task TestNotWithAsCheck() + { + await TestMissingInRegularAndScriptAsync( +@"using System; + +class BswParser3 +{ + private ParserSyntax m_syntax; + + public BswParser3(ISyntax syntax) + { + if (syntax == null) + { + [|throw|] new ArgumentNullException(nameof(syntax)); + } + + m_syntax = syntax as ParserSyntax; + + if (m_syntax == null) + throw new ArgumentException(); + } +} + +internal class ParserSyntax +{ +} + +public interface ISyntax +{ }"); } } diff --git a/src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs b/src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs index 825a8dd2a2b5b..02ee807915167 100644 --- a/src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs +++ b/src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs @@ -233,8 +233,7 @@ private bool TryDecomposeIfCondition( localOrParameter = null; var condition = ifStatement.Condition; - var binaryOperator = condition as IBinaryOperatorExpression; - if (binaryOperator == null) + if (!(condition is IBinaryOperatorExpression binaryOperator)) { return false; } @@ -262,21 +261,18 @@ private bool TryDecomposeIfCondition( private bool TryGetLocalOrParameterSymbol( IOperation operation, out ISymbol localOrParameter) { - if (operation.Kind == OperationKind.ConversionExpression) + if (operation is IConversionExpression conversion && !conversion.IsExplicit) { - var conversionExpression = (IConversionExpression)operation; - return TryGetLocalOrParameterSymbol( - conversionExpression.Operand, out localOrParameter); + return TryGetLocalOrParameterSymbol(conversion.Operand, out localOrParameter); } - - if (operation.Kind == OperationKind.LocalReferenceExpression) + else if (operation is ILocalReferenceExpression localReference) { - localOrParameter = ((ILocalReferenceExpression)operation).Local; + localOrParameter = localReference.Local; return true; } - else if (operation.Kind == OperationKind.ParameterReferenceExpression) + else if (operation is IParameterReferenceExpression parameterReference) { - localOrParameter = ((IParameterReferenceExpression)operation).Parameter; + localOrParameter = parameterReference.Parameter; return true; } diff --git a/src/Workspaces/CoreTest/LinkedFileDiffMerging/LinkedFileDiffMergingTests.cs b/src/Workspaces/CoreTest/LinkedFileDiffMerging/LinkedFileDiffMergingTests.cs index 011fc22bfab6e..de073b9a052f6 100644 --- a/src/Workspaces/CoreTest/LinkedFileDiffMerging/LinkedFileDiffMergingTests.cs +++ b/src/Workspaces/CoreTest/LinkedFileDiffMerging/LinkedFileDiffMergingTests.cs @@ -50,4 +50,4 @@ public void TestLinkedFileSet(string startText, List updatedTexts, strin } } } -} +} \ No newline at end of file