roslyn: "When variable type is apparent" doesn't apply to array initialization

This the same bug I filed https://github.com/dotnet/roslyn/issues/23907, but it stated that it was fixed.

Version Used: Version 15.8.0 Preview 1.0 [27628.0.d15.8]

Steps to Reproduce:

image


public class Foo
{
    public void Method()
    {
        var files = new string[0];
    }

}

Expected Behavior:

No message

Actual Behavior:

Message IDE0008 Use explicit type instead of ‘var’ ConsoleApp300 C:\Users\davkean\Source\Repos\ConsoleApp300\ConsoleApp300\Program.cs 6 Active

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 18 (17 by maintainers)

Most upvoted comments

I reviewed https://github.com/dotnet/roslyn/issues/26531#issuecomment-385825621 and https://github.com/dotnet/roslyn/issues/26531#issuecomment-385985344 again today with @mavasani. I believe my original concerns can now be addressed by setting the severity for the diagnostic ID instead of setting it as part of the code style configuration. For example, the “allow when apparent, disallow otherwise” setting could be:

csharp_style_var_for_built_in_types = never
csharp_style_var_elsewhere = when_type_is_apparent
# IDE0007: Use implicit type
dotnet_diagnostic.IDE0007.severity = hidden
# IDE0008: Use explicit type
dotnet_diagnostic.IDE0008.severity = warning

@Neme12 That makes sense, I was grouping the following together when I filed https://github.com/dotnet/roslyn/issues/23714, but I like your idea better.

var i = new int[];
var i = 0;

@sharwell

It covers things like this:

I don’t think so. I would expect 0 to be classified as “not apparent”. var would only be suggested if you type var i = new int()

The current set of options is:

csharp_style_var_for_built_in_types		true/fale
csharp_style_var_when_type_is_apparent	true/false
csharp_style_var_elsewhere				true/false

I believe having these options instead would make sense and only increase the number o possible combinations from 8 to 9 while solving this issue:

csharp_style_var_for_built_in_types		always/never/when_type_is_apparent
csharp_style_var_elsewhere				always/never/when_type_is_apparent