未验证 提交 68739328 编写于 作者: D David Wengier 提交者: GitHub

Move null check above where the variable is used (#46558)

* Move null check above where the variable is used

* Fix formatting
上级 3b8d19d7
......@@ -1814,6 +1814,35 @@ static void Main()
await VerifyCS.VerifyCodeFixAsync(source, source);
}
[WorkItem(46423, "https://github.com/dotnet/roslyn/issues/46423")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotCrashWhenTypeCantBeDetermined()
{
var source =
@"
class Other
{
public short GetScopeIdForTelemetry(FixAllScope scope)
=> (short)(scope switch
{
FixAllScope.Document => 1,
FixAllScope.Project => 2,
FixAllScope.Solution => 3,
_ => 4,
});
public enum FixAllScope
{
Document,
Project,
Solution,
Other
}
}";
await VerifyCS.VerifyCodeFixAsync(source, source);
}
[WorkItem(545777, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545777")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DoNotRemoveImportantTrailingTrivia()
......
......@@ -339,15 +339,15 @@ private static bool IsObjectCastInInterpolation(ExpressionSyntax castNode, IType
// The type in `(Type)...` or `... as Type`
var castType = semanticModel.GetTypeInfo(castNode, cancellationToken).Type;
// If we don't understand the type, we must keep it.
if (castType == null)
return true;
// The type in `(...)expr` or `expr as ...`
var castedExpressionType = semanticModel.GetTypeInfo(castedExpressionNode, cancellationToken).Type;
var conversion = semanticModel.ClassifyConversion(castNode.SpanStart, castedExpressionNode, castType, isExplicitInSource: true);
// If we don't understand the type, we must keep it.
if (castType == null)
return true;
// If we've got an error for some reason, then we don't want to touch this at all.
if (castType.IsErrorType())
return true;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册