dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.02k stars 4.03k forks source link

A warning is produced when `csharp_style_expression_bodied_* = when_on_single_line` is used #53482

Closed Youssef1313 closed 3 days ago

Youssef1313 commented 3 years ago

Part of my editorconfig:

# Default severity for analyzer diagnostics with category 'Style'
dotnet_analyzer_diagnostic.category-Style.severity = warning

# IDE0022: Use block body for methods
csharp_style_expression_bodied_methods = when_on_single_line

# IDE0021: Use block body for constructors
csharp_style_expression_bodied_constructors = when_on_single_line

The following code generates a warning:

    internal class LocalLinkValidator : ILinkValidator
    {
        private readonly string _baseDirectory;

        // Warning on the following line:
        public LocalLinkValidator(string baseDirectory) => _baseDirectory = baseDirectory;
    }

It's not clear to me why there is a warning? I'm using an expression bodied member when on single line per editorconfig.

AraHaan commented 3 years ago

Part of my editorconfig:

# Default severity for analyzer diagnostics with category 'Style'
dotnet_analyzer_diagnostic.category-Style.severity = warning

# IDE0022: Use block body for methods
csharp_style_expression_bodied_methods = when_on_single_line

# IDE0021: Use block body for constructors
csharp_style_expression_bodied_constructors = when_on_single_line

The following code generates a warning:

    internal class LocalLinkValidator : ILinkValidator
    {
        private readonly string _baseDirectory;

        // Warning on the following line:
        public LocalLinkValidator(string baseDirectory) => _baseDirectory = baseDirectory;
    }

It's not clear to me why there is a warning? I'm using an expression bodied member when on single line per editorconfig.

That is because your editorconfig file configured it to warn when the body is of a single line, did you mean to make it to when it's not a single line?

Youssef1313 commented 3 years ago

@AraHaan I think csharp_style_expression_bodied_methods = when_on_single_line should mean that this is the style I want. Not the style I would like to be warned for.

But adding @CyrusNajmabadi to confirm.

CyrusNajmabadi commented 3 days ago

For better or worse, this is what it means. Having the names of the options/knobs be totally clear is def something we want, but not something we always get.