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

Enforcing IDE0007 (Use implicit type) as a build warning for only one of three supported var code style options seems difficult #44250

Open
mavasani opened this issue May 14, 2020 · 3 comments
Labels
Area-IDE Bug Feature - IDE0007 Use implicit type IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Milestone

Comments

@mavasani
Copy link
Contributor

mavasani commented May 14, 2020

Consider the below test snippet, with the desired IDE0007 diagnostics:

public class Class1
{
    public static void M()
    {
        Class1 x = new Class1();  // Want "use var" to be enforced as build warning here
        object o = new object();  // Want "use var" to be enforced as build warning here
        Class1 x2 = M2();  // Want "use var" to be an IDE suggestion here
        object x3 = M3();  // Want "use var" to be an IDE suggestion here
    }

    public static Class1 M2() => null;

    public static object M3() => 0;
}

Ideally, the following settings would suffice to get the above behavior:

[*.cs]
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_elsewhere = true:suggestion
csharp_style_var_when_type_is_apparent = true:warning

However, due to #44201, we are forced to add the below entry to editorconfig to get build time enforcement:

[*.cs]
dotnet_diagnostic.IDE0007.severity = warning

This in turn bumps up all the 3 buckets of csharp_style_var violations to warnings, so you end up warnings for all the 4 local declarations above:
image

So, the only alternative to turn off csharp_style_var_elsewhere and csharp_style_var_when_type_is_apparent as follows:

[*.cs]
dotnet_diagnostic.IDE0007.severity = warning
csharp_style_var_for_built_in_types = false:none
csharp_style_var_elsewhere = false:none
csharp_style_var_when_type_is_apparent = true:warning

This gives correct enforcement for the first case, but not second. Assuming the second case is disregarded, you still lose the suggestions (three dots '...') for all but warning cases.
image

We do get a refactoring "use implicit type" for these cases, but they will be bumped down the list if you have any other code fixes in the list, for example:
image

I think the root cause for all these issues is that we use same diagnostic ID IDE0007 for all the csharp_style_var code style options.

@mavasani mavasani added Bug Area-IDE Need Design Review The end user experience design needs to be reviewed and approved. labels May 14, 2020
@mavasani mavasani changed the title Enforcing IDE0007 (Use implicit type) for only one of three supported var options seems difficult Enforcing IDE0007 (Use implicit type) as a build warning for only one of three supported var code style options seems difficult May 14, 2020
@mavasani
Copy link
Contributor Author

@sharwell mentioned this is a duplicate of #26531 (comment).

I am going to repurpose this issue to a pure design/code style issue to finalize the desired build enforcement for Roslyn IDE layers.

@sharwell
Copy link
Member

However, due to #44201, we are forced to add the below entry to editorconfig to get build time enforcement:

This is the problem #27712 aimed to solve.

@jinujoseph jinujoseph added this to the Backlog milestone Jun 20, 2020
@mavasani mavasani added the IDE-CodeStyle Built-in analyzers, fixes, and refactorings label Jul 15, 2020
@CyrusNajmabadi CyrusNajmabadi added the Feature - IDE0007 Use implicit type label Nov 1, 2022
@sharwell
Copy link
Member

I found a workaround for this issue:
dotnet/winforms@e8e42f7

@sharwell sharwell moved this to Need Update in IDE: Design review Aug 22, 2023
@CyrusNajmabadi CyrusNajmabadi removed the Need Design Review The end user experience design needs to be reviewed and approved. label Oct 26, 2024
@CyrusNajmabadi CyrusNajmabadi moved this from Need Update to Complete in IDE: Design review Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Bug Feature - IDE0007 Use implicit type IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Projects
Status: Complete
Development

No branches or pull requests

4 participants