Skip to content

Commit 54e2846

Browse files
committed
Fix tests for nullable
1 parent 392f866 commit 54e2846

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

  • ReflectionAnalyzers.Tests/REFL033UseSameTypeAsParameterTests
  • ReflectionAnalyzers/NodeAnalzers

ReflectionAnalyzers.Tests/REFL033UseSameTypeAsParameterTests/Valid.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,33 @@ namespace N
2020
2121
class C
2222
{
23-
public object Get => typeof(C).GetMethod(nameof(Static), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(IComparable) }, null);
23+
public MemberInfo? Get => typeof(C).GetMethod(nameof(Static), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(IComparable) }, null);
2424
2525
public static IComparable Static(IComparable i) => i;
2626
}
2727
}";
2828

29-
RoslynAssert.Valid(Analyzer, Descriptor, code);
29+
RoslynAssert.Valid(Analyzer, Descriptor, code, settings: LibrarySettings.NullableEnabled);
30+
}
31+
32+
[Test]
33+
public static void NullableParameter()
34+
{
35+
var code = @"
36+
namespace N
37+
{
38+
using System;
39+
using System.Reflection;
40+
41+
class C
42+
{
43+
public MemberInfo? Get => typeof(C).GetMethod(nameof(Static), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(IComparable) }, null);
44+
45+
public static IComparable? Static(IComparable? i) => i;
46+
}
47+
}";
48+
49+
RoslynAssert.Valid(Analyzer, Descriptor, code, settings: LibrarySettings.NullableEnabled);
3050
}
3151

3252
[Test]

ReflectionAnalyzers/NodeAnalzers/GetXAnalyzer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ private static bool ShouldUseSameTypeAsParameter(ReflectedMember member, Types t
667667

668668
for (var i = 0; i < method.Parameters.Length; i++)
669669
{
670-
if (!TypeSymbolComparer.Equal(types.Symbols[i], method.Parameters[i].Type) &&
670+
if (!TypeSymbolComparer.Equal(types.Symbols[i], Effective(method.Parameters[i].Type)) &&
671671
context.SemanticModel.IsAccessible(context.Node.SpanStart, method.Parameters[i].Type))
672672
{
673673
typeText = method.Parameters[i].Type.ToString(context);
@@ -679,6 +679,11 @@ private static bool ShouldUseSameTypeAsParameter(ReflectedMember member, Types t
679679
: argument.GetLocation();
680680
return true;
681681
}
682+
683+
ITypeSymbol Effective(ITypeSymbol t) =>
684+
t.NullableAnnotation == NullableAnnotation.Annotated
685+
? t.WithNullableAnnotation(NullableAnnotation.None)
686+
: t;
682687
}
683688
}
684689

0 commit comments

Comments
 (0)