Skip to content

In SDK 8.0.2xx IDE0300 (collection expression) uses loose type matching which should not happen according to documentation  #72532

Open
dotnet/docs
#45790
@ArturDorochowicz

Description

@ArturDorochowicz

Version Used:
SDK 8.0.202

Steps to Reproduce:

dotnet new classlib
dotnet new editorconfig --empty
// .editorconfig
root = true
[*.cs]
dotnet_diagnostic.IDE0300.severity = warning
// Class1.cs
namespace src;

public class Class1
{
	void Method() {
		IEnumerable<int> i = new int[] { 1, 2, 3 };
	}
}
$> dotnet build -p:EnforceCodeStyleInBuild=true
MSBuild version 17.9.6+a4ecab324 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
/src/Class1.cs(6,24): warning IDE0300: Collection initialization can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0300) [/src/src.csproj]
/src/Class1.cs(6,24): warning IDE0300: Collection initialization can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0300) [/src/src.csproj]
  src -> /src/bin/Debug/net8.0/src.dll

Build succeeded.

/src/Class1.cs(6,24): warning IDE0300: Collection initialization can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0300) [/src/src.csproj]
/src/Class1.cs(6,24): warning IDE0300: Collection initialization can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0300) [/src/src.csproj]
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.19

Diagnostic Id:

IDE0300, but others that depend on dotnet_style_prefer_collection_expression probably behave the same.

Expected Behavior:
The analyzer should not trigger. According to documentation at https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0300 loose type matching is behavior from .Net 9 and later. Unless 8.0.202 somehow is not .Net 8, it should default to exact match.

Of course it's also possible that the documentation is wrong.

Actual Behavior:
The analyzer triggers, see build output above.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions