提交 92922112 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #18431 from CyrusNajmabadi/useThrowExplicitConversion

Don't offer 'use throw expression' when there is an explicit cast in source.  It may change semantics.
...@@ -324,6 +324,40 @@ void Foo() ...@@ -324,6 +324,40 @@ void Foo()
_s = s; _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
{
}"); }");
} }
} }
......
...@@ -233,8 +233,7 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol ...@@ -233,8 +233,7 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
localOrParameter = null; localOrParameter = null;
var condition = ifStatement.Condition; var condition = ifStatement.Condition;
var binaryOperator = condition as IBinaryOperatorExpression; if (!(condition is IBinaryOperatorExpression binaryOperator))
if (binaryOperator == null)
{ {
return false; return false;
} }
...@@ -262,21 +261,18 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol ...@@ -262,21 +261,18 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
private bool TryGetLocalOrParameterSymbol( private bool TryGetLocalOrParameterSymbol(
IOperation operation, out ISymbol localOrParameter) IOperation operation, out ISymbol localOrParameter)
{ {
if (operation.Kind == OperationKind.ConversionExpression) if (operation is IConversionExpression conversion && !conversion.IsExplicit)
{ {
var conversionExpression = (IConversionExpression)operation; return TryGetLocalOrParameterSymbol(conversion.Operand, out localOrParameter);
return TryGetLocalOrParameterSymbol(
conversionExpression.Operand, out localOrParameter);
} }
else if (operation is ILocalReferenceExpression localReference)
if (operation.Kind == OperationKind.LocalReferenceExpression)
{ {
localOrParameter = ((ILocalReferenceExpression)operation).Local; localOrParameter = localReference.Local;
return true; return true;
} }
else if (operation.Kind == OperationKind.ParameterReferenceExpression) else if (operation is IParameterReferenceExpression parameterReference)
{ {
localOrParameter = ((IParameterReferenceExpression)operation).Parameter; localOrParameter = parameterReference.Parameter;
return true; return true;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册