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 not firing in System.Net.Mail source code #7078

Closed
rzikm opened this issue Dec 8, 2023 · 1 comment · Fixed by #7133
Closed

CA1859 not firing in System.Net.Mail source code #7078

rzikm opened this issue Dec 8, 2023 · 1 comment · Fixed by #7133
Assignees
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Negative No diagnostic is reported for a problematic case
Milestone

Comments

@rzikm
Copy link
Member

rzikm commented Dec 8, 2023

Analyzer

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

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 7.0.0-preview1.22559.1

Describe the bug

See dotnet/runtime#95601 (comment)

Steps To Reproduce

public class C
{
    internal void ParseValue(string addresses)
    {
        IList<MailAddress> result = MailAddressParser.ParseMultipleAddresses(addresses); // should be flagged by CA1859

        for (int i = 0; i < result.Count; i++)
        {
            DoSomething(result[i]);
        }
    }
}
internal static class MailAddressParser
{
    internal static List<MailAddress> ParseMultipleAddresses(string data)
    {
        List<MailAddress> results = new List<MailAddress>();
        ... // Parse the string
        return results;
    }
}

Expected behavior

Build fails (Treat warnings as errors is true) because of CA1859 on the following line

https://github.com/dotnet/runtime/blob/d7375b05ff2588884a4d2f4374d41cdbb9f07d27/src/libraries/System.Net.Mail/src/System/Net/Mail/MailAddressCollection.cs#L42

Actual behavior

CA1859 is not detected, build passes.

CC @geeknoid

@geeknoid
Copy link
Member

geeknoid commented Dec 8, 2023

The issue is likely that the analyzer doesn't treat the indexer call as an interface call in the first place (where the affected code is doing result[i] = xxx). CA1859 only fires if the analyzer thinks it would eliminate some virtual or interface calls in the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Negative No diagnostic is reported for a problematic case
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants