Skip to content

Commit e0b4ec1

Browse files
committed
Update SA1618 to suppress messages when documentation for an element is optional
Fixes #2446
1 parent e936b4f commit e0b4ec1

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1618UnitTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,25 @@ public void TestMethod<T>(T param1) { }
395395
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
396396
}
397397

398+
[Fact]
399+
[WorkItem(2446, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2446")]
400+
public async Task TestPrivateMethodMissingGenericParametersAsync()
401+
{
402+
var testCode = @"
403+
internal class ClassName
404+
{
405+
///
406+
private void Test1<T>(int arg) { }
407+
408+
/**
409+
*
410+
*/
411+
private void Test2<T>(int arg) { }
412+
}";
413+
414+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
415+
}
416+
398417
protected override Project ApplyCompilationOptions(Project project)
399418
{
400419
var resolver = new TestXmlReferenceResolver();

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1618GenericTypeParametersMustBeDocumented.cs

Lines changed: 26 additions & 10 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 generic C# element is missing documentation for one or more of its generic type parameters.
@@ -41,9 +42,9 @@ internal class SA1618GenericTypeParametersMustBeDocumented : 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> TypeDeclarationAction = HandleTypeDeclaration;
45-
private static readonly Action<SyntaxNodeAnalysisContext> MethodDeclarationAction = HandleMethodDeclaration;
46-
private static readonly Action<SyntaxNodeAnalysisContext> DelegateDeclarationAction = HandleDelegateDeclaration;
45+
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> TypeDeclarationAction = HandleTypeDeclaration;
46+
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> MethodDeclarationAction = HandleMethodDeclaration;
47+
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> DelegateDeclarationAction = HandleDelegateDeclaration;
4748

4849
/// <inheritdoc/>
4950
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
@@ -60,7 +61,7 @@ public override void Initialize(AnalysisContext context)
6061
context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration);
6162
}
6263

63-
private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)
64+
private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
6465
{
6566
TypeDeclarationSyntax typeDeclaration = (TypeDeclarationSyntax)context.Node;
6667

@@ -70,25 +71,40 @@ private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)
7071
return;
7172
}
7273

73-
HandleMemberDeclaration(context, typeDeclaration, typeDeclaration.TypeParameterList);
74+
Accessibility declaredAccessibility = typeDeclaration.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
75+
Accessibility effectiveAccessibility = typeDeclaration.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
76+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, typeDeclaration.Kind(), typeDeclaration.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
77+
HandleMemberDeclaration(context, needsComment, typeDeclaration, typeDeclaration.TypeParameterList);
7478
}
7579

76-
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
80+
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
7781
{
7882
MethodDeclarationSyntax methodDeclaration = (MethodDeclarationSyntax)context.Node;
7983

80-
HandleMemberDeclaration(context, methodDeclaration, methodDeclaration.TypeParameterList);
84+
Accessibility declaredAccessibility = methodDeclaration.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
85+
Accessibility effectiveAccessibility = methodDeclaration.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
86+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, methodDeclaration.Kind(), methodDeclaration.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
87+
HandleMemberDeclaration(context, needsComment, methodDeclaration, methodDeclaration.TypeParameterList);
8188
}
8289

83-
private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context)
90+
private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
8491
{
8592
DelegateDeclarationSyntax delegateDeclaration = (DelegateDeclarationSyntax)context.Node;
8693

87-
HandleMemberDeclaration(context, delegateDeclaration, delegateDeclaration.TypeParameterList);
94+
Accessibility declaredAccessibility = delegateDeclaration.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
95+
Accessibility effectiveAccessibility = delegateDeclaration.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
96+
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, delegateDeclaration.Kind(), delegateDeclaration.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
97+
HandleMemberDeclaration(context, needsComment, delegateDeclaration, delegateDeclaration.TypeParameterList);
8898
}
8999

90-
private static void HandleMemberDeclaration(SyntaxNodeAnalysisContext context, SyntaxNode node, TypeParameterListSyntax typeParameterList)
100+
private static void HandleMemberDeclaration(SyntaxNodeAnalysisContext context, bool needsComment, SyntaxNode node, TypeParameterListSyntax typeParameterList)
91101
{
102+
if (!needsComment)
103+
{
104+
// Documentation is not required for this element.
105+
return;
106+
}
107+
92108
if (typeParameterList == null)
93109
{
94110
// The member does not have a type parameter list

0 commit comments

Comments
 (0)