提交 ad5fb400 编写于 作者: B Balaji Krishnan

Merge pull request #2316 from balajikris/IntroduceLocal-909152

Introduce Constants should not be offered for Null Literals
......@@ -613,13 +613,12 @@ public void TestMissingOnVariableWrite()
}
[WorkItem(544577)]
[WorkItem(909152)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)]
public void TestExpressionTLambda()
{
Test(
@"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);
TestMissing(
@"using System ; using System . Linq . Expressions ; class Program { static Expression < Func < int ? , char ? > > e1 = c => [|null|] ; } ");
}
[WorkItem(544915)]
......@@ -2328,5 +2327,23 @@ class TestClass
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;
}
}
");
}
}
}
......@@ -794,11 +794,10 @@ index:=1)
End Sub
<WorkItem(543529)>
<WorkItem(909152)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)>
Public Sub TestInStatementlessConstructorParameter()
Test(
NewLines("Class C1 \n Sub New(Optional ByRef x As String = [|Nothing|]) \n End Sub \n End Class"),
NewLines("Class C1 \n Private Const {|Rename:P|} As String = Nothing \n Sub New(Optional ByRef x As String = P) \n End Sub \n End Class"))
TestMissing(NewLines("Class C1 \n Sub New(Optional ByRef x As String = [|Nothing|]) \n End Sub \n End Class"))
End Sub
<WorkItem(543650)>
......@@ -910,12 +909,10 @@ NewLines("Module M \n Sub Main() \n Dim x = <[|x|]/> \n End Sub \n End Module"))
End Sub
<WorkItem(545262)>
<WorkItem(909152)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)>
Public Sub TestInTernaryConditional()
Test(
NewLines("Module Program \n Sub Main(args As String()) \n Dim p As Object = Nothing \n Dim Obj1 = If(New With {.a = True}.a, p, [|Nothing|]) \n End Sub \n End Module"),
NewLines("Module Program \n Sub Main(args As String()) \n Dim p As Object = Nothing \n Const {|Rename:P1|} As Object = Nothing \n Dim Obj1 = If(New With {.a = True}.a, p, P1) \n End Sub \n End Module"),
index:=2)
TestMissing(NewLines("Module Program \n Sub Main(args As String()) \n Dim p As Object = Nothing \n Dim Obj1 = If(New With {.a = True}.a, p, [|Nothing|]) \n End Sub \n End Module"))
End Sub
<WorkItem(545316)>
......@@ -1401,6 +1398,24 @@ End Module
Test(code, expected, index:=3, compareTokens:=False)
End Sub
<WorkItem(909152)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)>
Public Sub TestMissingOnNothingLiteral()
TestMissing(
<File>
Imports System
Module Program
Sub Main(args As String())
Main([|Nothing|])
M(Nothing)
End Sub
Sub M(i As Integer)
End Sub
End Module
</File>)
End Sub
<WorkItem(1065661)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)>
Public Sub TestIntroduceVariableTextDoesntSpanLines()
......
......@@ -96,13 +96,24 @@ protected override bool IsInAttributeArgumentInitializer(ExpressionSyntax expres
return false;
}
/// <summary>
/// Checks for conditions where we should not generate a variable for an expression
/// </summary>
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))
{
return false;
}
// (b) For Null Literals, as AllOccurences could introduce semantic errors.
if (expression.IsKind(SyntaxKind.NullLiteralExpression))
{
return false;
}
return true;
}
......
......@@ -216,9 +216,6 @@ private TExpressionSyntax GetExpressionUnderSpan(SyntaxTree tree, TextSpan textS
private bool CanIntroduceVariable(
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))
{
return false;
......
......@@ -77,6 +77,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.IntroduceVariable
Return False
End If
' For Nothing Literals, AllOccurences could introduce semantic errors.
If expression.IsKind(SyntaxKind.NothingLiteralExpression) Then
Return False
End If
Return True
End Function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册