Skip to content

Commit bb71e97

Browse files
committed
handle conditional access
1 parent c7794af commit bb71e97

10 files changed

Lines changed: 103 additions & 56 deletions

File tree

ReflectionAnalyzers.Tests/REFL001CastReturnValueTests/CodeFix.MethodInfoInvoke.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class MethodInfoInvoke
1313

1414
[TestCase("typeof(C).GetMethod(nameof(M)).Invoke(null, null)")]
1515
[TestCase("typeof(C).GetMethod(nameof(M))!.Invoke(null, null)")]
16-
//[TestCase("typeof(C).GetMethod(nameof(M))?.Invoke(null, null)")]
16+
[TestCase("typeof(C).GetMethod(nameof(M))?.Invoke(null, null)")]
1717
//[TestCase("typeof(C).GetMethod(nameof(M))?.Invoke(null, null) ?? throw new Exception()")]
1818
public static void AssigningLocal(string expression)
1919
{
@@ -53,12 +53,11 @@ public C()
5353
RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after, settings: LibrarySettings.NullableEnabled);
5454
}
5555

56-
[Explicit("Fix later")]
5756
[Test]
58-
public static void Returning()
57+
public static void ReturningExpressionBody()
5958
{
6059
var before = @"
61-
#pragma warning disable CS8602
60+
#pragma warning disable CS8602, CS8605
6261
namespace N
6362
{
6463
public class C
@@ -70,13 +69,48 @@ public class C
7069
}";
7170

7271
var after = @"
73-
#pragma warning disable CS8602
72+
#pragma warning disable CS8602, CS8605
7473
namespace N
7574
{
7675
public class C
7776
{
7877
public object? Get() => (int)typeof(C).GetMethod(nameof(M)).Invoke(null, null);
7978
79+
public static int M() => 0;
80+
}
81+
}";
82+
RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after, settings: LibrarySettings.NullableEnabled);
83+
}
84+
85+
[Test]
86+
public static void ReturningStatementBody()
87+
{
88+
var before = @"
89+
#pragma warning disable CS8602, CS8605
90+
namespace N
91+
{
92+
public class C
93+
{
94+
public object? Get()
95+
{
96+
return ↓typeof(C).GetMethod(nameof(M)).Invoke(null, null);
97+
}
98+
99+
public static int M() => 0;
100+
}
101+
}";
102+
103+
var after = @"
104+
#pragma warning disable CS8602, CS8605
105+
namespace N
106+
{
107+
public class C
108+
{
109+
public object? Get()
110+
{
111+
return (int)typeof(C).GetMethod(nameof(M)).Invoke(null, null);
112+
}
113+
80114
public static int M() => 0;
81115
}
82116
}";

ReflectionAnalyzers.Tests/REFL041CreateDelegateTypeTests/Valid.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace N
2222
class C
2323
{
2424
25-
public static object Get => Delegate.CreateDelegate(
25+
public static object Get => (Func<Type, string, bool, ParameterExpression>)Delegate.CreateDelegate(
2626
typeof(Func<Type, string, bool, ParameterExpression>),
2727
typeof(ParameterExpression).GetMethod(""Make"", BindingFlags.Static | BindingFlags.NonPublic));
2828
}
@@ -42,7 +42,7 @@ class C
4242
{
4343
public static int M(string arg) => arg.Length;
4444
45-
public static object Get => Delegate.CreateDelegate(
45+
public static object Get => (Func<string, int>)Delegate.CreateDelegate(
4646
typeof(Func<string, int>),
4747
typeof(C).GetMethod(nameof(M)));
4848
}
@@ -62,7 +62,7 @@ class C
6262
{
6363
public static int M(string arg) => arg.Length;
6464
65-
public static object Get => Delegate.CreateDelegate(
65+
public static object Get => (Func<int>)Delegate.CreateDelegate(
6666
typeof(Func<int>),
6767
string.Empty,
6868
typeof(C).GetMethod(nameof(M)));
@@ -83,7 +83,7 @@ class C
8383
{
8484
public static void M() { }
8585
86-
public static object Get => Delegate.CreateDelegate(
86+
public static object Get => (Action)Delegate.CreateDelegate(
8787
typeof(Action),
8888
typeof(C).GetMethod(nameof(M)));
8989
}
@@ -103,7 +103,7 @@ class C
103103
{
104104
public static void M(string arg) { }
105105
106-
public static object Get => Delegate.CreateDelegate(
106+
public static object Get => (Action<string>)Delegate.CreateDelegate(
107107
typeof(Action<string>),
108108
typeof(C).GetMethod(nameof(M)));
109109
}
@@ -124,7 +124,7 @@ class C
124124
{
125125
public static void M(string arg) { }
126126
127-
public static object Get => Delegate.CreateDelegate(
127+
public static object Get => (Action)Delegate.CreateDelegate(
128128
typeof(Action),
129129
string.Empty,
130130
typeof(C).GetMethod(nameof(M)));
@@ -146,7 +146,7 @@ class C
146146
{
147147
public static void M(string arg1, string arg2) { }
148148
149-
public static object Get => Delegate.CreateDelegate(
149+
public static object Get => (Action<string>)Delegate.CreateDelegate(
150150
typeof(Action<string>),
151151
string.Empty,
152152
typeof(C).GetMethod(nameof(M)));
@@ -168,7 +168,7 @@ class C
168168
{
169169
public int M(string arg) => arg.Length;
170170
171-
public static object Get => Delegate.CreateDelegate(
171+
public static object Get => (Func<C, string, int>)Delegate.CreateDelegate(
172172
typeof(Func<C, string, int>),
173173
typeof(C).GetMethod(nameof(M)));
174174
}
@@ -188,7 +188,7 @@ class C
188188
{
189189
public int M(string arg) => arg.Length;
190190
191-
public static object Get => Delegate.CreateDelegate(
191+
public static object Get => (Func<string, int>)Delegate.CreateDelegate(
192192
typeof(Func<string, int>),
193193
new C(),
194194
typeof(C).GetMethod(nameof(M)));
@@ -211,7 +211,7 @@ class C
211211
212212
public static int M(string arg) => arg.Length;
213213
214-
public static object Get => Delegate.CreateDelegate(
214+
public static object Get => (StringInt)Delegate.CreateDelegate(
215215
typeof(StringInt),
216216
typeof(C).GetMethod(nameof(M)));
217217
}

ReflectionAnalyzers.Tests/REFL042FirstArgumentMustBeReferenceTypeTests/Valid.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class C
2121
{
2222
public static int M(string arg) => arg.Length;
2323
24-
public static object Get => Delegate.CreateDelegate(
24+
public static object Get => (Func<int>)Delegate.CreateDelegate(
2525
typeof(Func<int>),
2626
string.Empty,
2727
typeof(C).GetMethod(nameof(M)));
@@ -42,7 +42,7 @@ class C
4242
{
4343
public static void M(string arg) { }
4444
45-
public static object Get => Delegate.CreateDelegate(
45+
public static object Get => (Action)Delegate.CreateDelegate(
4646
typeof(Action),
4747
string.Empty,
4848
typeof(C).GetMethod(nameof(M)));
@@ -66,7 +66,7 @@ class C
6666
{
6767
public static void M(object arg) { }
6868
69-
public static object Get => Delegate.CreateDelegate(
69+
public static object Get => (Action)Delegate.CreateDelegate(
7070
typeof(Action),
7171
1,
7272
typeof(C).GetMethod(nameof(M)));
@@ -88,7 +88,7 @@ class C
8888
{
8989
public static void M(string arg1, string arg2) { }
9090
91-
public static object Get => Delegate.CreateDelegate(
91+
public static object Get => (Action<string>)Delegate.CreateDelegate(
9292
typeof(Action<string>),
9393
string.Empty,
9494
typeof(C).GetMethod(nameof(M)));
@@ -111,7 +111,7 @@ namespace N
111111
class C
112112
{
113113
114-
public static object Get => Delegate.CreateDelegate(
114+
public static object Get => (Func<Type, string, bool, ParameterExpression>)Delegate.CreateDelegate(
115115
typeof(Func<Type, string, bool, ParameterExpression>),
116116
typeof(ParameterExpression).GetMethod(""Make"", BindingFlags.Static | BindingFlags.NonPublic));
117117
}
@@ -131,7 +131,7 @@ class C
131131
{
132132
public static int M(string arg) => arg.Length;
133133
134-
public static object Get => Delegate.CreateDelegate(
134+
public static object Get => (Func<string, int>)Delegate.CreateDelegate(
135135
typeof(Func<string, int>),
136136
typeof(C).GetMethod(nameof(M)));
137137
}
@@ -151,7 +151,7 @@ class C
151151
{
152152
public static void M() { }
153153
154-
public static object Get => Delegate.CreateDelegate(
154+
public static object Get => (Action)Delegate.CreateDelegate(
155155
typeof(Action),
156156
typeof(C).GetMethod(nameof(M)));
157157
}
@@ -171,7 +171,7 @@ class C
171171
{
172172
public static void M(string arg) { }
173173
174-
public static object Get => Delegate.CreateDelegate(
174+
public static object Get => (Action<string>)Delegate.CreateDelegate(
175175
typeof(Action<string>),
176176
typeof(C).GetMethod(nameof(M)));
177177
}
@@ -192,7 +192,7 @@ class C
192192
{
193193
public int M(string arg) => arg.Length;
194194
195-
public static object Get => Delegate.CreateDelegate(
195+
public static object Get => (Func<C, string, int>)Delegate.CreateDelegate(
196196
typeof(Func<C, string, int>),
197197
typeof(C).GetMethod(nameof(M)));
198198
}
@@ -212,7 +212,7 @@ class C
212212
{
213213
public int M(string arg) => arg.Length;
214214
215-
public static object Get => Delegate.CreateDelegate(
215+
public static object Get => (Func<string, int>)Delegate.CreateDelegate(
216216
typeof(Func<string, int>),
217217
new C(),
218218
typeof(C).GetMethod(nameof(M)));
@@ -235,7 +235,7 @@ class C
235235
236236
public static int M(string arg) => arg.Length;
237237
238-
public static object Get => Delegate.CreateDelegate(
238+
public static object Get => (StringInt)Delegate.CreateDelegate(
239239
typeof(StringInt),
240240
typeof(C).GetMethod(nameof(M)));
241241
}

ReflectionAnalyzers.Tests/REFL043FirstArgumentTypeTests/Valid.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class C
2222
{
2323
public static int M(string arg) => arg.Length;
2424
25-
public static object Get => Delegate.CreateDelegate(
25+
public static object Get => (Func<int>)Delegate.CreateDelegate(
2626
typeof(Func<int>),
2727
string.Empty,
2828
typeof(C).GetMethod(nameof(M)));
@@ -43,7 +43,7 @@ class C
4343
{
4444
public static void M(string arg) { }
4545
46-
public static object Get => Delegate.CreateDelegate(
46+
public static object Get => (Action)Delegate.CreateDelegate(
4747
typeof(Action),
4848
string.Empty,
4949
typeof(C).GetMethod(nameof(M)));
@@ -67,7 +67,7 @@ class C
6767
{
6868
public static void M(object arg) { }
6969
70-
public static object Get => Delegate.CreateDelegate(
70+
public static object Get => (Action)Delegate.CreateDelegate(
7171
typeof(Action),
7272
1,
7373
typeof(C).GetMethod(nameof(M)));
@@ -89,7 +89,7 @@ class C
8989
{
9090
public static void M(string arg1, string arg2) { }
9191
92-
public static object Get => Delegate.CreateDelegate(
92+
public static object Get => (Action<string>)Delegate.CreateDelegate(
9393
typeof(Action<string>),
9494
string.Empty,
9595
typeof(C).GetMethod(nameof(M)));
@@ -112,7 +112,7 @@ namespace N
112112
class C
113113
{
114114
115-
public static object Get => Delegate.CreateDelegate(
115+
public static object Get => (Func<Type, string, bool, ParameterExpression>)Delegate.CreateDelegate(
116116
typeof(Func<Type, string, bool, ParameterExpression>),
117117
typeof(ParameterExpression).GetMethod(""Make"", BindingFlags.Static | BindingFlags.NonPublic));
118118
}
@@ -132,7 +132,7 @@ class C
132132
{
133133
public static int M(string arg) => arg.Length;
134134
135-
public static object Get => Delegate.CreateDelegate(
135+
public static object Get => (Func<string, int>)Delegate.CreateDelegate(
136136
typeof(Func<string, int>),
137137
typeof(C).GetMethod(nameof(M)));
138138
}
@@ -152,7 +152,7 @@ class C
152152
{
153153
public static void M() { }
154154
155-
public static object Get => Delegate.CreateDelegate(
155+
public static object Get => (Action)Delegate.CreateDelegate(
156156
typeof(Action),
157157
typeof(C).GetMethod(nameof(M)));
158158
}
@@ -172,7 +172,7 @@ class C
172172
{
173173
public static void M(string arg) { }
174174
175-
public static object Get => Delegate.CreateDelegate(
175+
public static object Get => (Action<string>)Delegate.CreateDelegate(
176176
typeof(Action<string>),
177177
typeof(C).GetMethod(nameof(M)));
178178
}
@@ -193,7 +193,7 @@ class C
193193
{
194194
public int M(string arg) => arg.Length;
195195
196-
public static object Get => Delegate.CreateDelegate(
196+
public static object Get => (Func<C, string, int>)Delegate.CreateDelegate(
197197
typeof(Func<C, string, int>),
198198
typeof(C).GetMethod(nameof(M)));
199199
}
@@ -213,7 +213,7 @@ class C
213213
{
214214
public int M(string arg) => arg.Length;
215215
216-
public static object Get => Delegate.CreateDelegate(
216+
public static object Get => (Func<string, int>)Delegate.CreateDelegate(
217217
typeof(Func<string, int>),
218218
new C(),
219219
typeof(C).GetMethod(nameof(M)));
@@ -236,7 +236,7 @@ class C
236236
237237
public static int M(string arg) => arg.Length;
238238
239-
public static object Get => Delegate.CreateDelegate(
239+
public static object Get => (StringInt)Delegate.CreateDelegate(
240240
typeof(StringInt),
241241
typeof(C).GetMethod(nameof(M)));
242242
}

ReflectionAnalyzers/Codefixes/CastReturnValueFix.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ protected override async Task RegisterCodeFixesAsync(DocumentEditorCodeFixContex
3737
nameof(CastReturnValueFix),
3838
diagnostic);
3939
}
40-
else if (syntaxRoot?.FindNode(diagnostic.Location.SourceSpan) is InvocationExpressionSyntax invocation)
40+
else if (syntaxRoot?.FindNode(diagnostic.Location.SourceSpan) is ExpressionSyntax expression)
4141
{
4242
context.RegisterCodeFix(
4343
$"Cast to {typeString}.",
4444
(editor, _) => editor.ReplaceNode(
45-
invocation,
45+
expression,
4646
x => SyntaxFactory.CastExpression(SyntaxFactory.ParseTypeName(typeString!), x)),
4747
nameof(CastReturnValueFix),
4848
diagnostic);

0 commit comments

Comments
 (0)