dotnet / dotnet/roslyn

`Use explicit type instead of 'var'` emitted on enum value assignment

Open
#75,289 1 comment 0 reactions 0 assignees View on GitHub
Area-IDE
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

### Version Used

Compiler: 4.12.0-3.24470.4 (d799b056)
.NET: 9.0.100-rc.1.24452.12
Visual Studio Code: 1.93.1, `38c31bc77e0dd6ae88a4e9cc93428cc27a56ba40`, x64
C# Extension: v2.49.25
C# Dev Kit Extension: v1.11.11

### Steps to Reproduce

1. Create or open a C# project in Visual Studio Code (Visual Studio untested).
2. Set or add the following properties to an `.editorconfig` file that applies to the project:
```
csharp_style_var_elsewhere = false:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
```
3. Add the following code wherever syntactically applicable:
```cs
var e = E.A;
enum E { A }
```
4. Observe the suggestion `Use explicit type instead of 'var' (IDE0008)` on the `var` keyword.

### Expected Behavior

The suggestion is not emitted, as the type is quite apparent.

### Actual Behavior

The suggestion is emitted.

The suggestion is not emitted for these snippets:
```cs
var e = default(E);
```
```cs
var e = new E();
```

Contributor guide

Open the contributing guide

Research direction

No source files or tests are named. Reproduce the diagnostic in a C# project using the shown .editorconfig settings, then compare the enum assignment with the default(E) and new E() examples; done means IDE0008 is not reported for the enum value assignment while the intended var rules remain intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, vscode
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.