提交 aa98f1a5 编写于 作者: S Steve Chovanec

Recognize condition with logical negation

上级 250d883c
......@@ -506,7 +506,7 @@ void M(C c)
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestWithNullableTypeAndReferenceEquals()
public async Task TestWithNullableTypeAndReferenceEquals1()
{
await TestInRegularAndScriptAsync(
@"
......@@ -531,7 +531,7 @@ void M(C c)
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestWithNullableTypeAndReferenceEqualsReversed()
public async Task TestWithNullableTypeAndReferenceEquals2()
{
await TestInRegularAndScriptAsync(
@"
......@@ -556,7 +556,7 @@ void M(C c)
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestWithNullableTypeAndReferenceEqualsWithObject()
public async Task TestWithNullableTypeAndReferenceEqualsWithObject1()
{
await TestInRegularAndScriptAsync(
@"
......@@ -581,7 +581,7 @@ void M(C c)
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestWithNullableTypeAndReferenceEqualsWithObjectReversed()
public async Task TestWithNullableTypeAndReferenceEqualsWithObject2()
{
await TestInRegularAndScriptAsync(
@"
......@@ -595,6 +595,181 @@ void M(C c)
}",
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}");
}
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestWithNullableTypeAndNotIsNull()
{
await TestInRegularAndScriptAsync(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!(c is null) ? c.f : null;
}
}",
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}");
}
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestWithNullableTypeAndLogicalNotReferenceEquals1()
{
await TestInRegularAndScriptAsync(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!ReferenceEquals(c, null) ? c.f : null;
}
}",
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}");
}
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestWithNullableTypeAndLogicalNotReferenceEquals2()
{
await TestInRegularAndScriptAsync(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!ReferenceEquals(null, c) ? c.f : null;
}
}",
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}");
}
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestWithNullableTypeAndLogicalNotReferenceEqualsWithObject1()
{
await TestInRegularAndScriptAsync(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!object.ReferenceEquals(c, null) ? c.f : null;
}
}",
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}");
}
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestWithNullableTypeAndLogicalNotReferenceEqualsWithObject2()
{
await TestInRegularAndScriptAsync(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!object.ReferenceEquals(null, c) ? c.f : null;
}
}",
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}");
}
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestEqualsWithLogicalNot()
{
await TestInRegularAndScriptAsync(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!(c == null) ? c.f : null;
}
}",
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}");
}
[WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)]
public async Task TestNotEqualsWithLogicalNot()
{
await TestInRegularAndScriptAsync(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!(c != null) ? null : c.f;
}
}",
@"
class C
{
public int? f;
void M(C c)
......
......@@ -326,7 +326,7 @@ End Class")
<WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestWithNullableTypeAndReferenceEquals() As Task
Public Async Function TestWithNullableTypeAndReferenceEquals1() As Task
Await TestInRegularAndScriptAsync(
"
Imports System
......@@ -348,7 +348,7 @@ End Class")
<WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestWithNullableTypeAndReferenceEqualsReversed() As Task
Public Async Function TestWithNullableTypeAndReferenceEquals2() As Task
Await TestInRegularAndScriptAsync(
"
Imports System
......@@ -370,7 +370,7 @@ End Class")
<WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestWithNullableTypeAndReferenceEqualsWithObject() As Task
Public Async Function TestWithNullableTypeAndReferenceEqualsWithObject1() As Task
Await TestInRegularAndScriptAsync(
"
Imports System
......@@ -392,7 +392,7 @@ End Class")
<WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestWithNullableTypeAndReferenceEqualsWithObjectReversed() As Task
Public Async Function TestWithNullableTypeAndReferenceEqualsWithObject2() As Task
Await TestInRegularAndScriptAsync(
"
Imports System
......@@ -439,5 +439,116 @@ Class C
End Sub
End Class")
End Function
<WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestWithNullableTypeAndLogicalNotReferenceEquals1() As Task
Await TestInRegularAndScriptAsync(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not ReferenceEquals(o, Nothing), o.ToString(), Nothing)
End Sub
End Class",
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class")
End Function
<WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestWithNullableTypeAndLogicalNotReferenceEquals2() As Task
Await TestInRegularAndScriptAsync(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not ReferenceEquals(Nothing, o), o.ToString(), Nothing)
End Sub
End Class",
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class")
End Function
<WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestWithNullableTypeAndLogicalNotReferenceEqualsWithObject1() As Task
Await TestInRegularAndScriptAsync(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not Object.ReferenceEquals(o, Nothing), o.ToString(), Nothing)
End Sub
End Class",
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class")
End Function
<WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestEqualsWithLogicalNot() As Task
Await TestInRegularAndScriptAsync(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not (o Is Nothing), o.ToString(), Nothing)
End Sub
End Class",
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class")
End Function
<WorkItem(23043, "https://github.com/dotnet/roslyn/issues/23043")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseNullPropagation)>
Public Async Function TestNotEqualsWithLogicalNot() As Task
Await TestInRegularAndScriptAsync(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not (o IsNot Nothing), Nothing, o.ToString())
End Sub
End Class",
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class")
End Function
End Class
End Namespace
......@@ -100,6 +100,14 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, INamedTypeSymbol e
conditionNode = syntaxFacts.WalkDownParentheses(conditionNode);
var conditionIsNegated = false;
if (syntaxFacts.IsLogicalNotExpression(conditionNode))
{
conditionIsNegated = true;
conditionNode = syntaxFacts.WalkDownParentheses(
syntaxFacts.GetOperandOfPrefixUnaryExpression(conditionNode));
}
var isEqualityLikeCondition = TryAnalyzeCondition(
context, syntaxFacts, referenceEqualsMethodOpt, conditionNode,
out var conditionPartToCheck, out var isEquals);
......@@ -108,6 +116,11 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, INamedTypeSymbol e
return;
}
if (conditionIsNegated)
{
isEquals = !isEquals;
}
// Needs to be of the form:
// x == null ? null : ... or
// x != null ? ... : null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册