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

fix incorrect FirstOrDefault usage #4580

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

ClassDeclarationSyntax declaration = syntaxToken.Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>().FirstOrDefault();
TypeDeclarationSyntax declaration = syntaxToken.Parent.AncestorsAndSelf().OfType<TypeDeclarationSyntax>().First();

// Register a code action that will invoke the fix.
context.RegisterCodeFix(
Expand All @@ -52,7 +52,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
diagnostic);
}

public static async Task<Document> FixClassDeclarationAsync(Document document, ClassDeclarationSyntax classDeclaration, CancellationToken cancellationToken)
public static async Task<Document> FixClassDeclarationAsync(Document document, TypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

Expand All @@ -64,9 +64,9 @@ public static async Task<Document> 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
Expand All @@ -78,8 +78,8 @@ public static async Task<Document> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down