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: false positive if the value is changed by await #6904

Closed
pcf0 opened this issue Sep 2, 2023 · 1 comment · Fixed by #7133
Closed

CA1859: false positive if the value is changed by await #6904

pcf0 opened this issue Sep 2, 2023 · 1 comment · Fixed by #7133
Assignees
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design False_Positive A diagnostic is reported for non-problematic case
Milestone

Comments

@pcf0
Copy link

pcf0 commented Sep 2, 2023

Analyzer

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

Analyzer source

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

Version: SDK 8.0.100

OR

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 8.0.0 (Latest 2023-11-17)

Describe the bug

A property or field with an interface type is initialized with a concrete implementation. In an async method another implementation is assigned using await. CA1859 is generated.

Steps To Reproduce

public class Class1
{
    private I Prop { get; set; } = new Impl1(); //<-- CA1859 

    public async Task Init()
    {
        Prop = await Task.FromResult<I>(new Impl2());
        Prop.M();
    }
}

internal interface I
{
    void M();
}

internal class Impl1 : I
{
    public void M() { }
}

internal class Impl2 : I
{
    public void M() { }
}

Expected behavior

No errors reported

Actual behavior

Error reported:
Class1.cs(3,15): error CA1859: Change type of property 'Prop' from 'I' to 'Impl1' for improved performance (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859)

Additional context

@JustArchi
Copy link
Contributor

Another repro for this on rc1:

	public static async Task<IList<T>> InParallel2<T>(IEnumerable<Task<T>> tasks) {
		ArgumentNullException.ThrowIfNull(tasks);

		IList<T> results;

		switch (DateTime.Now.Year) {
			case 2023:
				results = new List<T>();

				foreach (Task<T> task in tasks) {
					results.Add(await task.ConfigureAwait(false));
				}

				break;
			default:
				results = await Task.WhenAll(tasks).ConfigureAwait(false);

				break;
		}

		return results;
	}

@mavasani mavasani added Bug The product is not behaving according to its current intended design Area-Microsoft.CodeQuality.Analyzers False_Positive A diagnostic is reported for non-problematic case labels Oct 11, 2023
@mavasani mavasani added this to the .NET vNext milestone Oct 11, 2023
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 False_Positive A diagnostic is reported for non-problematic case
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants