-
Notifications
You must be signed in to change notification settings - Fork 470
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
Add analyzer/fixer for informal IDE brace style with statements. #4647
Conversation
Could someone remind me how to run this against Roslyn itself? I want to see if i missed any cases. Thanks! |
Added the cases. There are two differences between SA1513 and my code currently. Specifically:
Note that in both of these cases we simply do not care what the code is. So that leaves us in the state we are in today. IMO, we can decide to be more stringent about the coding here later if we want to. Thoughts @sharwell ? |
@sharwell #4647 (comment) ^ plz :) |
Can see results of this here: dotnet/roslyn#50201 |
@CyrusNajmabadi thanks for detailing the differences. No objections. |
@sharwell i have no idea why this particular test is failing :) |
Specifically:
|
....Diagnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
…LinesBetweenStatementsCodeFixProvider.cs
I am really surprised that dotnet/roslyn#50195 got closed and this analyzer pops-up as a Roslyn specific analyzer. |
### New Rules | ||
Rule ID | Category | Severity | Notes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Evangelink Didn't you fix this before (the empty line before the table)? or I'm mis-remembering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recall doing the fix :)
....Diagnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsCodeFixProvider.cs
Show resolved
Hide resolved
...agnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
Why? As I said in that issue:
This pr follows that opinion. I don't think this should be part of roslyn. It can just be something written externally. This was also something Manish asked if I would do when I did my last set of analyzers :-) |
...agnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...agnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsDiagnosticAnalyzer.cs
Show resolved
Hide resolved
await new Verify.Test() | ||
{ | ||
TestCode = code, | ||
FixedCode = code, | ||
}.RunAsync(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
await Verify.VerifyCodeFixAsync(code, code); is shorter.
I don't want to pollute your PR too much so I am just going to ask one last question. Aren't all rules described here: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/formatting-rules#c-formatting-rules part of the |
I believe the set in roslyn is the core set we want to support (esp. for legacy reasons). I don't want to add new ad-hoc rules like this. :-) A core reason for this is just how ad-hoc this is and how little I want to be beholden to the rules here. As a repo rule, this can be tweaked to our hearts content. As an official rule, our hands are much more tied with what we can do here. |
### New Rules | ||
Rule ID | Category | Severity | Notes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't recall doing the fix :)
public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var document = context.Document; | ||
var diagnostic = context.Diagnostics.First(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have some code-fixes doing a loop over the Diagnostics
and some other doing .First()
what is the right way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be worth doing an anlyzer for this then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this logic is correct. If there were multiple diagnostics for the same token, we'd only want to process one of them anyways. If we looked at all diagnostics, we'd have to dedupe. So this has the same effect.
...agnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
....Diagnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsCodeFixProvider.cs
Show resolved
Hide resolved
...agnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...agnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsDiagnosticAnalyzer.cs
Show resolved
Hide resolved
...agnostics.Analyzers/CSharp/BlankLines/CSharpBlankLinesBetweenStatementsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
int Y { get; } | ||
}"; | ||
|
||
await new Verify.Test() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not test the FixedCode
when we don't expect a diagnostic to be raised. If you go with this change, it applies to some other tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree... i'm trying to doc expected behavior here about what is/isn't controlled by this analyzer. this is very normal IMO for full testing of these types.
…t/roslyn-analyzers into blankLinesAfterCloseBraces
You'll need to run |
Thanks! |
Codecov Report
@@ Coverage Diff @@
## master #4647 +/- ##
=========================================
Coverage 95.77% 95.77%
=========================================
Files 1193 1198 +5
Lines 269067 270078 +1011
Branches 16245 16273 +28
=========================================
+ Hits 257706 258675 +969
- Misses 9278 9282 +4
- Partials 2083 2121 +38 |
Personally, I completely agree. There is nothing Roslyn.sln specific about all these formatting analyzers that we are adding them to Roslyn.Diagnostics.Analyzers, our private package. These are extremely useful rules, which should either ship as part of IDE0055: Fix formatting OR a new IDExxxx diagnostic for experimental formatting rules. Either way - the current approach is causing us to block shipping these useful formatting rules to VS customers, which is very unfortunate. |
Cyrus, this was done as part of a temporary experiment to enable dogfooding these rules on Roslyn.sln. I had presented my pushback on this approach even back then, but agreed contingent on the plan that these analyzers will be deleted in 16.8 and appropriately moved into Roslyn repo as part of existing or new formatting diagnostic ID. This unfortunately did not happen. I have started an email thread around this and proposed we start with deleting these temporary formatting rules from Roslyn.Diagnostics.Analyzers, self-inflict some pain on Roslyn.sln devs and help fast track shipping these as part of VS/CodeStyle NuGet for external customers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marking this as blocked until we finalize the long term ship strategy for these new, useful formatting rules to external customers. I have no pushback on the analyzer/fixer as such.
Sure. I can move back to roslyn. |
Results of running dotnet/roslyn-analyzers#4647
This merged into roslyn itself. |
Adds a new analyzer/fixer for the informal IDE style that we shouldn't bunch of statements too closely. Specifically, if we have a multiline block, and that block is followed by a statement, then there should be a blank line between the block and the statement. In other words, we prefer:
over