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

CA1859 fails to show in IDE in specific scenarios #7496

Open
Morilli opened this issue Dec 3, 2024 · 1 comment
Open

CA1859 fails to show in IDE in specific scenarios #7496

Morilli opened this issue Dec 3, 2024 · 1 comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design

Comments

@Morilli
Copy link

Morilli commented Dec 3, 2024

Analyzer

Diagnostic ID: CA1859: Use concrete types when possible for improved performance

Analyzer source

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

Version: SDK 9.0.101

OR

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 9.0.0

Describe the bug

The CA1859 inspection fails to get shown in the IDE when certain criteria are met. The inspection will still be shown on build if set to warning or error level.

Steps To Reproduce

Make sure CA1859 is enabled and will show in the IDE, e.g. by setting dotnet_diagnostic.CA1859.severity = warning in the .globalconfig file.
Create two files.

File 1:

using System;
using System.Collections.Generic;

namespace ExampleNamespace;

public partial class ExampleClass
{
    private readonly IList<string> exampleList = new List<string>();

    private void DoStuff()
    {
        Console.WriteLine(ABC);
        Console.WriteLine(DEF);
        exampleList.Add("item");
        Console.WriteLine(exampleList[0]);
    }
}

public partial class ExampleClass
{
    public const int DEF = 2;
}

File 2:

namespace ExampleNamespace;

public partial class ExampleClass
{
    public const int ABC = 1;
}

Expected behavior

The variable exampleList should get annotated, suggesting me to use the List<string> type instead of IList<string>.

Actual behavior

There is no CA1859 annotation and the warning will only be shown when building. This also means that having the inspection on suggestion level will silence it completely.

Additional context

Referencing a variable from another definition of a partial class seems to break the CA1859 inspection, even if the variables are not related in any way. It seems to also be required for the variable to be in an actual different file, not just another part of the class in the same file. In the example above, the inspection can be easily toggled by commenting the Console.WriteLine(ABC); line.

@Youssef1313
Copy link
Member

This is a compilation end analyzer and is missing the CompilationEnd tag, which could be the root cause of it not showing in IDE

@Youssef1313 Youssef1313 added Bug The product is not behaving according to its current intended design Area-Microsoft.CodeAnalysis.NetAnalyzers labels Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design
Projects
None yet
Development

No branches or pull requests

2 participants