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

CA1861 makes a breaking suggestion for attribute arguments #7205

Open
Kaoticz opened this issue Feb 20, 2024 · 9 comments
Open

CA1861 makes a breaking suggestion for attribute arguments #7205

Kaoticz opened this issue Feb 20, 2024 · 9 comments

Comments

@Kaoticz
Copy link

Kaoticz commented Feb 20, 2024

Analyzer

Diagnostic ID: CA1861: Avoid constant arrays as arguments

Analyzer source

Version: SDK 8.0.101

Describe the bug

CA1861 triggers when an array is initialized as an argument to an attribute. This shouldn't happen, as attributes require constant arguments. Therefore, the CA1861 suggestion of initializing a static array is a breaking suggestion, leading to code that does not compile.

Found it while writing an xUnit test:

[Theory]
[InlineData(new int[] { })]	// CA1861 triggers here. It should not.
[InlineData(new int[] { 0, 1, 2, 3 })] // CA1861 triggers here. It should not.
internal void SomeTest(IReadOnlyList<int> collection)
{
    // test code...
}

If you apply the suggested correction, you get CS0182 (An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type), which is a compilation error.

This issue seems to be related to #7033

Steps To Reproduce

Initialize an array for an attribute argument.

[SomeAttribute(new object[] { })]

Expected behavior

No suggestion, warning, or error should be made.

Actual behavior

A CA1861 suggestion is made.

Additional context

VS Code + C# Dev Kit 1.3.10
.NET 8.0.101

@CollinAlpert
Copy link
Contributor

Since this issue is already fixed, I suggest you install the Microsoft.CodeAnalysis.NetAnalyzers package with version 9.0.0-preview.24072.1

@cw397
Copy link

cw397 commented Feb 29, 2024

I can confirm it is fixed for us in that preview package. From the dates it looks like the fix just missed the cut for version 8. Does this mean the fix won't be in the SDK until .NET 9? Is there any likelihood of a patch release for version 8? There were several for version 7.

@CollinAlpert
Copy link
Contributor

I'm not sure if analyzers are included in the patch releases of an SDK. Someone from the team would need to comment on that.

@cw397
Copy link

cw397 commented Feb 29, 2024

Even a patch release just of the analyzers might be helpful. It was that I was thinking of more when I said about the ones for version 7.

@CollinAlpert
Copy link
Contributor

@steveberdy
Copy link
Contributor

Can this be closed now?

@Morilli
Copy link

Morilli commented Dec 4, 2024

I just ran into this same issue. Using the nuget package Microsoft.CodeAnalysis.NetAnalyzers 9.0.0 fixes it, but building with the .NET 9 SDK without that package still shows the CA1861 warning, even though the .NET 9 SDK has a newer version, so something is still wrong.

@steveberdy
Copy link
Contributor

@Morilli which precise version 9.0 did you use before .NET 9? A false-positive fix may have come after the version shipped with .NET 9.

@Morilli
Copy link

Morilli commented Dec 6, 2024

Testing this again, I cannot reproduce this inspection again with any version of the .NET 9 SDK, just the .NET 8 one. So it looks like some inspection results may have been cached incorrectly or I had forgotten to switch back to the .NET 9 SDK while testing.

All versions of the .NET 9 SDK look fine for me now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants