Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/CustomCode-Analyzer/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
Expand All @@ -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;
}

Expand Down
18 changes: 9 additions & 9 deletions tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Analyzer>.VerifyAnalyzerAsync(
test,
Expand Down