Skip to content

Commit 5400cf3

Browse files
committed
Update SA1604 to ignore elements that do not require documentation
Fixes #2443
1 parent 20aab72 commit 5400cf3

4 files changed

Lines changed: 75 additions & 17 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1604UnitTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,28 @@ public void Test() { }
207207
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
208208
}
209209

210+
[Fact]
211+
[WorkItem(2443, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2443")]
212+
public async Task TestPrivateMethodWithoutSummaryAsync()
213+
{
214+
var testCode = @"
215+
/// <summary>
216+
///
217+
/// </summary>
218+
internal class ClassName
219+
{
220+
///
221+
private void Test1() { }
222+
223+
/**
224+
*
225+
*/
226+
private void Test2() { }
227+
}";
228+
229+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
230+
}
231+
210232
[Fact]
211233
public async Task TestPartialMethodWithoutDocumentationAsync()
212234
{
@@ -663,10 +685,10 @@ public async Task TestEventPropertyWithoutDocumentationAsync()
663685
public class ClassName
664686
{
665687
///
666-
event System.Action Foo { add { } remove { } }
688+
public event System.Action Foo { add { } remove { } }
667689
}";
668690

669-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(8, 25);
691+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(8, 32);
670692

671693
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
672694
}

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/ElementDocumentationSummaryBase.cs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public override void Initialize(AnalysisContext context)
6363
/// Analyzes the top-level <c>&lt;summary&gt;</c> element of a documentation comment.
6464
/// </summary>
6565
/// <param name="context">The current analysis context.</param>
66+
/// <param name="needsComment"><see langword="true"/> if the current documentation settings indicate that the
67+
/// element should be documented; otherwise, <see langword="false"/>.</param>
6668
/// <param name="documentation">The documentation syntax associated with the element.</param>
6769
/// <param name="syntax">The <see cref="XmlElementSyntax"/> or <see cref="XmlEmptyElementSyntax"/> of the node
6870
/// to examine.</param>
@@ -71,7 +73,7 @@ public override void Initialize(AnalysisContext context)
7173
/// element, this value will be <see langword="null"/>, even if the XML documentation comment also included an
7274
/// <c>&lt;include&gt;</c> element.</param>
7375
/// <param name="diagnosticLocations">The location(s) where diagnostics, if any, should be reported.</param>
74-
protected abstract void HandleXmlElement(SyntaxNodeAnalysisContext context, DocumentationCommentTriviaSyntax documentation, XmlNodeSyntax syntax, XElement completeDocumentation, params Location[] diagnosticLocations);
76+
protected abstract void HandleXmlElement(SyntaxNodeAnalysisContext context, bool needsComment, DocumentationCommentTriviaSyntax documentation, XmlNodeSyntax syntax, XElement completeDocumentation, params Location[] diagnosticLocations);
7577

7678
private void HandleTypeDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
7779
{
@@ -87,7 +89,10 @@ private void HandleTypeDeclaration(SyntaxNodeAnalysisContext context, StyleCopSe
8789
return;
8890
}
8991

90-
this.HandleDeclaration(context, node, node.Identifier.GetLocation());
92+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
93+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
94+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
95+
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
9196
}
9297

9398
private void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
@@ -98,7 +103,10 @@ private void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context, StyleC
98103
return;
99104
}
100105

101-
this.HandleDeclaration(context, node, node.Identifier.GetLocation());
106+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
107+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
108+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
109+
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
102110
}
103111

104112
private void HandleMethodDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
@@ -115,7 +123,10 @@ private void HandleMethodDeclaration(SyntaxNodeAnalysisContext context, StyleCop
115123
return;
116124
}
117125

118-
this.HandleDeclaration(context, node, node.Identifier.GetLocation());
126+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
127+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
128+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
129+
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
119130
}
120131

121132
private void HandleConstructorDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
@@ -126,7 +137,10 @@ private void HandleConstructorDeclaration(SyntaxNodeAnalysisContext context, Sty
126137
return;
127138
}
128139

129-
this.HandleDeclaration(context, node, node.Identifier.GetLocation());
140+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
141+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
142+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
143+
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
130144
}
131145

132146
private void HandleDestructorDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
@@ -137,7 +151,10 @@ private void HandleDestructorDeclaration(SyntaxNodeAnalysisContext context, Styl
137151
return;
138152
}
139153

140-
this.HandleDeclaration(context, node, node.Identifier.GetLocation());
154+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
155+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
156+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
157+
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
141158
}
142159

143160
private void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
@@ -148,7 +165,10 @@ private void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context, StyleC
148165
return;
149166
}
150167

151-
this.HandleDeclaration(context, node, node.Identifier.GetLocation());
168+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
169+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
170+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
171+
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
152172
}
153173

154174
private void HandleIndexerDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
@@ -159,7 +179,10 @@ private void HandleIndexerDeclaration(SyntaxNodeAnalysisContext context, StyleCo
159179
return;
160180
}
161181

162-
this.HandleDeclaration(context, node, node.ThisKeyword.GetLocation());
182+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
183+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
184+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
185+
this.HandleDeclaration(context, needsComment, node, node.ThisKeyword.GetLocation());
163186
}
164187

165188
private void HandleFieldDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
@@ -188,7 +211,10 @@ private void HandleFieldDeclaration(SyntaxNodeAnalysisContext context, StyleCopS
188211
// int i,;
189212
Array.Resize(ref locations, insertionIndex);
190213

191-
this.HandleDeclaration(context, node, locations);
214+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
215+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
216+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
217+
this.HandleDeclaration(context, needsComment, node, locations);
192218
}
193219

194220
private void HandleEventDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
@@ -199,10 +225,13 @@ private void HandleEventDeclaration(SyntaxNodeAnalysisContext context, StyleCopS
199225
return;
200226
}
201227

202-
this.HandleDeclaration(context, node, node.Identifier.GetLocation());
228+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
229+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
230+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
231+
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
203232
}
204233

205-
private void HandleDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings, SyntaxNode node, params Location[] locations)
234+
private void HandleDeclaration(SyntaxNodeAnalysisContext context, bool needsComment, SyntaxNode node, params Location[] locations)
206235
{
207236
var documentation = node.GetDocumentationCommentTriviaSyntax();
208237
if (documentation == null)
@@ -229,7 +258,7 @@ private void HandleDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettin
229258
}
230259
}
231260

232-
this.HandleXmlElement(context, documentation, relevantXmlElement, completeDocumentation, locations);
261+
this.HandleXmlElement(context, needsComment, documentation, relevantXmlElement, completeDocumentation, locations);
233262
}
234263
}
235264
}

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1604ElementDocumentationMustHaveSummary.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ namespace StyleCop.Analyzers.DocumentationRules
66
using System.Collections.Immutable;
77
using System.Linq;
88
using System.Xml.Linq;
9-
using Helpers;
109
using Microsoft.CodeAnalysis;
1110
using Microsoft.CodeAnalysis.CSharp.Syntax;
1211
using Microsoft.CodeAnalysis.Diagnostics;
12+
using StyleCop.Analyzers.Helpers;
13+
using StyleCop.Analyzers.Settings.ObjectModel;
1314

1415
/// <summary>
1516
/// The XML header documentation for a C# element is missing a <c>&lt;summary&gt;</c> tag.
@@ -43,8 +44,14 @@ internal class SA1604ElementDocumentationMustHaveSummary : ElementDocumentationS
4344
ImmutableArray.Create(Descriptor);
4445

4546
/// <inheritdoc/>
46-
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, DocumentationCommentTriviaSyntax documentation, XmlNodeSyntax syntax, XElement completeDocumentation, Location[] diagnosticLocations)
47+
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, bool needsComment, DocumentationCommentTriviaSyntax documentation, XmlNodeSyntax syntax, XElement completeDocumentation, Location[] diagnosticLocations)
4748
{
49+
if (!needsComment)
50+
{
51+
// A missing summary is allowed for this element.
52+
return;
53+
}
54+
4855
if (completeDocumentation != null)
4956
{
5057
// We are working with an <include> element

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1606ElementDocumentationMustHaveSummaryText.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal class SA1606ElementDocumentationMustHaveSummaryText : ElementDocumentat
4545
ImmutableArray.Create(Descriptor);
4646

4747
/// <inheritdoc/>
48-
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, DocumentationCommentTriviaSyntax documentation, XmlNodeSyntax syntax, XElement completeDocumentation, Location[] diagnosticLocations)
48+
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, bool needsComment, DocumentationCommentTriviaSyntax documentation, XmlNodeSyntax syntax, XElement completeDocumentation, Location[] diagnosticLocations)
4949
{
5050
if (syntax == null)
5151
{

0 commit comments

Comments
 (0)