SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
788 stars 227 forks source link

Fix S4144 FP: Implicit object creation expression #9654

Open nrankin18 opened 2 months ago

nrankin18 commented 2 months ago

Description

Rule S4144 is raised when two methods have identical bodies but are not identical due to different return types.

Repro steps

public record Foo(string A);
public record Bar(string A);

private static Foo Test1()
{
  string s = "A";
  return new(s);
}

private static Bar Test2() // S4144 is flagged here
{
  string s = "A";
  return new(s);
}

Expected behavior

S4144 should not be raised since the methods are different (one returns Foo, the other Bar).

Actual behavior

S4144 is raised.

Known workarounds

Inhibit S4144

Related information

sebastien-marichal commented 2 months ago

Hello @nrankin18,

Thank you for reporting this issue; I confirm this to be a false positive.

The issue is due to the usage of implicit object creation new(). When using explicit object creation, the issue does not raise.

I will add it to our backlog to fix it.

Have a nice day!