Skip to content

Commit 42ad546

Browse files
Fix false positive for unsupported default values on nullable parameters (#29)
* first shot * merge ifs
1 parent c78d163 commit 42ad546

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/CustomCode-Analyzer/Analyzer.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,11 +1323,22 @@ private static bool IsValidParameterDefaultValue(IParameterSymbol parameter)
13231323
return true;
13241324
}
13251325

1326-
// Check if type is supported
1326+
// Unwrap nullable types to check the underlying type
1327+
ITypeSymbol typeToCheck = parameter.Type;
13271328
if (
1328-
!ValidParameterSpecialTypes.Contains(parameter.Type.SpecialType)
1329+
typeToCheck.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T
1330+
&& typeToCheck is INamedTypeSymbol namedType
1331+
&& namedType.TypeArguments.Length == 1
1332+
)
1333+
{
1334+
typeToCheck = namedType.TypeArguments[0];
1335+
}
1336+
1337+
// Check if the (unwrapped) type is supported
1338+
if (
1339+
!ValidParameterSpecialTypes.Contains(typeToCheck.SpecialType)
13291340
&& !(
1330-
parameter.Type is IArrayTypeSymbol arrayType
1341+
typeToCheck is IArrayTypeSymbol arrayType
13311342
&& arrayType.ElementType.SpecialType == SpecialType.System_Byte
13321343
)
13331344
)
@@ -1347,8 +1358,7 @@ parameter.Type is IArrayTypeSymbol arrayType
13471358
return false;
13481359
}
13491360

1350-
// Check if the default value is a literal expression
1351-
// This ensures that the default value is a compile-time constant
1361+
// Check if the default value is a literal expression (compile-time constant)
13521362
return parameterSyntax.Default?.Value is LiteralExpressionSyntax;
13531363
}
13541364

tests/CustomCode-Analyzer.Tests/AnalyzerTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,25 +1778,25 @@ public async Task UnsupportedDefaultValueRule_ValidLiterals_NoWarning()
17781778
public interface ITestInterface
17791779
{
17801780
void TestMethod(string text = ""hello"",
1781-
int number = 42,
1781+
int? number = 42,
17821782
long bigNumber = 123L,
17831783
double precise = 3.14,
17841784
decimal money = 10.5m,
17851785
bool flag = true,
1786-
DateTime date = default,
1786+
DateTime? date = default,
17871787
string nullString = null);
17881788
}
17891789
17901790
public class Implementation : ITestInterface
17911791
{
17921792
public void TestMethod(string text = ""hello"",
1793-
int number = 42,
1794-
long bigNumber = 123L,
1795-
double precise = 3.14,
1796-
decimal money = 10.5m,
1797-
bool flag = true,
1798-
DateTime date = default,
1799-
string nullString = null) { }
1793+
int? number = 42,
1794+
long bigNumber = 123L,
1795+
double precise = 3.14,
1796+
decimal money = 10.5m,
1797+
bool flag = true,
1798+
DateTime? date = default,
1799+
string nullString = null) { }
18001800
}";
18011801
await CSharpAnalyzerVerifier<Analyzer>.VerifyAnalyzerAsync(
18021802
test,

0 commit comments

Comments
 (0)