diff --git a/src/CustomCode-Analyzer/Analyzer.cs b/src/CustomCode-Analyzer/Analyzer.cs index 05ce6a9..78a35ed 100644 --- a/src/CustomCode-Analyzer/Analyzer.cs +++ b/src/CustomCode-Analyzer/Analyzer.cs @@ -1298,11 +1298,22 @@ 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 ( - !ValidParameterSpecialTypes.Contains(parameter.Type.SpecialType) + typeToCheck.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T + && typeToCheck is INamedTypeSymbol namedType + && namedType.TypeArguments.Length == 1 + ) + { + typeToCheck = namedType.TypeArguments[0]; + } + + // Check if the (unwrapped) type is supported + if ( + !ValidParameterSpecialTypes.Contains(typeToCheck.SpecialType) && !( - parameter.Type is IArrayTypeSymbol arrayType + typeToCheck is IArrayTypeSymbol arrayType && arrayType.ElementType.SpecialType == SpecialType.System_Byte ) ) @@ -1322,8 +1333,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,