Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SA1642 and its code fix to handle record structs correctly #3696

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace StyleCop.Analyzers.DocumentationRules
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Globalization;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
Expand All @@ -20,6 +20,7 @@ namespace StyleCop.Analyzers.DocumentationRules
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using StyleCop.Analyzers.Helpers;
using StyleCop.Analyzers.Lightup;

/// <summary>
/// Implements a code fix for <see cref="SA1642ConstructorSummaryDocumentationMustBeginWithStandardText"/>
Expand Down Expand Up @@ -83,7 +84,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

internal static ImmutableArray<string> GenerateStandardText(Document document, BaseMethodDeclarationSyntax methodDeclaration, BaseTypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
{
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration) || typeDeclaration.IsKind(SyntaxKindEx.RecordStructDeclaration);
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(methodDeclaration.SyntaxTree, cancellationToken);
var culture = settings.DocumentationRules.DocumentationCultureInfo;
var resourceManager = DocumentationResources.ResourceManager;
Expand Down Expand Up @@ -147,7 +148,19 @@ private static TypeParameterListSyntax GetTypeParameterList(BaseTypeDeclarationS
return classDeclaration.TypeParameterList;
}

return (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
if (typeDeclaration is StructDeclarationSyntax structDeclaration)
{
return structDeclaration.TypeParameterList;
}

if (RecordDeclarationSyntaxWrapper.IsInstance(typeDeclaration))
{
var recordDeclaration = (RecordDeclarationSyntaxWrapper)typeDeclaration;
return recordDeclaration.TypeParameterList;
}

Debug.Assert(false, $"Unhandled type {typeDeclaration.Kind()}");
return null;
}

private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlElementSyntax node, CancellationToken cancellationToken)
Expand Down Expand Up @@ -202,21 +215,10 @@ private static bool IsMultiLine(XmlElementSyntax node)
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlEmptyElementSyntax node)
{
var typeDeclaration = node.FirstAncestorOrSelf<BaseTypeDeclarationSyntax>();

TypeParameterListSyntax typeParameterList;
if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
{
typeParameterList = classDeclaration.TypeParameterList;
}
else
{
typeParameterList = (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
}
var typeParameterList = GetTypeParameterList(typeDeclaration);

var newRoot = root.ReplaceNode(node, BuildSeeElement(typeDeclaration.Identifier, typeParameterList));

var newDocument = document.WithSyntaxRoot(newRoot);

return Task.FromResult(newDocument);
}

Expand Down