sonar-dotnet
sonar-dotnet copied to clipboard
S3257 FP: when explicit array types in conjunction with c#9 target-typed new
Description
The S3257 RedundantDeclaration analyzer is currently reporting a false positive: error S3257: Remove the array type; it is redundant
when an explicit array type is provided and C#9 target-typed new is used for it's elements.
Repro steps
new DataEntityFieldDefinition[]
{
new(BackEndTypes.Guid, FrontEndDataTypeName.guid, "InstanceId", "InstanceId"),
new(BackEndTypes.String, FrontEndDataTypeName.text, "EMailadres", "EMailadres"),
new(BackEndTypes.String, FrontEndDataTypeName.text, "Voorletters", "Voorletters"),
new(BackEndTypes.String, FrontEndDataTypeName.text, "Roepnaam", "Roepnaam"),
new(BackEndTypes.String, FrontEndDataTypeName.text, "Geboortenaam", "Geboortenaam"),
}
Expected behavior
The explicit type is actually required for the target-typed new logic to kick in.
Actual behavior
error S3257: Remove the array type; it is redundant
is raised.
Known workarounds
Do not use target-typed new.
new []
{
new DataEntityFieldDefinition(BackEndTypes.Guid, FrontEndDataTypeName.guid, "InstanceId", "InstanceId"),
new DataEntityFieldDefinition(BackEndTypes.String, FrontEndDataTypeName.text, "EMailadres", "EMailadres"),
new DataEntityFieldDefinition(BackEndTypes.String, FrontEndDataTypeName.text, "Voorletters", "Voorletters"),
new DataEntityFieldDefinition(BackEndTypes.String, FrontEndDataTypeName.text, "Roepnaam", "Roepnaam"),
new DataEntityFieldDefinition(BackEndTypes.String, FrontEndDataTypeName.text, "Geboortenaam", "Geboortenaam"),
}
Related information
Tested using SonarAnalyzer.CSharp 8.29.0.36737.
Hi @tomdegoede, thanks for reporting this. I can confirm the false positive.
Any news on this issue? I'm running into the same issue atm