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

when_multiline on csharp_prefer_braces not working #40912

Open
MithrilMan opened this issue Jan 11, 2020 · 7 comments
Open

when_multiline on csharp_prefer_braces not working #40912

MithrilMan opened this issue Jan 11, 2020 · 7 comments
Labels
Area-IDE Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Milestone

Comments

@MithrilMan
Copy link
Contributor

Version Used:
Visual Studio 16.4.2 Enterprise
Steps to Reproduce:
add in .editorconfig the line
csharp_prefer_braces = when_multiline : error

Expected Behavior:
shouldn't report the warning below and should work in the IDE
Actual Behavior:
image
image

@MithrilMan
Copy link
Contributor Author

as a note, if this rule enforce just to put braces for expressions broken in multiple lines, I don't find this rule useful, we'd need a rule like when_not_inline

basically I'd like to use
if (clause) return/throw
and I want CodeAnalysys require braces for

if(clause)
   return/throw

because I find always risky to put blocks without braces, so I only want to use them when I not inline them togheter like in the example.
Is it possible?

anyway the editorconfig is blaming so something doesn't work as expected to me.

@sharwell
Copy link
Member

shouldn't report the warning below

This is a duplicate of madskristensen/EditorConfigLanguage#78.

so I only want to use them when I not inline them togheter like in the example. Is it possible?

This is not currently supported, but you could write a custom analyzer for this. In addition to the support provided by the IDE analyzer, StyleCop Analyzers provides its own implementation of brace requirements. While neither of these supports the specific style you requested, you can use them as an example to create a new analyzer that handles your specific case.

@MithrilMan
Copy link
Contributor Author

thanks for the link.
Do you think it's not a common scenario to have a rule for what I described?
May it be considered something to support officially?

@sharwell
Copy link
Member

I haven't seen another request for the specific combination you were looking for. The when_multiline is already quite close to what you are working on, as long as it's acceptable to not have a warning in one specific case.

@jinujoseph jinujoseph added Area-IDE IDE-CodeStyle Built-in analyzers, fixes, and refactorings Need Design Review The end user experience design needs to be reviewed and approved. labels Jan 14, 2020
@jinujoseph jinujoseph added this to the Backlog milestone Jan 14, 2020
@daniel-liuzzi
Copy link

daniel-liuzzi commented Feb 5, 2020

I second @MithrilMan's opinion. This rule would be far more useful if it also enforced braces on anything that's not inline. The documentation for csharp_prefer_braces states (emphasis mine):

  • true - Prefer curly braces even for one line of code
  • false - Prefer no curly braces if allowed
  • when_multiline - Prefer curly braces on multiple lines

I was surprised to find when_multiline didn't complain about the code below and thought I had hit a bug:

if (true)
    Console.WriteLine("Hello World!");

I assume whether you consider the if above a part of the statement or not (hence whether the code is considered "one line of code" or not) is all semantics, but this style is "risky business" and there should be a rule to prevent it (without enforcing braces everywhere.)

@sharwell
Copy link
Member

sharwell commented Feb 5, 2020

The when_multiline option was developed to support the C# Coding Style used for .NET Core and it's runtime libraries, along with most related open source projects (e.g. dotnet/roslyn). The recommended approach for other specific styles not supported by any configuration of the current rule would be disabling this rule, and instead providing a custom analyzer for the specific desired code style.

@maxild
Copy link

maxild commented Jan 15, 2021

It still seems like you have to use this (soon #43051 (comment)) deprecated syntax to configure the option

csharp_prefer_braces = when_multiline:suggestion
dotnet_diagnostic.IDE0011.severity = warning
# or just "csharp_prefer_braces = when_multiline:warning"...

If using (the preferred syntax)

csharp_prefer_braces = when_multiline
dotnet_diagnostic.IDE0011.severity = warning

The 'Microsoft.CodeAnalysis.CSharp.Diagnostics.AddBraces.CSharpAddBracesDiagnosticAnalyzer throws System.ArgumentNullException with message 'Value cannot be null'.

version : sdk-5.0.102 built-in analyzers

@CyrusNajmabadi CyrusNajmabadi removed the Need Design Review The end user experience design needs to be reviewed and approved. label Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Projects
Status: Complete
Development

No branches or pull requests

6 participants