Repository navigation
Issue #2801: SA1500 fires for the while clause of do/while statement #3196
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
Changes from 4 commits
bfbb4ca
a965593
3163cdf
036d8e9
c58307c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -235,7 +235,7 @@ private static void CheckBraces(SyntaxNodeAnalysisContext context, SyntaxToken o | |
| CheckBraceToken(context, openBraceToken); | ||
| if (checkCloseBrace) | ||
| { | ||
| CheckBraceToken(context, closeBraceToken); | ||
| CheckBraceToken(context, closeBraceToken, openBraceToken); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -247,7 +247,7 @@ private static bool InitializerExpressionSharesLine(InitializerExpressionSyntax | |
| return (index > 0) && (parent.Expressions[index - 1].GetEndLine() == parent.Expressions[index].GetLine()); | ||
| } | ||
|
|
||
| private static void CheckBraceToken(SyntaxNodeAnalysisContext context, SyntaxToken token) | ||
| private static void CheckBraceToken(SyntaxNodeAnalysisContext context, SyntaxToken token, SyntaxToken openBraceToken = default) | ||
| { | ||
| if (token.IsMissing) | ||
| { | ||
|
|
@@ -284,6 +284,21 @@ private static void CheckBraceToken(SyntaxNodeAnalysisContext context, SyntaxTok | |
| // last token of this file | ||
| return; | ||
|
|
||
| case SyntaxKind.WhileKeyword: | ||
| // Because the default Visual Studio code completion snippet for a do-while loop | ||
| // places the while expression on the same line as the closing brace, some users | ||
| // may want to allow that and not have SA1500 report it as a style error. | ||
| if (context.GetStyleCopSettings(context.CancellationToken).LayoutRules.AllowDoWhileOnClosingBrace) | ||
| { | ||
| if (openBraceToken.Parent.IsKind(SyntaxKind.Block) | ||
| && openBraceToken.Parent.Parent.IsKind(SyntaxKind.DoStatement)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since I don't know the syntax tree well enough, I just want to make sure, as I had similar code and this same question elsewhere, "Is using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| break; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -420,6 +420,7 @@ The following properties are used to configure layout rules in StyleCop Analyzer | |
| | --- | --- | --- | --- | | ||
| | `newlineAtEndOfFile` | `"allow"` | 1.0.0 | Specifies the handling for newline characters which appear at the end of a file | | ||
| | `allowConsecutiveUsings` | `true` | 1.1.0 | Specifies if SA1519 will allow consecutive using statements without braces | | ||
| | `allowDoWhileOnClosingBrace` | `false` | >1.2.0 | Specifies if SA1500 will allow the `while` expression of a `do-while` loop to be on the same line as the closing brace, as is generated by the default code snippet of Visual Studio | | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| ### Lines at End of File | ||
|
|
||
|
|
@@ -441,6 +442,13 @@ The `allowConsecutiveUsings` property specifies the behavior: | |
| This only allows omitting the braces for a using followed by another using statement. A using statement followed by any other type of statement will still | ||
| require braces to used. | ||
|
|
||
| ### Do-While Loop Placement | ||
|
|
||
| The behavior of [SA1500](SA1500.md) can be customized regarding the manner in which the `while` expression of a `do-while` loop is allowed to be placed. The `allowDoWhileOnClosingBrace` property specified the behavior: | ||
|
|
||
| * `true`: the `while` expression of a `do-while` loop may be placed on the same line as the closing brace | ||
| * `false`: the `while` expression of a `do-while` loop must be on a separate line from the closing brace | ||
|
|
||
| ## Documentation Rules | ||
|
|
||
| This section describes the features of documentation rules which can be configured in **stylecop.json**. Each of the described properties are configured in the `documentationRules` object, which is shown in the following sample file. | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.
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.
This line shows only partial code coverage from the Unit Tests and would be the reason for the decrease in Codecov.
The reason is because there isn't a test in which the
braceToken.Parentisnull. I'm not completely familiar with the flow of execution and what is possible and what isn't, so I wrote this just being safe. I suppose, given theSyntaxTree, it may be impossible forbraceToken.Parentto benull, but I just don't know. I tried to write some Unit Tests that had malformed syntax and they would fail with a compiler error.If someone is sure that
braceToken.Parentcan never benull, then I'd be happy to change it. I know the line of code right above this also usesbraceToken.Parentwithout the Null-Conditional operator, but I wasn't sure if the fact that it is applicable to a Multi-Dimensional Array Initializer, that the syntax may be different. So I just wanted to be extra safe.But we can remove it if we want the Codcov percentage to go back up.