Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S2743 FP: Should not raise when base type is generic #7521

Closed
zsolt-kolbay-sonarsource opened this issue Jun 30, 2023 · 3 comments · Fixed by #9404
Closed

Fix S2743 FP: Should not raise when base type is generic #7521

zsolt-kolbay-sonarsource opened this issue Jun 30, 2023 · 3 comments · Fixed by #9404
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Milestone

Comments

@zsolt-kolbay-sonarsource
Copy link
Contributor

zsolt-kolbay-sonarsource commented Jun 30, 2023

Description

S2743 raises a warning when non-generic static fields are declared in a generic type. The recommendation is to move these fields to a non-generic base class (or create one if it doesn't exist).
Such a solution is not feasible if the base type is also generic, as the same issue would be raised there.
This FP is also raised in the sonar-dotnet repo.

Repro steps

class BaseClass<T> { }
class DerivedClass<T> : BaseClass<T>
{
    private static readonly string[] field = new[] { "a", "b", "c" }; // FP
    public string[] SomeMethod() => field;
}

Expected behavior

S2743 should not be raised.

Actual behavior

S2743 is raised on the static field.

Known workarounds

None.

Related information

  • C#/VB.NET Plugins version: 9.4
  • Visual Studio version: 17.6
  • MSBuild / dotnet version: .NET 7.0
  • Operating System: Windows 10 (10.0.19045)
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Jun 30, 2023
@Tim-Pohlmann
Copy link
Contributor

Tim-Pohlmann commented Apr 9, 2024

@zsolt-kolbay-sonarsource this can be solved by creating an additional base class or am I missing something?

class BaseClass
{
    protected static readonly string[] field = new[] { "a", "b", "c" }; // Compliant
}
class BaseClass<T> : BaseClass { }
class DerivedClass<T> : BaseClass<T>
{
    public string[] SomeMethod() => field;
}

@zsolt-kolbay-sonarsource
Copy link
Contributor Author

zsolt-kolbay-sonarsource commented Apr 9, 2024

@Tim-Pohlmann
What if the generic base class is not under our control or we just don't wish to modify it?
How would you fix the raised issue in this case? The analyzer raises a warning on those 5 static readonly members.
Where can we move them to eliminate the warnings?

  • move them to a different class? Possible, but they are only used by the DoNotUseDateTimeNowBase class, I think they should stay inside.
  • move them to a nested class inside DoNotUseDateTimeNowBase? The analyzer doesn't raise warnings, but I don't think that will fix the issue. I'm not sure if the nested class save the readonly members from being created multiple times.
  • Any other ideas?

@Tim-Pohlmann
Copy link
Contributor

According to RSPEC, they should go to SonarDiagnosticAnalyzer (the non-generic base class). I agree, however, that this is suboptimal. Maybe the better approach for S2743 is to not raise on read-only fields with an immutable type. The latter might be hard to detect, though.
Another workaround would be to make the fields non-static.

@github-actions github-actions bot added this to Review in progress in Best Kanban Jun 5, 2024
@github-actions github-actions bot moved this from Review in progress to Review approved in Best Kanban Jun 5, 2024
Best Kanban automation moved this from Review approved to Validate Peach Jun 5, 2024
@pavel-mikula-sonarsource pavel-mikula-sonarsource moved this from Validate Peach to Done in Best Kanban Jun 6, 2024
@Tim-Pohlmann Tim-Pohlmann added this to the 9.27 milestone Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
Best Kanban
  
Done
Development

Successfully merging a pull request may close this issue.

4 participants