Skip to content

Commit ac00d09

Browse files
authored
Merge pull request #3836 from bjornhellander/feature/sa1004-param-modifier-3817
Update SA1004 to allow doc comments to start with the in, out and ref keywords
2 parents 5bdaf0a + 2a93406 commit ac00d09

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp12/SpacingRules/SA1004CSharp12UnitTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,35 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp12.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp11.SpacingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.SpacingRules.SA1004DocumentationLinesMustBeginWithSingleSpace,
13+
StyleCop.Analyzers.SpacingRules.SA1004CodeFixProvider>;
714

815
public partial class SA1004CSharp12UnitTests : SA1004CSharp11UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3817, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3817")]
19+
public async Task TestParameterModifierReadOnlyFirstOnLineAsync()
20+
{
21+
string testCode = $@"
22+
/// <summary>
23+
/// Description of some remarks that refer to a method: <see cref=""SomeMethod(int, int, ref
24+
/// readonly string)""/>.
25+
/// </summary>
26+
public class TypeName
27+
{{
28+
public void SomeMethod(int x, int y, ref readonly string z)
29+
{{
30+
throw new System.Exception();
31+
}}
32+
}}";
33+
34+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
35+
}
1036
}
1137
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1004UnitTests.cs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.SpacingRules
75
{
6+
using System.Collections.Generic;
87
using System.Threading;
98
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis.CSharp;
1010
using Microsoft.CodeAnalysis.Testing;
11+
using StyleCop.Analyzers.Lightup;
1112
using StyleCop.Analyzers.SpacingRules;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
@@ -19,6 +20,20 @@ namespace StyleCop.Analyzers.Test.SpacingRules
1920
/// </summary>
2021
public class SA1004UnitTests
2122
{
23+
public static IEnumerable<object[]> ParameterModifiers
24+
{
25+
get
26+
{
27+
yield return new[] { "out" };
28+
yield return new[] { "ref" };
29+
30+
if (LightupHelpers.SupportsCSharp72)
31+
{
32+
yield return new[] { "in" };
33+
}
34+
}
35+
}
36+
2237
[Fact]
2338
public async Task TestFixedExampleAsync()
2439
{
@@ -213,5 +228,33 @@ private void Method1(int x, int y)
213228

214229
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
215230
}
231+
232+
[Theory]
233+
[MemberData(nameof(ParameterModifiers))]
234+
[WorkItem(3817, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3817")]
235+
public async Task TestParameterModifierFirstOnLineAsync(string keyword)
236+
{
237+
string testCode = $@"
238+
/// <summary>
239+
/// Description of some remarks that refer to a method: <see cref=""SomeMethod(int, int,
240+
/// {keyword} string)""/>.
241+
/// </summary>
242+
public class TypeName
243+
{{
244+
public void SomeMethod(int x, int y, {keyword} string z)
245+
{{
246+
throw new System.Exception();
247+
}}
248+
}}";
249+
250+
var languageVersion = (LightupHelpers.SupportsCSharp8, LightupHelpers.SupportsCSharp72) switch
251+
{
252+
// Make sure to use C# 7.2 if supported, unless we are going to default to something greater
253+
(false, true) => LanguageVersionEx.CSharp7_2,
254+
_ => (LanguageVersion?)null,
255+
};
256+
257+
await VerifyCSharpDiagnosticAsync(languageVersion, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
258+
}
216259
}
217260
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1004DocumentationLinesMustBeginWithSingleSpace.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.SpacingRules
75
{
86
using System;
@@ -112,6 +110,10 @@ private static void HandleDocumentationCommentExteriorTrivia(SyntaxTreeAnalysisC
112110
case SyntaxKind.XmlCommentEndToken:
113111
case SyntaxKind.XmlCDataStartToken:
114112
case SyntaxKind.XmlCDataEndToken:
113+
case SyntaxKind.InKeyword:
114+
case SyntaxKind.OutKeyword:
115+
case SyntaxKind.RefKeyword:
116+
case SyntaxKind.ReadOnlyKeyword:
115117
if (!token.HasLeadingTrivia)
116118
{
117119
break;

0 commit comments

Comments
 (0)