Skip to content

Commit e936b4f

Browse files
committed
Update SA1615 to suppress messages when documentation for an element is optional
Fixes #2445
1 parent c813702 commit e936b4f

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1615UnitTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,25 @@ public int MethodName()
330330
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
331331
}
332332

333+
[Fact]
334+
[WorkItem(2445, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2445")]
335+
public async Task TestPrivateMethodMissingReturnsAsync()
336+
{
337+
var testCode = @"
338+
internal class ClassName
339+
{
340+
///
341+
private int Test1(int arg) { throw new System.NotImplementedException(); }
342+
343+
/**
344+
*
345+
*/
346+
private int Test2(int arg) { throw new System.NotImplementedException(); }
347+
}";
348+
349+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
350+
}
351+
333352
/// <inheritdoc/>
334353
protected override Project ApplyCompilationOptions(Project project)
335354
{

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1615ElementReturnValueMustBeDocumented.cs

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

1617
/// <summary>
1718
/// A C# element is missing documentation for its return value.
@@ -41,8 +42,8 @@ internal class SA1615ElementReturnValueMustBeDocumented : DiagnosticAnalyzer
4142
private static readonly DiagnosticDescriptor Descriptor =
4243
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.DocumentationRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
4344

44-
private static readonly Action<SyntaxNodeAnalysisContext> MethodDeclarationAction = HandleMethodDeclaration;
45-
private static readonly Action<SyntaxNodeAnalysisContext> DelegateDeclarationAction = HandleDelegateDeclaration;
45+
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> MethodDeclarationAction = HandleMethodDeclaration;
46+
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> DelegateDeclarationAction = HandleDelegateDeclaration;
4647

4748
/// <inheritdoc/>
4849
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
@@ -58,22 +59,34 @@ public override void Initialize(AnalysisContext context)
5859
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
5960
}
6061

61-
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
62+
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
6263
{
6364
var node = (MethodDeclarationSyntax)context.Node;
6465

65-
HandleDeclaration(context, node.ReturnType);
66+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
67+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
68+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
69+
HandleDeclaration(context, needsComment, node.ReturnType);
6670
}
6771

68-
private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context)
72+
private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
6973
{
7074
var node = (DelegateDeclarationSyntax)context.Node;
7175

72-
HandleDeclaration(context, node.ReturnType);
76+
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
77+
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
78+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
79+
HandleDeclaration(context, needsComment, node.ReturnType);
7380
}
7481

75-
private static void HandleDeclaration(SyntaxNodeAnalysisContext context, TypeSyntax returnType)
82+
private static void HandleDeclaration(SyntaxNodeAnalysisContext context, bool needsComment, TypeSyntax returnType)
7683
{
84+
if (!needsComment)
85+
{
86+
// Documentation is optional for this element.
87+
return;
88+
}
89+
7790
var predefinedType = returnType as PredefinedTypeSyntax;
7891

7992
if (predefinedType != null && predefinedType.Keyword.IsKind(SyntaxKind.VoidKeyword))

0 commit comments

Comments
 (0)