提交 287c94ab 编写于 作者: B Balaji Krishnan

Introduce Constants should not be offered for Nulls

Don't offer Introduce Constants on NullLiterals. They could cause
semantic errors when used in a replace all occurrences context and are
offer no value anyway.
上级 7e345e79
...@@ -613,13 +613,12 @@ public void TestMissingOnVariableWrite() ...@@ -613,13 +613,12 @@ public void TestMissingOnVariableWrite()
} }
[WorkItem(544577)] [WorkItem(544577)]
[WorkItem(909152)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)] [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)]
public void TestExpressionTLambda() public void TestExpressionTLambda()
{ {
Test( TestMissing(
@"using System ; using System . Linq . Expressions ; class Program { static Expression < Func < int ? , char ? > > e1 = c => [|null|] ; } ", @"using System ; using System . Linq . Expressions ; class Program { static Expression < Func < int ? , char ? > > e1 = c => [|null|] ; } ");
@"using System ; using System . Linq . Expressions ; class Program { private const char ? {|Rename:P|} = null ; static Expression < Func < int ? , char ? > > e1 = c => P ; } ",
index: 1);
} }
[WorkItem(544915)] [WorkItem(544915)]
...@@ -2323,5 +2322,23 @@ class TestClass ...@@ -2323,5 +2322,23 @@ class TestClass
Test(code, expected, index: 2, compareTokens: false); Test(code, expected, index: 2, compareTokens: false);
} }
[WorkItem(909152)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)]
public void TestMissingOnNullLiteral()
{
TestMissing(
@"class C1 { }
class C2 { }
class Test
{
void M()
{
C1 c1 = [|null|];
C2 c2 = null;
}
}
");
}
} }
} }
...@@ -96,13 +96,24 @@ protected override bool IsInAttributeArgumentInitializer(ExpressionSyntax expres ...@@ -96,13 +96,24 @@ protected override bool IsInAttributeArgumentInitializer(ExpressionSyntax expres
return false; return false;
} }
/// <summary>
/// Checks for conditions where we should not generate a variable for an expression
/// </summary>
protected override bool CanIntroduceVariableFor(ExpressionSyntax expression) protected override bool CanIntroduceVariableFor(ExpressionSyntax expression)
{ {
// (a) If that's the only expression in a statement.
// Otherwise we'll end up with something like "v;" which is not legal in C#.
if (expression.WalkUpParentheses().IsParentKind(SyntaxKind.ExpressionStatement)) if (expression.WalkUpParentheses().IsParentKind(SyntaxKind.ExpressionStatement))
{ {
return false; return false;
} }
// (b) For Null Literals, as AllOccurences could introduce semantic errors.
if (expression is LiteralExpressionSyntax && ((LiteralExpressionSyntax)expression).IsKind(SyntaxKind.NullLiteralExpression))
{
return false;
}
return true; return true;
} }
......
...@@ -216,9 +216,6 @@ private TExpressionSyntax GetExpressionUnderSpan(SyntaxTree tree, TextSpan textS ...@@ -216,9 +216,6 @@ private TExpressionSyntax GetExpressionUnderSpan(SyntaxTree tree, TextSpan textS
private bool CanIntroduceVariable( private bool CanIntroduceVariable(
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
// Don't generate a variable for an expression that's the only expression in a
// statement. Otherwise we'll end up with something like "v;" which is not
// legal in C#.
if (!_service.CanIntroduceVariableFor(this.Expression)) if (!_service.CanIntroduceVariableFor(this.Expression))
{ {
return false; return false;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册