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

CA1801 false positive on record with property-targeting attribute on parameter #4397

Closed
huoyaoyuan opened this issue Nov 1, 2020 · 9 comments · Fixed by #4499
Closed

CA1801 false positive on record with property-targeting attribute on parameter #4397

huoyaoyuan opened this issue Nov 1, 2020 · 9 comments · Fixed by #4499
Labels
False_Positive A diagnostic is reported for non-problematic case

Comments

@huoyaoyuan
Copy link
Member

Analyzer

Diagnostic ID: CA1801: Review unused parameters

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 5.0.100-rc.2

Describe the bug

    public sealed record AuthProfileImageUrls(
        [property: JsonPropertyName("px_16x16")]
        Uri PixelSize16,
        [property: JsonPropertyName("px_50x50")]
        Uri PixelSize50,
        [property: JsonPropertyName("px_170x170")]
        Uri PixelSize170);

If any of the parameters have a [property: attribute, then CA1801 will fire on all parameters.

Additional context

I wonder the relationship between CA1801 and IDE0060. IDE0060 used to fire a false positive on this, but get fixed later. Once IDExxxx analyzers are available in CLI builds, I will consider to use IDE0060 to replace CA1801.

@mavasani
Copy link
Contributor

mavasani commented Nov 2, 2020

Once IDExxxx analyzers are available in CLI builds, I will consider to use IDE0060 to replace CA1801.

They are available in CLI. Please see https://docs.microsoft.com/dotnet/fundamentals/code-analysis/overview#code-style-analysis

@billybraga
Copy link

How can we be alerted when this is released ? It's included in roslyn-analyzers 5.0.3, but .NET SDK 5.0.201 (most recent) ships with version 5.0.2 of roslyn-analyzers.

@jmarolf
Copy link
Contributor

jmarolf commented Mar 11, 2021

this is not going to be ported to the 5.0.2xx SDKs but will appear in 5.0.3xx

@billybraga
Copy link

I'm still having this issue in 5.0.203

@Sharparam
Copy link

Having this issue on 5.0.400, which is strange considering it was going to be included in 5.0.3xx?

@dlouwers
Copy link

dlouwers commented Sep 14, 2021

Also having this on records with default parameters, 5.0.400

@dlouwers
Copy link

Workaround:
public record MyRecord(string P1 = null, string P2 = null);

to

public record MyRecord
{
  public string P1 { get; init; }
  public string P2 { get; init; }

  public MyRecord(string p1 = null, string p2 = null)
  {
    P1 = p1;
    P2 = p2;
  }
}

Very verbose and hope this false positive will be fixed so think it is better to suppress for now.

@AlbertoMonteiro
Copy link

AlbertoMonteiro commented Jan 21, 2022

@dlouwers I think that using that way is much simplier

#pragma warning disable CA1801
public sealed record AuthProfileImageUrls(
    [property: JsonPropertyName("px_16x16")] Uri PixelSize16,
    [property: JsonPropertyName("px_50x50")] Uri PixelSize50,
    [property: JsonPropertyName("px_170x170")] Uri PixelSize170);

@chibanemourad
Copy link

I have the same problem (CA1801 false positive) with sdk version 5.0.402 :

public abstract record ATest(int Id, string Name);
public record ConcreteTest(int Id, string Name, State state) : ATest(Id, Name);

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False_Positive A diagnostic is reported for non-problematic case
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants