-
Notifications
You must be signed in to change notification settings - Fork 512
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 all 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)) | ||
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
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; | ||
} | ||
|
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.Parent
isnull
. 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.Parent
to 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.Parent
can never benull
, then I'd be happy to change it. I know the line of code right above this also usesbraceToken.Parent
without 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.