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 with arrays #6931

Closed
JustArchi opened this issue Sep 13, 2023 · 2 comments
Closed

CA1859 false positive with arrays #6931

JustArchi opened this issue Sep 13, 2023 · 2 comments
Labels
False_Positive A diagnostic is reported for non-problematic case

Comments

@JustArchi
Copy link
Contributor

JustArchi commented Sep 13, 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-rc1

Describe the bug

False positive of CA1859 where I don't expect one.

Steps To Reproduce

I got false positive with following code:

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

		IList<T> results;

		switch (ASF.GlobalConfig?.OptimizationMode) {
			case GlobalConfig.EOptimizationMode.MinMemoryUsage:
				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;
	}

Rewritten for easier reproduction to the code that compiles:

	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;
	}

Expected behavior

No CA1859 in above code.

Actual behavior

Here roslyn analyzer suggests me to change IList<T> results into List<T>, but doesn't take into account that Task.WhenAll() returns T[] which is not a List<T>.

Additional context

I reproduced this with clean .NET 8 rc1 which was released yesterday so I believe this issue is recent - please feel free to reproduce and see if it applies.

@sharwell sharwell added the False_Positive A diagnostic is reported for non-problematic case label Sep 13, 2023
@pcf0
Copy link

pcf0 commented Sep 13, 2023

I think this is the same problem as in my case? #6904 (value is changed by await)

@JustArchi
Copy link
Contributor Author

Looks the same, yeah! At least we have confirmation that it still happens on rc1 then.

@JustArchi JustArchi closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False_Positive A diagnostic is reported for non-problematic case
Projects
None yet
Development

No branches or pull requests

3 participants