Skip to content

Commit

Permalink
Update SA1615 to suppress messages when documentation for an element …
Browse files Browse the repository at this point in the history
…is optional

Fixes DotNetAnalyzers#2445
  • Loading branch information
sharwell committed Jun 20, 2017
1 parent b99dd6e commit 6161f3c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,25 @@ public int MethodName()
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2445, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2445")]
public async Task TestPrivateMethodMissingReturnsAsync()
{
var testCode = @"
internal class ClassName
{
///
private int Test1(int arg) { throw new System.NotImplementedException(); }
/**
*
*/
private int Test2(int arg) { throw new System.NotImplementedException(); }
}";

await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <inheritdoc/>
protected override Project ApplyCompilationOptions(Project project)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace StyleCop.Analyzers.DocumentationRules
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Settings.ObjectModel;

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

private static readonly Action<SyntaxNodeAnalysisContext> MethodDeclarationAction = HandleMethodDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext> DelegateDeclarationAction = HandleDelegateDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> MethodDeclarationAction = HandleMethodDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> DelegateDeclarationAction = HandleDelegateDeclaration;

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

private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
{
var node = (MethodDeclarationSyntax)context.Node;

HandleDeclaration(context, node.ReturnType);
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
HandleDeclaration(context, needsComment, node.ReturnType);
}

private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context)
private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
{
var node = (DelegateDeclarationSyntax)context.Node;

HandleDeclaration(context, node.ReturnType);
Accessibility declaredAccessibility = node.GetDeclaredAccessibility(context.SemanticModel, context.CancellationToken);
Accessibility effectiveAccessibility = node.GetEffectiveAccessibility(context.SemanticModel, context.CancellationToken);
bool needsComment = SA1600ElementsMustBeDocumented.NeedsComment(settings.DocumentationRules, node.Kind(), node.Parent.Kind(), declaredAccessibility, effectiveAccessibility);
HandleDeclaration(context, needsComment, node.ReturnType);
}

private static void HandleDeclaration(SyntaxNodeAnalysisContext context, TypeSyntax returnType)
private static void HandleDeclaration(SyntaxNodeAnalysisContext context, bool needsComment, TypeSyntax returnType)
{
if (!needsComment)
{
// Documentation is optional for this element.
return;
}

var predefinedType = returnType as PredefinedTypeSyntax;

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

0 comments on commit 6161f3c

Please sign in to comment.