Skip to content

Commit 2f67c9c

Browse files
committed
Merge pull request #1726 from vweijsters/fix-1678
Added support to SA1611 for operators and conversion operators
2 parents fbc39b3 + 7fa68a6 commit 2f67c9c

4 files changed

Lines changed: 125 additions & 6 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1611UnitTests.cs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public class ClassName
142142
/// </summary>
143143
public ##
144144
}";
145-
var expected = new[]
145+
DiagnosticResult[] expected =
146146
{
147147
this.CSharpDiagnostic().WithLocation(10, 38).WithArguments("param1"),
148148
this.CSharpDiagnostic().WithLocation(10, 53).WithArguments("param2"),
@@ -152,6 +152,83 @@ public class ClassName
152152
await this.VerifyCSharpDiagnosticAsync(testCode.Replace("##", p), expected, CancellationToken.None).ConfigureAwait(false);
153153
}
154154

155+
/// <summary>
156+
/// Verifies that valid operator declarations will not produce diagnostics.
157+
/// </summary>
158+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
159+
[Fact]
160+
public async Task VerifyValidOperatorDeclarationsAsync()
161+
{
162+
var testCode = @"
163+
/// <summary>
164+
/// Test class
165+
/// </summary>
166+
public class TestClass
167+
{
168+
/// <summary>
169+
/// Foo
170+
/// </summary>
171+
/// <param name=""value"">The value to use.</param>
172+
public static TestClass operator +(TestClass value)
173+
{
174+
return value;
175+
}
176+
177+
/// <summary>
178+
/// Foo
179+
/// </summary>
180+
/// <param name=""value"">The value to use.</param>
181+
public static explicit operator TestClass(int value)
182+
{
183+
return new TestClass();
184+
}
185+
}
186+
";
187+
188+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
189+
}
190+
191+
/// <summary>
192+
/// Verifies that invalid operator declarations will produce the expected diagnostics.
193+
/// </summary>
194+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
195+
[Fact]
196+
public async Task VerifyInvalidOperatorDeclarationsAsync()
197+
{
198+
var testCode = @"
199+
/// <summary>
200+
/// Test class
201+
/// </summary>
202+
public class TestClass
203+
{
204+
/// <summary>
205+
/// Foo
206+
/// </summary>
207+
public static TestClass operator +(TestClass value)
208+
{
209+
return value;
210+
}
211+
212+
/// <summary>
213+
/// Foo
214+
/// </summary>
215+
public static explicit operator TestClass(int value)
216+
{
217+
return new TestClass();
218+
}
219+
}
220+
";
221+
222+
DiagnosticResult[] expected =
223+
{
224+
this.CSharpDiagnostic().WithLocation(10, 50).WithArguments("value"),
225+
this.CSharpDiagnostic().WithLocation(18, 51).WithArguments("value")
226+
};
227+
228+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
229+
}
230+
231+
/// <inheritdoc/>
155232
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
156233
{
157234
yield return new SA1611ElementParametersMustBeDocumented();

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/DocumentationResources.Designer.cs

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/DocumentationResources.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@
123123
<data name="SA1609SA1610CodeFix" xml:space="preserve">
124124
<value>Document value from summary</value>
125125
</data>
126+
<data name="SA1611Description" xml:space="preserve">
127+
<value>A C# method, constructor, delegate or indexer element is missing documentation for one or more of its parameters.</value>
128+
</data>
129+
<data name="SA1611MessageFormat" xml:space="preserve">
130+
<value>The documentation for parameter '{0}' is missing</value>
131+
</data>
132+
<data name="SA1611Title" xml:space="preserve">
133+
<value>Element parameters must be documented</value>
134+
</data>
126135
<data name="SA1615SA1616CodeFix" xml:space="preserve">
127136
<value>Document return value</value>
128137
</data>

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1611ElementParametersMustBeDocumented.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,22 @@ internal class SA1611ElementParametersMustBeDocumented : DiagnosticAnalyzer
3434
/// The ID for diagnostics produced by the <see cref="SA1611ElementParametersMustBeDocumented"/> analyzer.
3535
/// </summary>
3636
public const string DiagnosticId = "SA1611";
37-
private const string Title = "Element parameters must be documented";
38-
private const string MessageFormat = "The documentation for parameter '{0}' is missing";
39-
private const string Description = "A C# method, constructor, delegate or indexer element is missing documentation for one or more of its parameters.";
40-
private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1611.md";
37+
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(DocumentationResources.SA1611Title), DocumentationResources.ResourceManager, typeof(DocumentationResources));
38+
private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(DocumentationResources.SA1611MessageFormat), DocumentationResources.ResourceManager, typeof(DocumentationResources));
39+
private static readonly LocalizableString Description = new LocalizableResourceString(nameof(DocumentationResources.SA1611Description), DocumentationResources.ResourceManager, typeof(DocumentationResources));
40+
private static readonly string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1611.md";
4141

4242
private static readonly DiagnosticDescriptor Descriptor =
4343
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.DocumentationRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
4444

4545
private static readonly ImmutableArray<SyntaxKind> HandledSyntaxKinds =
46-
ImmutableArray.Create(SyntaxKind.MethodDeclaration, SyntaxKind.ConstructorDeclaration, SyntaxKind.DelegateDeclaration, SyntaxKind.IndexerDeclaration);
46+
ImmutableArray.Create(
47+
SyntaxKind.MethodDeclaration,
48+
SyntaxKind.ConstructorDeclaration,
49+
SyntaxKind.DelegateDeclaration,
50+
SyntaxKind.IndexerDeclaration,
51+
SyntaxKind.OperatorDeclaration,
52+
SyntaxKind.ConversionOperatorDeclaration);
4753

4854
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
4955
private static readonly Action<SyntaxNodeAnalysisContext> SyntaxNodeAction = HandleSyntaxNode;

0 commit comments

Comments
 (0)