Skip to content

Commit 051dd50

Browse files
committed
Merge remote-tracking branch 'DotNetAnalyzers/stabilization' into fixall-performance
2 parents 5fa8d69 + 7a4fcf8 commit 051dd50

31 files changed

Lines changed: 325 additions & 136 deletions

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.ReadabilityRules
66
using System.Collections.Generic;
77
using System.Collections.Immutable;
88
using System.Composition;
9+
using System.Linq;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112
using Helpers;
@@ -88,18 +89,11 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
8889

8990
private static string GetParentIndentation(SyntaxToken token)
9091
{
91-
var parentLine = token.Parent.Parent;
92-
var parentIndentation = string.Empty;
93-
var parentTrivia = parentLine.GetLeadingTrivia();
94-
foreach (var trivia in parentTrivia)
95-
{
96-
if (trivia.IsKind(SyntaxKind.WhitespaceTrivia))
97-
{
98-
parentIndentation += trivia.ToString();
99-
}
100-
}
92+
var parentTrivia = token.Parent.Parent.GetLeadingTrivia();
10193

102-
return parentIndentation;
94+
return parentTrivia
95+
.LastOrDefault(SyntaxKind.WhitespaceTrivia)
96+
.ToString();
10397
}
10498

10599
// This function will remove any unnecessary whitespace or end-of-line trivia from a token.

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ private static List<AttributeListSyntax> GetNewAttributeList(AttributeListSyntax
7777

7878
for (var i = 0; i < attributeList.Attributes.Count; i++)
7979
{
80-
var newAttributes = SyntaxFactory.SingletonSeparatedList(attributeList.Attributes[i]);
80+
var newAttributes = SyntaxFactory.SingletonSeparatedList(
81+
attributeList.Attributes[i].WithLeadingTrivia(
82+
attributeList.Attributes[i].GetLeadingTrivia().WithoutLeadingWhitespace()));
8183
var newAttributeList = SyntaxFactory.AttributeList(attributeList.Target, newAttributes);
8284

8385
newAttributeList = (i == 0)

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/InheritdocCodeFixProviderUnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public override {memberData}
8686

8787
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
8888
await this.VerifyCSharpDiagnosticAsync(fixedCode, expectedFixed, CancellationToken.None).ConfigureAwait(false);
89-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
89+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
9090
}
9191

9292
[Theory]
@@ -151,7 +151,7 @@ public class ChildClass : IParent
151151

152152
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
153153
await this.VerifyCSharpDiagnosticAsync(fixedCode, expectedFixed, CancellationToken.None).ConfigureAwait(false);
154-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
154+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
155155
}
156156

157157
[Theory]

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1606UnitTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,28 @@ public void MethodName()
745745
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
746746
}
747747

748+
[Fact]
749+
public async Task TestIncompleteMemberAsync()
750+
{
751+
var testCode = @"
752+
class Class1
753+
{
754+
/// <include file='ClassWithSummary.xml' path='/Class1/MethodName/*'/>
755+
public string MethodName
756+
}
757+
";
758+
759+
var expected = new DiagnosticResult
760+
{
761+
Id = "CS1002",
762+
Severity = DiagnosticSeverity.Error,
763+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 5, 29) },
764+
Message = "; expected"
765+
};
766+
767+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
768+
}
769+
748770
/// <inheritdoc/>
749771
protected override Project ApplyCompilationOptions(Project project)
750772
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1651UnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ public class ClassName
225225
};
226226

227227
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
228-
229-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
228+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
229+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
230230
}
231231

232232
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()

StyleCop.Analyzers/StyleCop.Analyzers.Test/HelperTests/IndentationHelperTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class IndentationHelperTests
3333
new object[] { " ", 1, 4, 4 },
3434

3535
// 3 spaces, indentation size = 2
36-
new object[] { " ", 1, 4, 4 },
36+
new object[] { " ", 2, 2, 4 },
3737

3838
// 4 spaces
3939
new object[] { " ", 1, 4, 4 },

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1501UnitTests.cs

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ void MethodName()
512512
while (false);
513513
";
514514

515-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
515+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
516516
}
517517

518518
/// <summary>
@@ -717,7 +717,7 @@ public void Bar(int i)
717717

718718
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
719719
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
720-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
720+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 3, cancellationToken: CancellationToken.None).ConfigureAwait(false);
721721
}
722722

723723
/// <summary>
@@ -735,50 +735,6 @@ public async Task TestNoSA1503StatementWithBracesAsync(string statementText)
735735
await this.VerifyCSharpDiagnosticAsync(this.GenerateFixedTestStatement(statementText), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
736736
}
737737

738-
/// <summary>
739-
/// Verifies that an if / else statement followed by a block without braces will produce a warning.
740-
/// </summary>
741-
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
742-
[Fact]
743-
public async Task TestNoSA1503IfElseStatementWithoutBracesAsync()
744-
{
745-
this.suppressSA1503 = true;
746-
747-
var testCode = @"using System.Diagnostics;
748-
public class TypeName
749-
{
750-
public void Bar(int i)
751-
{
752-
if (i == 0) Debug.Assert(true); else Debug.Assert(false);
753-
}
754-
}";
755-
756-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 21);
757-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
758-
}
759-
760-
/// <summary>
761-
/// Verifies that nested if statements followed by a block without braces will produce warnings.
762-
/// </summary>
763-
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
764-
[Fact]
765-
public async Task TestNoSA1503MultipleIfStatementsWithoutBracesAsync()
766-
{
767-
this.suppressSA1503 = true;
768-
769-
var testCode = @"using System.Diagnostics;
770-
public class TypeName
771-
{
772-
public void Bar(int i)
773-
{
774-
if (i == 0) if (i == 0) Debug.Assert(true);
775-
}
776-
}";
777-
778-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 21);
779-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
780-
}
781-
782738
/// <summary>
783739
/// Verifies that the code fix provider will work properly for an if .. else statement.
784740
/// </summary>
@@ -809,7 +765,11 @@ public void Bar(int i)
809765
}
810766
}";
811767

812-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
768+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 21);
769+
770+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
771+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
772+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
813773
}
814774

815775
/// <summary>
@@ -864,18 +824,11 @@ public void Bar(int i)
864824
}
865825
}";
866826

867-
var fixedTestCode = @"using System.Diagnostics;
868-
public class TypeName
869-
{
870-
public void Bar(int i)
871-
{
872-
#pragma warning restore
873-
if (i == 0)
874-
Debug.Assert(true);
875-
}
876-
}";
827+
// The code fix will not make any changes.
828+
var fixedTestCode = testCode;
877829

878-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
830+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
831+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 0, cancellationToken: CancellationToken.None).ConfigureAwait(false);
879832
}
880833

881834
/// <summary>
@@ -907,7 +860,10 @@ public void Bar(int i)
907860
}
908861
}";
909862

910-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
863+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 21);
864+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
865+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
866+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
911867
}
912868

913869
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void Bar()
9090

9191
expected = this.CSharpDiagnostic().WithLocation(3, 66);
9292
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
93-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
93+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
9494
}
9595

9696
[Fact]
@@ -122,7 +122,7 @@ public void Bar()
122122

123123
expected = this.CSharpDiagnostic().WithLocation(4, 34);
124124
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
125-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
125+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
126126
}
127127

128128
[Fact]
@@ -154,7 +154,7 @@ public void Bar()
154154

155155
expected = this.CSharpDiagnostic().WithLocation(4, 32);
156156
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
157-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
157+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
158158
}
159159

160160
[Fact]
@@ -184,7 +184,7 @@ public void Bar()
184184

185185
expected = this.CSharpDiagnostic().WithLocation(3, 66);
186186
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
187-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
187+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
188188
}
189189

190190
[Fact]
@@ -214,7 +214,7 @@ public void Bar()
214214

215215
expected = this.CSharpDiagnostic().WithLocation(3, 66);
216216
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
217-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
217+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
218218
}
219219

220220
[Fact]
@@ -244,7 +244,7 @@ public void Bar()
244244

245245
expected = this.CSharpDiagnostic().WithLocation(3, 66);
246246
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
247-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
247+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
248248
}
249249

250250
[Fact]
@@ -274,7 +274,7 @@ public void Bar()
274274

275275
expected = this.CSharpDiagnostic().WithLocation(3, 66);
276276
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
277-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
277+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
278278
}
279279

280280
[Fact]
@@ -322,7 +322,7 @@ public void Bar()
322322

323323
expected = this.CSharpDiagnostic().WithLocation(4, 66);
324324
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
325-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
325+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
326326
}
327327

328328
[Fact]

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1303UnitTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ public async Task TestFieldWhichIsNotConstStartingWithLowerCaseAsync()
199199
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
200200
}
201201

202+
/// <summary>
203+
/// Regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1936.
204+
/// </summary>
205+
/// <remarks>SA1303 should not be reported on <c>enum</c> declarations. SA1300 will be reported in this case.</remarks>
206+
/// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns>
207+
[Fact]
208+
public async Task TestEnumDeclarationsDoNotReportAsync()
209+
{
210+
var testCode = @"
211+
public enum SpecialFile
212+
{
213+
iTunesMetadata
214+
}";
215+
216+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
217+
218+
}
219+
202220
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
203221
{
204222
yield return new SA1303ConstFieldNamesMustBeginWithUpperCaseLetter();

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1306UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public async Task TestThatDiagnosticIsReported_SingleFieldAsync(string modifiers
183183
}}";
184184

185185
await this.VerifyCSharpDiagnosticAsync(string.Format(fixedCode, modifiers), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
186-
await this.VerifyCSharpFixAsync(string.Format(testCode, modifiers), string.Format(fixedCode, modifiers)).ConfigureAwait(false);
186+
await this.VerifyCSharpFixAsync(string.Format(testCode, modifiers), string.Format(fixedCode, modifiers), numberOfFixAllIterations: 4, cancellationToken: CancellationToken.None).ConfigureAwait(false);
187187
}
188188

189189
[Theory]

0 commit comments

Comments
 (0)