diff --git a/src/Analyzers/MSTest.Analyzers.CodeFixes/TestClassShouldBeValidFixer.cs b/src/Analyzers/MSTest.Analyzers.CodeFixes/TestClassShouldBeValidFixer.cs index 685697f453..9f47dffa9a 100644 --- a/src/Analyzers/MSTest.Analyzers.CodeFixes/TestClassShouldBeValidFixer.cs +++ b/src/Analyzers/MSTest.Analyzers.CodeFixes/TestClassShouldBeValidFixer.cs @@ -41,7 +41,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) return; } - ClassDeclarationSyntax declaration = syntaxToken.Parent.AncestorsAndSelf().OfType().FirstOrDefault(); + TypeDeclarationSyntax declaration = syntaxToken.Parent.AncestorsAndSelf().OfType().First(); // Register a code action that will invoke the fix. context.RegisterCodeFix( @@ -52,7 +52,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) diagnostic); } - public static async Task FixClassDeclarationAsync(Document document, ClassDeclarationSyntax classDeclaration, CancellationToken cancellationToken) + public static async Task FixClassDeclarationAsync(Document document, TypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); @@ -64,9 +64,9 @@ public static async Task FixClassDeclarationAsync(Document document, C // Remove the static modifier if it exists SyntaxTokenList modifiers = SyntaxFactory.TokenList( - classDeclaration.Modifiers.Where(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword))); + typeDeclaration.Modifiers.Where(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword))); - if (!classDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword)) + if (!typeDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword)) { // Determine the visibility modifier SyntaxToken visibilityModifier = canDiscoverInternals @@ -78,8 +78,8 @@ public static async Task FixClassDeclarationAsync(Document document, C } // Create a new class declaration with the updated modifiers. - ClassDeclarationSyntax newClassDeclaration = classDeclaration.WithModifiers(modifiers); - editor.ReplaceNode(classDeclaration, newClassDeclaration); + TypeDeclarationSyntax newTypeDeclaration = typeDeclaration.WithModifiers(modifiers); + editor.ReplaceNode(typeDeclaration, newTypeDeclaration); SyntaxNode newRoot = editor.GetChangedRoot(); return document.WithSyntaxRoot(newRoot); diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/TestClassShouldBeValidAnalyzerTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/TestClassShouldBeValidAnalyzerTests.cs index 82c94cc4f3..adda5b0aab 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/TestClassShouldBeValidAnalyzerTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/TestClassShouldBeValidAnalyzerTests.cs @@ -54,6 +54,35 @@ await VerifyCS.VerifyCodeFixAsync( fixedCode); } + [TestMethod] + public async Task WhenClassIsInternalAndTestRecord_Diagnostic() + { + string code = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + internal record {|#0:MyTestClass|} + { + } + """; + + string fixedCode = """ + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public record MyTestClass + { + } + """; + + await VerifyCS.VerifyCodeFixAsync( + code, + VerifyCS.Diagnostic(TestClassShouldBeValidAnalyzer.TestClassShouldBeValidRule) + .WithLocation(0) + .WithArguments("MyTestClass"), + fixedCode); + } + [DataRow("private")] [DataRow("internal")] [TestMethod]