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 S2094 FP: Implicit parameterless constructor widens the scope of the base class constructor #7591

Closed
rjgotten opened this issue Jul 13, 2023 · 1 comment · Fixed by #9081 or #9115
Assignees
Labels
Area: C# C# rules related issues. Area: VB.NET VB.NET rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@rjgotten
Copy link

rjgotten commented Jul 13, 2023

Description

S2094 regarding an empty class can be raised incorrectly in situations where an implicit parameterless constructor widens the scope modifier of the base class constructor. E.g. where an inherited class publishes a previously protected parameterless constructor.

Repro steps

public class Foo
{
  protected Foo() { }
}

public class Bar : Foo
{
}

Expected behavior

S2094 is not raised where the implicit parameterless constructor widens the scope modifier of the base class parameterless constructor.

Actual behavior

S2094 is raised.

Known workarounds

Explicitly implement the constructor. E.g.

public class Foo
{
  protected Foo() { }
}

public class Bar : Foo
{
  public Bar() : base() { }
}

However, this (correctly!) raises S3253 regarding having defined a redundant constructor.
In effect, the only way to fix this is to suppress S2094.

Related information

  • C#/VB.NET Plugins version : C# 9.4.0.72892 (SonarLint 7.0.0.74072)
  • Visual Studio version: 17.6.5
  • MSBuild / dotnet version: dotnet 7.0.306
  • Operating System: Windows 11
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. labels Jul 17, 2023
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource changed the title Fix S2094 FP: Does not take into account effect of implicit parameterless constructor Fix S2094 FP: Implicit parameterless constructor widens the scope of the base class constructor Jul 17, 2023
@zsolt-kolbay-sonarsource
Copy link
Contributor

Hi @rjgotten. Thank you for reporting this. Confirmed as False Positive.

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. Area: VB.NET VB.NET rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
5 participants