Skip to content

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

@zsolt-kolbay-sonarsource

Description

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)

Metadata

Metadata

Labels

False PositiveRule IS triggered when it shouldn't be.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions