@@ -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