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

"IDE0305 Use collection expression for fluent" not raised if ToArray() is assigned to a var variable #73638

Closed
Piedone opened this issue May 15, 2024 · 2 comments
Labels
untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@Piedone
Copy link
Member

Piedone commented May 15, 2024

Analyzer

Diagnostic ID: IDE0305: Use collection expression for fluent

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.CSharp.CodeStyle

Version: 4.9.2

Describe the bug

Steps To Reproduce

  1. Use the following code:
var list = new List<string>
{
    "one",
    "two",
    "three"
};

var array = list.ToArray();

foreach (var item in array)
{
    Console.WriteLine(item);
}
  1. Observe that var array = list.ToArray(); line does NOT violate IDE0305.
  2. Try the code modified as follows:
List<string> list =
[
    "one",
    "two",
    "three",
];

string[] array = list.ToArray();

foreach (string item in array)
{
    Console.WriteLine(item);
}
  1. Observe that string[] array = list.ToArray(); does violate IDE0305.

Expected behavior

Both versions violate IDE0305.

Actual behavior

Only the non-var version violates IDE0305.

Additional context

Full source available here. The offending code is in the root Program.

#73639 might be related.

Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label May 22, 2024
@CyrusNajmabadi
Copy link
Member

This is by design. Collection expressions have no natural type, and so cannot be used with var.

@CyrusNajmabadi CyrusNajmabadi closed this as not planned Won't fix, can't repro, duplicate, stale May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants