Skip to content

Commit d8e47eb

Browse files
committed
Make code fix available in arguments
1 parent e6b25b7 commit d8e47eb

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

src/CSharpIsNullAnalyzer.CodeFixes/CSIsNull001Fixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3434
if (diagnostic.Id == CSIsNull001.Id)
3535
{
3636
SyntaxNode? syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken);
37-
BinaryExpressionSyntax? expr = syntaxRoot?.FindNode(diagnostic.Location.SourceSpan).FirstAncestorOrSelf<BinaryExpressionSyntax>();
37+
BinaryExpressionSyntax? expr = syntaxRoot?.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true).FirstAncestorOrSelf<BinaryExpressionSyntax>();
3838
if (expr is not null)
3939
{
4040
context.RegisterCodeFix(

src/CSharpIsNullAnalyzer.CodeFixes/CSIsNull002Fixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4646
if (diagnostic.Id == CSIsNull002.Id)
4747
{
4848
SyntaxNode? syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken);
49-
BinaryExpressionSyntax? expr = syntaxRoot?.FindNode(diagnostic.Location.SourceSpan).FirstAncestorOrSelf<BinaryExpressionSyntax>();
49+
BinaryExpressionSyntax? expr = syntaxRoot?.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true).FirstAncestorOrSelf<BinaryExpressionSyntax>();
5050
if (expr is not null)
5151
{
5252
context.RegisterCodeFix(

test/CSharpIsNullAnalyzer.Tests/CSIsNull001Tests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,32 @@ void Method(object o)
9898

9999
await VerifyCS.VerifyCodeFixAsync(source, fixedSource);
100100
}
101+
102+
[Fact]
103+
public async Task EqualsInArgument_ProducesDiagnostic()
104+
{
105+
string source = @"
106+
class Test
107+
{
108+
void Method(object o)
109+
{
110+
Other(o [|== null|]);
111+
}
112+
113+
void Other(bool condition) { }
114+
}";
115+
116+
string fixedSource = @"
117+
class Test
118+
{
119+
void Method(object o)
120+
{
121+
Other(o is null);
122+
}
123+
124+
void Other(bool condition) { }
125+
}";
126+
127+
await VerifyCS.VerifyCodeFixAsync(source, fixedSource);
128+
}
101129
}

test/CSharpIsNullAnalyzer.Tests/CSIsNull002Tests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,44 @@ void Method(object o)
137137
await VerifyCS.VerifyCodeFixAsync(source, fixedSource1, CSIsNull002Fixer.IsObjectEquivalenceKey);
138138
await VerifyCS.VerifyCodeFixAsync(source, fixedSource2, CSIsNull002Fixer.IsNotNullEquivalenceKey);
139139
}
140+
141+
[Fact]
142+
public async Task NullNotEqualsInArgument_ProducesDiagnostic()
143+
{
144+
string source = @"
145+
class Test
146+
{
147+
void Method(object o)
148+
{
149+
Other([|null !=|] o);
150+
}
151+
152+
void Other(bool condition) { }
153+
}";
154+
155+
string fixedSource1 = @"
156+
class Test
157+
{
158+
void Method(object o)
159+
{
160+
Other(o is object);
161+
}
162+
163+
void Other(bool condition) { }
164+
}";
165+
166+
string fixedSource2 = @"
167+
class Test
168+
{
169+
void Method(object o)
170+
{
171+
Other(o is not null);
172+
}
173+
174+
void Other(bool condition) { }
175+
}";
176+
177+
await VerifyCS.VerifyCodeFixAsync(source, fixedSource1, CSIsNull002Fixer.IsObjectEquivalenceKey);
178+
await VerifyCS.VerifyCodeFixAsync(source, fixedSource2, CSIsNull002Fixer.IsNotNullEquivalenceKey);
179+
}
140180
}

0 commit comments

Comments
 (0)