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 S3604 FP: Primary constructors #7624
Comments
Thank you for reporting this issue @hankovich. Confirmed as False Positive. |
With .NET 8.0 right around the corner, this would be nice to get fixed as the primary constructor refactor causes this to appear |
This is an issue for properties too: this code will raises an incorrect S3604 warning:
Thanks. |
.NET 8 is out, and indeed I get public sealed class MyClass(string name, string lastName) : IEquatable<MyClass>
{
public string Name { get; } = name; // S3604
public string LastName{ get; } = lastName; // S3604
// ...
} As a workaround, you could rewrite the properties to return the generated private field dirrectly instead of the backing fields: public sealed class MyClass(string name, string lastName) : IEquatable<MyClass>
{
public string Name { get => name; }
public string LastName{ get; } = lastName; // S3604
// ...
}
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class MyClass
{
[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string <name>P;
[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly string <LastName>k__BackingField;
public string Name
{
get
{
return <name>P;
}
}
public string LastName
{
[CompilerGenerated]
get
{
return <LastName>k__BackingField;
}
}
public MyClass(string name, string lastName)
{
<name>P = name;
<LastName>k__BackingField = lastName;
base..ctor();
}
} So pretty much the same. |
I can confirm this issue. |
Still got this issue, NET 8.0.100, latest visal studio 17.8, latest sonarlint 7.4.0.80741 |
The workaround suppresses the S3604 message, but the compiler-generated backing field is not Thank you for your effort in providing the workaround. However, I still need the Anyhow, I mainly want to add a comment, so this issue may get prioritized. |
You're right, missed that. |
The issue has affected our codebases as we're upgrading to .NET 8 and new C# features as well. +1. |
+1. |
Thanks, we will prioritize it and apologies for missing this in our previous release. |
Those things can happen. No worries. Are you considering to ship it earlier than you normally do? |
@Corniel, it went into the latest release. Soon, we'll deploy to nuget.org, and next week, we'll deploy to SonarCloud. Thanks to all who reported this issue on this thread, and again, my apologies for slipping this through the cracks. |
We're all developers, we all miss things sometimes. No reason to apologize. Keep up the good work! |
Description
S3604 is reported for classes with primary constructors.
Repro steps
Expected behavior
S3064 should not be raised.
Actual behavior
S3064 raises a warning.
Known workarounds
N/A
Related information
The text was updated successfully, but these errors were encountered: