sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

Fix S1172 FP: Target typed new operator (C# 9.0)

Open AHarman opened this issue 1 year ago • 3 comments

Description

S1172 triggers a false positive - parameters are used but a warning is applied anyway. This is appearing in Visual Studio, but not SonarCloud.

Repro steps

This triggers it:

private static MyClass CreateObj(string param1, string param2) => new()
{
    Prop1 = param1,
    Prop2 = param2,
    Prop3 = "a value"
};

Expected behavior

The warning does not appear, because the parameters are used.

Actual behavior

The warning appears, even though the parameters are used

Known workarounds

The following examples do not trigger the false positive:

private static MyClass CreateObj(string param1, string param2) => new MyClass()
{
    Prop1 = param1,
    Prop2 = param2,
    Prop3 = "a value"
};
private static MyClass CreateObj(string param1, string param2) {
    return new()
    {
        Prop1 = param1,
        Prop2 = param2,
        Prop3 = "a value"
    };
}

Related information

  • C#/VB.NET Plugins version
    • C# 11
  • Visual Studio version
    • 17.4.4
  • MSBuild / dotnet version
    • .Net 7
  • SonarScanner for .NET version (if used)
  • Operating System
    • Windows 10

Not mentioned in the default list, but I'm using SonarLint for Visual Studio - 6.16.0.69538

AHarman avatar Jun 22 '23 11:06 AHarman

Hi @AHarman. I've been unable to replicate this false positive in Visual Studio 17.6 with SonarLint 6.16.0.69538. Could you check if the problem still appears if you upgrade Visual Studio from 17.4.4 to the latest release? If it does then please attach the zipped sample project here.

Hi @zsolt-kolbay-sonarsource, the problem still happens on the latest version of Visual Studio.

S1172.zip

AHarman avatar Jun 23 '23 12:06 AHarman

Hi @AHarman. Thank you for the attached project. The issue is confirmed as False Positive.