From 999f6e049b6d5158a3061afde47c3ff95c56ec37 Mon Sep 17 00:00:00 2001 From: Jonathan Algar <93204286+jonathanalgar@users.noreply.github.com> Date: Sun, 23 Feb 2025 00:48:15 +0000 Subject: [PATCH 1/2] first shot --- src/CustomCode-Analyzer/Analyzer.cs | 8 +++++++ .../AnalyzerTests.cs | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/CustomCode-Analyzer/Analyzer.cs b/src/CustomCode-Analyzer/Analyzer.cs index 9e3ff44..7226235 100644 --- a/src/CustomCode-Analyzer/Analyzer.cs +++ b/src/CustomCode-Analyzer/Analyzer.cs @@ -1373,6 +1373,14 @@ private static bool IsValidParameterType(ITypeSymbol typeSymbol, Compilation com return false; } + // If the type is nullable, unwrap it and check the underlying type + if (typeSymbol is INamedTypeSymbol named && + named.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T && + named.TypeArguments.Length == 1) + { + typeSymbol = named.TypeArguments[0]; + } + // Check for primitive or special types if (ValidParameterSpecialTypes.Contains(typeSymbol.SpecialType)) { diff --git a/tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs b/tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs index 9036836..d72db78 100644 --- a/tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs +++ b/tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs @@ -1136,6 +1136,30 @@ await CSharpAnalyzerVerifier.VerifyAnalyzerAsync( ); } + [TestMethod] + public async Task UnsupportedParameterTypeRule_InGlobalScope_PublicField_NullableStruct_NoWarning() + { + var test = + @" +[OSStructure] +public struct NestedStructure +{ + public int Value; +} + +[OSStructure] +public struct MyStructure +{ + public NestedStructure? NullableNested; // Nullable struct OK +} +"; + await CSharpAnalyzerVerifier.VerifyAnalyzerAsync( + test, + TestContext, + skipSDKreference: false + ); + } + // --------------- ParameterByReferenceRule (OS-ELG-MODL-05016) ------------ [TestMethod] public async Task ParameterByReferenceRule_InGlobalScope_ReportsWarning() From 5d313072d941e136be201f137add74fea4cc6deb Mon Sep 17 00:00:00 2001 From: Jonathan Algar <93204286+jonathanalgar@users.noreply.github.com> Date: Sun, 23 Feb 2025 00:52:10 +0000 Subject: [PATCH 2/2] format --- src/CustomCode-Analyzer/Analyzer.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/CustomCode-Analyzer/Analyzer.cs b/src/CustomCode-Analyzer/Analyzer.cs index 7226235..f272e10 100644 --- a/src/CustomCode-Analyzer/Analyzer.cs +++ b/src/CustomCode-Analyzer/Analyzer.cs @@ -1374,9 +1374,11 @@ private static bool IsValidParameterType(ITypeSymbol typeSymbol, Compilation com } // If the type is nullable, unwrap it and check the underlying type - if (typeSymbol is INamedTypeSymbol named && - named.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T && - named.TypeArguments.Length == 1) + if ( + typeSymbol is INamedTypeSymbol named + && named.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T + && named.TypeArguments.Length == 1 + ) { typeSymbol = named.TypeArguments[0]; }