Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,24 @@ private void Bar()
while (x == 0)
{
x = 1;
} while (x == 0)
[|}|] while (x == 0)
{
x = 1;
}
}
}";

var fixedCode = @"public class Foo
{
private void Bar()
{
var x = 0;

while (x == 0)
{
x = 1;
}
while (x == 0)
{
x = 1;
}
Expand All @@ -383,10 +400,7 @@ private void Bar()
var test = new CSharpTest
{
TestCode = testCode,
ExpectedDiagnostics =
{
Diagnostic().WithLocation(10, 9),
},
FixedCode = fixedCode,
Settings = testSettings,
};

Expand Down Expand Up @@ -423,7 +437,7 @@ private void Bar()

do
{
x = 1; } while (x == 0);
x = 1; [|}|] while (x == 0);
}
}";

Expand All @@ -443,10 +457,6 @@ private void Bar()
var test = new CSharpTest
{
TestCode = testCode,
ExpectedDiagnostics =
{
Diagnostic().WithLocation(9, 20),
},
FixedCode = fixedTestCode,
Settings = testSettings,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,10 @@ private static void CheckBraceToken(SyntaxNodeAnalysisContext context, SyntaxTok
// 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.Options.GetStyleCopSettings(context.CancellationToken).LayoutRules.AllowDoWhileOnClosingBrace)
if (context.GetStyleCopSettings(context.CancellationToken).LayoutRules.AllowDoWhileOnClosingBrace)
{
var openBracePreviousToken = openBraceToken.GetPreviousToken(includeZeroWidth: true);

if (openBracePreviousToken.IsKind(SyntaxKind.DoKeyword))
if (openBraceToken.Parent.IsKind(SyntaxKind.Block)
&& openBraceToken.Parent.Parent.IsKind(SyntaxKind.DoStatement))
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 .Parent.Parent safe here?" We know that openBraceToken.Parent is not null and is of kind Block, but does that guarantee that .Parent.Parent will never be null? For example what if I have code like the following that uses some extra curly braces for scoping:

{ // Some braces for scoping
   var x = 0;
   do
   {
      x = 1;
   } while (x == 0);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsKind allows null, and openBraceToken.Parent.IsKind(SyntaxKind.Block) is only true if openBraceToken.Parent is not null.

{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected internal LayoutSettings(JsonObject layoutSettingsObject, AnalyzerConfi
{
OptionSetting? newlineAtEndOfFile = null;
bool? allowConsecutiveUsings = null;
bool? allowDoWhileOnClosingBrace = null;

foreach (var kvp in layoutSettingsObject)
{
Expand All @@ -57,7 +58,7 @@ protected internal LayoutSettings(JsonObject layoutSettingsObject, AnalyzerConfi
break;

case "allowDoWhileOnClosingBrace":
this.allowDoWhileOnClosingBrace = kvp.ToBooleanValue();
allowDoWhileOnClosingBrace = kvp.ToBooleanValue();
break;

default:
Expand All @@ -73,9 +74,11 @@ protected internal LayoutSettings(JsonObject layoutSettingsObject, AnalyzerConfi
};

allowConsecutiveUsings ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.layout.allowConsecutiveUsings");
allowDoWhileOnClosingBrace ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.layout.allowDoWhileOnClosingBrace");

this.newlineAtEndOfFile = newlineAtEndOfFile.GetValueOrDefault(OptionSetting.Allow);
this.allowConsecutiveUsings = allowConsecutiveUsings.GetValueOrDefault(true);
this.allowDoWhileOnClosingBrace = allowDoWhileOnClosingBrace.GetValueOrDefault(false);
}

public OptionSetting NewlineAtEndOfFile =>
Expand Down