Skip to content

Commit 744e377

Browse files
authored
Merge pull request #2836 from vweijsters/fix-2774
Fixed indentation check issue where opening brace was always considered, even if it was…
2 parents d457c18 + 48bce9d commit 744e377

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1137UnitTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,5 +2015,34 @@ private class TestClass2
20152015

20162016
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
20172017
}
2018+
2019+
[Fact]
2020+
[WorkItem(2774, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2774")]
2021+
public async Task VerifyThatBraceOnSameLineAsOtherCodeAsync()
2022+
{
2023+
var testCode = @"
2024+
public class TestClass
2025+
{
2026+
public void TestMethod()
2027+
{
2028+
var x = TestMethod2(new TestClass2 {
2029+
TestValue = 12,
2030+
});
2031+
}
2032+
2033+
public int TestMethod2(TestClass2 input)
2034+
{
2035+
return 0;
2036+
}
2037+
}
2038+
2039+
public class TestClass2
2040+
{
2041+
public int TestValue { get; set; }
2042+
}
2043+
";
2044+
2045+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
2046+
}
20182047
}
20192048
}

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ private static void CheckBraces(SyntaxNodeAnalysisContext context, SyntaxToken o
480480
return;
481481
}
482482

483+
if (!openBraceToken.IsFirstInLine())
484+
{
485+
// Do not check brace indentation if the opening brace is not the first token on a line.
486+
return;
487+
}
488+
483489
SyntaxTrivia openBraceIndentationTrivia = openBraceToken.LeadingTrivia.LastOrDefault();
484490
string openBraceIndentation = openBraceIndentationTrivia.IsKind(SyntaxKind.WhitespaceTrivia) ? openBraceIndentationTrivia.ToString() : string.Empty;
485491

0 commit comments

Comments
 (0)