Skip to content

Commit 104ba14

Browse files
committed
Handle bang operator
1 parent 1970076 commit 104ba14

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

ReflectionAnalyzers.Tests/REFL001CastReturnValueTests/CodeFix.MethodInfoInvoke.cs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,69 @@ public static class MethodInfoInvoke
1111
private static readonly CastReturnValueFix Fix = new();
1212
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.REFL001CastReturnValue);
1313

14-
[Test]
15-
public static void AssigningLocal()
14+
[TestCase("typeof(C).GetMethod(nameof(M)).Invoke(null, null)")]
15+
[TestCase("typeof(C).GetMethod(nameof(M))!.Invoke(null, null)")]
16+
[TestCase("typeof(C).GetMethod(nameof(M))?.Invoke(null, null)")]
17+
[TestCase("typeof(C).GetMethod(nameof(M))?.Invoke(null, null) ?? throw new Exception()")]
18+
public static void AssigningLocal(string expression)
1619
{
1720
var before = @"
21+
#pragma warning disable CS8019, CS8602, CS8605
1822
namespace N
1923
{
24+
using System;
25+
2026
public class C
2127
{
2228
public C()
2329
{
24-
var value = ↓typeof(C).GetMethod(nameof(M)).Invoke(null, null);
30+
var x = ↓typeof(C).GetMethod(nameof(M)).Invoke(null, null);
2531
}
2632
2733
public static int M() => 0;
2834
}
29-
}";
35+
}".AssertReplace("typeof(C).GetMethod(nameof(M)).Invoke(null, null)", expression);
3036

3137
var after = @"
38+
#pragma warning disable CS8019, CS8602, CS8605
3239
namespace N
3340
{
41+
using System;
42+
3443
public class C
3544
{
3645
public C()
3746
{
38-
var value = (int)typeof(C).GetMethod(nameof(M)).Invoke(null, null);
47+
var x = (int)typeof(C).GetMethod(nameof(M)).Invoke(null, null);
3948
}
4049
50+
public static int M() => 0;
51+
}
52+
}".AssertReplace("typeof(C).GetMethod(nameof(M)).Invoke(null, null)", expression);
53+
RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after);
54+
}
55+
56+
[Test]
57+
public static void Returning()
58+
{
59+
var before = @"
60+
namespace N
61+
{
62+
public class C
63+
{
64+
public object? Get() => ↓typeof(C).GetMethod(nameof(M)).Invoke(null, null);
65+
66+
public static int M() => 0;
67+
}
68+
}";
69+
70+
var after = @"
71+
namespace N
72+
{
73+
public class C
74+
{
75+
public object? Get() => (int)typeof(C).GetMethod(nameof(M)).Invoke(null, null);
76+
4177
public static int M() => 0;
4278
}
4379
}";

ReflectionAnalyzers/Helpers/Reflection/GetX.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
{
33
using System.Diagnostics.CodeAnalysis;
44
using System.Threading;
5+
56
using Gu.Roslyn.AnalyzerExtensions;
7+
68
using Microsoft.CodeAnalysis;
79
using Microsoft.CodeAnalysis.CSharp;
810
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -179,6 +181,10 @@ private static bool TryFindInvocation(MemberAccessExpressionSyntax memberAccess,
179181
{
180182
switch (memberAccess.Expression)
181183
{
184+
case PostfixUnaryExpressionSyntax { RawKind: (int)SyntaxKind.SuppressNullableWarningExpression, Operand: InvocationExpressionSyntax candidate }
185+
when candidate.TryGetTarget(expected, semanticModel, cancellationToken, out _):
186+
invocation = candidate;
187+
return true;
182188
case InvocationExpressionSyntax candidate
183189
when candidate.TryGetTarget(expected, semanticModel, cancellationToken, out _):
184190
invocation = candidate;

0 commit comments

Comments
 (0)