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 erroneously suggests changing return type to one of multiple possible return types #6411

Closed
stephentoub opened this issue Jan 6, 2023 · 1 comment

Comments

@stephentoub
Copy link
Member

Analyzer

Diagnostic ID: CA1859

Analyzer source

6ae2683

Describe the bug

A method can return both the base abstract type and a derived type, and the analyzer erroneously suggests changing the return type to the derived type.

Steps To Reproduce

[Fact]
public static async Task Repro()
{
    const string Source = @"
        #nullable enable

        using System;
        using System.IO;

        namespace Example
        {
            internal static class C
            {
                private static Stream GetStream(int i)
                {
                    if (i == 0)
                    {
                        return Stream.Null;
                    }

                    return new MyStream();
                }
            }
        }

        public class MyStream : MemoryStream { }
        ";

    await TestCSAsync(Source);
}

Expected behavior

No warning.

Actual behavior

    // /0/Test0.cs(11,47): info CA1859: Change return type of method 'GetStream' from 'System.IO.Stream' to 'MyStream' for improved performance
VerifyCS.Diagnostic(UseConcreteTypeAnalyzer.CA1859).WithSpan(11, 47, 11, 56).WithArguments("GetStream", "System.IO.Stream", "MyStream"),

cc: @geeknoid, @buyaa-n

@Youssef1313
Copy link
Member

Closing as fixed per #6418 (comment)

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

2 participants