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 recommends nullable return type #6413

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

CA1859 erroneously recommends nullable return type #6413

stephentoub opened this issue Jan 6, 2023 · 1 comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design Category-Performance

Comments

@stephentoub
Copy link
Member

Analyzer

Diagnostic ID: CA1859

Analyzer source

6ae2683

Describe the bug

The analyzer recommends changing a non-nullable return type to a nullable one.

Steps To Reproduce

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

        using System;
        using System.IO;

        namespace Example
        {
            internal class C
            {
                private MemoryStream? _stream;

                private Stream GetStream()
                {
                    return _stream ?? Create();
                }

                private MemoryStream Create() => new MemoryStream();
            }
        }
        ";

    await TestCSAsync(Source);
}

Expected behavior

info CA1859: Change return type of method 'GetStream' from 'System.IO.Stream' to 'System.IO.MemoryStream' for improved performance

Actual behavior

info CA1859: Change return type of method 'GetStream' from 'System.IO.Stream' to 'System.IO.MemoryStream?' for improved performance

cc: @geeknoid, @buyaa-n

@stephentoub stephentoub changed the title CA1859 unnecessarily recommends nullable return type CA1859 erroneously recommends nullable return type Jan 6, 2023
@Youssef1313 Youssef1313 added Bug The product is not behaving according to its current intended design Area-Microsoft.CodeAnalysis.NetAnalyzers Category-Performance labels Jan 6, 2023
@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
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design Category-Performance
Projects
None yet
Development

No branches or pull requests

2 participants