You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@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.
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.
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
Expected behavior
S2743 should not be raised.
Actual behavior
S2743 is raised on the static field.
Known workarounds
None.
Related information
The text was updated successfully, but these errors were encountered: