From 159e1d775aa229ff6fb01a173aceddfbad01062a Mon Sep 17 00:00:00 2001 From: Jonathan Algar <93204286+jonathanalgar@users.noreply.github.com> Date: Sat, 22 Feb 2025 13:21:25 +0000 Subject: [PATCH 1/2] first shot --- src/CustomCode-Analyzer/Analyzer.cs | 22 ++++++++++++++----- .../AnalyzerTests.cs | 18 +++++++-------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/CustomCode-Analyzer/Analyzer.cs b/src/CustomCode-Analyzer/Analyzer.cs index 05ce6a9..6621bdb 100644 --- a/src/CustomCode-Analyzer/Analyzer.cs +++ b/src/CustomCode-Analyzer/Analyzer.cs @@ -1298,11 +1298,24 @@ private static bool IsValidParameterDefaultValue(IParameterSymbol parameter) return true; } - // Check if type is supported + // Unwrap nullable types to check the underlying type + ITypeSymbol typeToCheck = parameter.Type; + if (typeToCheck.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) + { + if ( + typeToCheck is INamedTypeSymbol namedType + && namedType.TypeArguments.Length == 1 + ) + { + typeToCheck = namedType.TypeArguments[0]; + } + } + + // Check if the (unwrapped) type is supported if ( - !ValidParameterSpecialTypes.Contains(parameter.Type.SpecialType) + !ValidParameterSpecialTypes.Contains(typeToCheck.SpecialType) && !( - parameter.Type is IArrayTypeSymbol arrayType + typeToCheck is IArrayTypeSymbol arrayType && arrayType.ElementType.SpecialType == SpecialType.System_Byte ) ) @@ -1322,8 +1335,7 @@ parameter.Type is IArrayTypeSymbol arrayType return false; } - // Check if the default value is a literal expression - // This ensures that the default value is a compile-time constant + // Check if the default value is a literal expression (compile-time constant) return parameterSyntax.Default?.Value is LiteralExpressionSyntax; } diff --git a/tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs b/tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs index 4828f49..f100cb8 100644 --- a/tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs +++ b/tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs @@ -1778,25 +1778,25 @@ public async Task UnsupportedDefaultValueRule_ValidLiterals_NoWarning() public interface ITestInterface { void TestMethod(string text = ""hello"", - int number = 42, + int? number = 42, long bigNumber = 123L, double precise = 3.14, decimal money = 10.5m, bool flag = true, - DateTime date = default, + DateTime? date = default, string nullString = null); } public class Implementation : ITestInterface { public void TestMethod(string text = ""hello"", - int number = 42, - long bigNumber = 123L, - double precise = 3.14, - decimal money = 10.5m, - bool flag = true, - DateTime date = default, - string nullString = null) { } + int? number = 42, + long bigNumber = 123L, + double precise = 3.14, + decimal money = 10.5m, + bool flag = true, + DateTime? date = default, + string nullString = null) { } }"; await CSharpAnalyzerVerifier.VerifyAnalyzerAsync( test, From b703ec12707155d676cf87a299f33743457c4f62 Mon Sep 17 00:00:00 2001 From: Jonathan Algar <93204286+jonathanalgar@users.noreply.github.com> Date: Sat, 22 Feb 2025 13:55:19 +0000 Subject: [PATCH 2/2] merge ifs --- src/CustomCode-Analyzer/Analyzer.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/CustomCode-Analyzer/Analyzer.cs b/src/CustomCode-Analyzer/Analyzer.cs index 6621bdb..78a35ed 100644 --- a/src/CustomCode-Analyzer/Analyzer.cs +++ b/src/CustomCode-Analyzer/Analyzer.cs @@ -1300,15 +1300,13 @@ private static bool IsValidParameterDefaultValue(IParameterSymbol parameter) // Unwrap nullable types to check the underlying type ITypeSymbol typeToCheck = parameter.Type; - if (typeToCheck.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) + if ( + typeToCheck.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T + && typeToCheck is INamedTypeSymbol namedType + && namedType.TypeArguments.Length == 1 + ) { - if ( - typeToCheck is INamedTypeSymbol namedType - && namedType.TypeArguments.Length == 1 - ) - { - typeToCheck = namedType.TypeArguments[0]; - } + typeToCheck = namedType.TypeArguments[0]; } // Check if the (unwrapped) type is supported