Skip to content

Commit 1fd8ff9

Browse files
Unwrap nullable in IsValidParameterType (#33)
* first shot * format
1 parent be00d3f commit 1fd8ff9

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/CustomCode-Analyzer/Analyzer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,16 @@ private static bool IsValidParameterType(ITypeSymbol typeSymbol, Compilation com
13731373
return false;
13741374
}
13751375

1376+
// If the type is nullable, unwrap it and check the underlying type
1377+
if (
1378+
typeSymbol is INamedTypeSymbol named
1379+
&& named.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T
1380+
&& named.TypeArguments.Length == 1
1381+
)
1382+
{
1383+
typeSymbol = named.TypeArguments[0];
1384+
}
1385+
13761386
// Check for primitive or special types
13771387
if (ValidParameterSpecialTypes.Contains(typeSymbol.SpecialType))
13781388
{

tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,30 @@ await CSharpAnalyzerVerifier<Analyzer>.VerifyAnalyzerAsync(
11361136
);
11371137
}
11381138

1139+
[TestMethod]
1140+
public async Task UnsupportedParameterTypeRule_InGlobalScope_PublicField_NullableStruct_NoWarning()
1141+
{
1142+
var test =
1143+
@"
1144+
[OSStructure]
1145+
public struct NestedStructure
1146+
{
1147+
public int Value;
1148+
}
1149+
1150+
[OSStructure]
1151+
public struct MyStructure
1152+
{
1153+
public NestedStructure? NullableNested; // Nullable struct OK
1154+
}
1155+
";
1156+
await CSharpAnalyzerVerifier<Analyzer>.VerifyAnalyzerAsync(
1157+
test,
1158+
TestContext,
1159+
skipSDKreference: false
1160+
);
1161+
}
1162+
11391163
// --------------- ParameterByReferenceRule (OS-ELG-MODL-05016) ------------
11401164
[TestMethod]
11411165
public async Task ParameterByReferenceRule_InGlobalScope_ReportsWarning()

0 commit comments

Comments
 (0)