diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/ClassMustBeSealedFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/ClassMustBeSealedFixer.cs index 5a62796e..f6effc16 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/ClassMustBeSealedFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/ClassMustBeSealedFixer.cs @@ -26,7 +26,7 @@ public override FixAllProvider GetFixAllProvider() public override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); - if (root?.FindNode(context.Span, getInnermostNodeForTie: true) is not ClassDeclarationSyntax nodeToFix) + if (root?.FindNode(context.Span, getInnermostNodeForTie: true) is not TypeDeclarationSyntax nodeToFix) return; var title = "Add sealed modifier"; @@ -42,7 +42,7 @@ private static async Task AddSealedModifier(Document document, SyntaxN { var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); - var classNode = (ClassDeclarationSyntax)nodeToFix; + var classNode = (TypeDeclarationSyntax)nodeToFix; var modifiers = classNode.Modifiers.Add(SyntaxKind.SealedKeyword); editor.ReplaceNode(classNode, classNode.WithModifiers(modifiers).WithAdditionalAnnotations(Formatter.Annotation)); return editor.GetChangedDocument(); diff --git a/tests/Meziantou.Analyzer.Test/Rules/ClassMustBeSealedAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/ClassMustBeSealedAnalyzerTests.cs index ec63074b..230c32db 100755 --- a/tests/Meziantou.Analyzer.Test/Rules/ClassMustBeSealedAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/ClassMustBeSealedAnalyzerTests.cs @@ -242,6 +242,20 @@ await CreateProjectBuilder() .ValidateAsync(); } + [Fact] + public async Task Record() + { + await CreateProjectBuilder() + .AddAnalyzerConfiguration("MA0053.class_with_virtual_member_shoud_be_sealed", "true") + .WithSourceCode(""" + internal record [||]Sample(); + """) + .ShouldFixCodeWith(""" + internal sealed record Sample(); + """) + .ValidateAsync(); + } + #if CSHARP10_OR_GREATER [Fact] public async Task TopLevelStatement_10()