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 S3459 FP: support classes marked with [AutoConstructor] attribute #9674

Open
nycdotnet opened this issue Sep 30, 2024 · 3 comments
Open
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.

Comments

@nycdotnet
Copy link

nycdotnet commented Sep 30, 2024

Description

We use the NuGet package Autoconstructor in many of our solutions. https://www.nuget.org/packages/AutoConstructor With this package, if a class is marked partial and has the [AutoConstructor] attribute, this NuGet package will generate a constructor that accepts an argument for and will assign a value to any readonly non-static fields (with various customizations possible).

In Visual Studio, the Sonar NuGet analyzer is able to interpret the generated constructor, but not in CI where we are wiring in sonarcloud.io. This means we will get S3459 wherever there are fields that are only assigned by the automatically generated constructor. (We don't check in the generated code).

Repro steps

namespace MyNameSpace;

[AutoConstructor]
public partial class SonarFalsePositive
{
    private TimeProvider TimeProvider { get; }

    public DateTimeOffset DoSomething()
    {
        return TimeProvider.GetUtcNow();
    }
}

Expected behavior

No S3459 just like in Visual Studio.

Actual behavior

S3459 is raised in SonarCloud.io: Remove unassigned auto-property 'TimeProvider', or set its value.

Known workarounds

We could disable S3459 in classes that use Autoconstructor or generally.
I wonder if this rule could be disabled maybe for partial classes if it's expected that your CI product can't use the generated code? Possibly only for partial classes that also have the Autoconstructor attribute? I see you already have an override for classes with [Serializable].

Related information

  • Visual Studio 2022 17.11.4
  • .NET 8
  • SonarScanner NuGet 9.10.0.77988 (in Visual Studio) which doesn't have the false positive. The false positive comes from sonar cloud in our CI.
  • Windows 10 latest
@CristianAmbrosini CristianAmbrosini changed the title Fix S3459 FP: False positive with partial class and code generated constructor Fix S3459 FP: support classes marked with [AutoConstructor] attribute Oct 2, 2024
@CristianAmbrosini
Copy link
Contributor

Hi @nycdotnet,

while it is a bit niche, it does appear to be a false positive.
As you pointed out, we are already opting out if specific attributes are found in the class.
Let's see if I can add a reproducer and fix it; it should be a low-hanging fruit.

@CristianAmbrosini CristianAmbrosini added Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be. labels Oct 2, 2024
@CristianAmbrosini
Copy link
Contributor

I tried adding a reproducer but it's not an easy task. The AutoConstructorAttribute is auto-generated and this adds some complexity to our test infrastructure. I'll leave this open for the future, we might add a syntax-level check and avoid raising on classes marked with attributes that are called "AutoConstructor".

@nycdotnet
Copy link
Author

OK thanks.

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
None yet
Development

No branches or pull requests

2 participants