Skip to content

Commit 14644aa

Browse files
committed
Update SA1609 to suppress messages when documentation for an element is optional
Fixes #2451
1 parent 1403715 commit 14644aa

5 files changed

Lines changed: 46 additions & 9 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1609UnitTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,31 @@ public int Property
354354
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
355355
}
356356

357+
[Fact]
358+
[WorkItem(2451, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2451")]
359+
public async Task TestPropertyWithoutDocumentationRequirementAsync()
360+
{
361+
var testCode = @"
362+
/// <summary>
363+
///
364+
/// </summary>
365+
public class ClassName
366+
{
367+
/// <include file='PropertyWithoutValue.xml' path='/ClassName/Property/*'/>
368+
private int Property
369+
{
370+
get;
371+
}
372+
373+
/// <summary>
374+
///
375+
/// </summary>
376+
private ClassName PropertyWithSummary { get; set; }
377+
}";
378+
379+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
380+
}
381+
357382
protected override Project ApplyCompilationOptions(Project project)
358383
{
359384
var resolver = new TestXmlReferenceResolver();

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/PropertyDocumentationBase.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.DocumentationRules
1111
using Microsoft.CodeAnalysis.CSharp.Syntax;
1212
using Microsoft.CodeAnalysis.Diagnostics;
1313
using StyleCop.Analyzers.Helpers;
14+
using StyleCop.Analyzers.Settings.ObjectModel;
1415

1516
/// <summary>
1617
/// This is the base class for analyzers which examine the <c>&lt;value&gt;</c> text of a documentation comment on a property declaration.
@@ -22,7 +23,7 @@ internal abstract class PropertyDocumentationBase : DiagnosticAnalyzer
2223
/// </summary>
2324
internal const string NoCodeFixKey = "NoCodeFix";
2425

25-
private readonly Action<SyntaxNodeAnalysisContext> propertyDeclarationAction;
26+
private readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> propertyDeclarationAction;
2627

2728
protected PropertyDocumentationBase()
2829
{
@@ -48,27 +49,32 @@ public override void Initialize(AnalysisContext context)
4849
/// Analyzes the top-level <c>&lt;summary&gt;</c> element of a documentation comment.
4950
/// </summary>
5051
/// <param name="context">The current analysis context.</param>
52+
/// <param name="needsComment"><see langword="true"/> if the current documentation settings indicate that the
53+
/// element should be documented; otherwise, <see langword="false"/>.</param>
5154
/// <param name="syntax">The <see cref="XmlElementSyntax"/> or <see cref="XmlEmptyElementSyntax"/> of the node
5255
/// to examine.</param>
5356
/// <param name="completeDocumentation">The complete documentation for the declared symbol, with any
5457
/// <c>&lt;include&gt;</c> elements expanded. If the XML documentation comment included a <c>&lt;summary&gt;</c>
5558
/// element, this value will be <see langword="null"/>, even if the XML documentation comment also included an
5659
/// <c>&lt;include&gt;</c> element.</param>
5760
/// <param name="diagnosticLocation">The location where diagnostics, if any, should be reported.</param>
58-
protected abstract void HandleXmlElement(SyntaxNodeAnalysisContext context, XmlNodeSyntax syntax, XElement completeDocumentation, Location diagnosticLocation);
61+
protected abstract void HandleXmlElement(SyntaxNodeAnalysisContext context, bool needsComment, XmlNodeSyntax syntax, XElement completeDocumentation, Location diagnosticLocation);
5962

60-
private void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context)
63+
private void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
6164
{
6265
var node = (PropertyDeclarationSyntax)context.Node;
6366
if (node.Identifier.IsMissing)
6467
{
6568
return;
6669
}
6770

68-
this.HandleDeclaration(context, node, node.Identifier.GetLocation());
71+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
72+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
73+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
74+
this.HandleDeclaration(context, needsComment, node, node.Identifier.GetLocation());
6975
}
7076

71-
private void HandleDeclaration(SyntaxNodeAnalysisContext context, SyntaxNode node, Location location)
77+
private void HandleDeclaration(SyntaxNodeAnalysisContext context, bool needsComment, SyntaxNode node, Location location)
7278
{
7379
var documentation = node.GetDocumentationCommentTriviaSyntax();
7480
if (documentation == null)
@@ -102,7 +108,7 @@ private void HandleDeclaration(SyntaxNodeAnalysisContext context, SyntaxNode nod
102108
}
103109
}
104110

105-
this.HandleXmlElement(context, relevantXmlElement, completeDocumentation, location);
111+
this.HandleXmlElement(context, needsComment, relevantXmlElement, completeDocumentation, location);
106112
}
107113
}
108114
}

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/PropertySummaryDocumentationAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ internal class PropertySummaryDocumentationAnalyzer : PropertyDocumentationBase
5757
protected override string XmlTagToHandle => XmlCommentHelper.SummaryXmlTag;
5858

5959
/// <inheritdoc/>
60-
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, XmlNodeSyntax syntax, XElement completeDocumentation, Location diagnosticLocation)
60+
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, bool needsComment, XmlNodeSyntax syntax, XElement completeDocumentation, Location diagnosticLocation)
6161
{
6262
var propertyDeclaration = (PropertyDeclarationSyntax)context.Node;
6363
var propertyType = context.SemanticModel.GetTypeInfo(propertyDeclaration.Type);

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1609PropertyDocumentationMustHaveValue.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ internal class SA1609PropertyDocumentationMustHaveValue : PropertyDocumentationB
4949
protected override string XmlTagToHandle => XmlCommentHelper.ValueXmlTag;
5050

5151
/// <inheritdoc/>
52-
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, XmlNodeSyntax syntax, XElement completeDocumentation, Location diagnosticLocation)
52+
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, bool needsComment, XmlNodeSyntax syntax, XElement completeDocumentation, Location diagnosticLocation)
5353
{
54+
if (!needsComment)
55+
{
56+
// A missing 'value' documentation is allowed for this element.
57+
return;
58+
}
59+
5460
var properties = ImmutableDictionary.Create<string, string>();
5561

5662
if (completeDocumentation != null)

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1610PropertyDocumentationMustHaveValueText.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal class SA1610PropertyDocumentationMustHaveValueText : PropertyDocumentat
4949
protected override string XmlTagToHandle => XmlCommentHelper.ValueXmlTag;
5050

5151
/// <inheritdoc/>
52-
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, XmlNodeSyntax syntax, XElement completeDocumentation, Location diagnosticLocation)
52+
protected override void HandleXmlElement(SyntaxNodeAnalysisContext context, bool needsComment, XmlNodeSyntax syntax, XElement completeDocumentation, Location diagnosticLocation)
5353
{
5454
var properties = ImmutableDictionary.Create<string, string>();
5555

0 commit comments

Comments
 (0)