Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 20, 2026

  • Create AvoidUnusedInternalTypesFixer with two code fix actions:
    • Remove type declaration - ✅ Removes document from solution when it's the only type (preserves files with assembly attributes)
    • Add DynamicallyAccessedMembers attribute - ✅ Working
  • Add comprehensive tests for both code fixers:
    • Test removing single type from file - ✅
    • Test removing type when multiple types exist in file - ✅
    • Test removing nested type - ✅
    • Test removing type with usings - ✅
    • Test removing type with namespace - ✅
    • Test removing type (record struct) - ✅
    • Test removing first of two unused types - ✅
    • Test removing type with assembly attribute - ✅
    • Test adding DynamicallyAccessedMembers attribute to class - ✅
    • Test adding DynamicallyAccessedMembers attribute to struct - ✅
    • Test adding DynamicallyAccessedMembers attribute to record - ✅
    • Test adding DynamicallyAccessedMembers attribute with existing attributes - ✅
  • Build and validate the changes - All 110 tests passing
  • Update markdown documentation
  • Address review feedback:
    • Changed to Solution-based approach
    • Use RemoveDocument instead of creating empty file
    • Preserve files with assembly attributes
Original prompt

Can you add 2 code fixers for MA0182:

  • Remove the type (and the file if there is no other type in the file)
  • Add DynamicallyAccessMembers attribute

Be sure to include as many tests as needed


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add code fixers for MA0182 issues Add code fixers for MA0182 (Avoid unused internal types) Jan 20, 2026
Copilot AI requested a review from meziantou January 20, 2026 22:00
// If this is the only type in the file, return a document that deletes the file
if (allTypes.Count == 1 && allTypes.Contains(typeDeclarationSyntax))
{
// Return an empty document to signal file deletion
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot I'm not sure this works. I think you need to use an overload of CodeAction.Create that uses a solution

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit b59f55d. Changed RemoveTypeDeclaration to return Task<Solution> instead of Task<Document>, and now using SyntaxFactory.CompilationUnit() to create an empty file when removing the only type. All 109 tests passing.

{
var newRoot = SyntaxFactory.CompilationUnit();
var newDocument = document.WithSyntaxRoot(newRoot);
return newDocument.Project.Solution;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Shouldn't this create a new solution without the document?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit c16d1f0. Now using solution.RemoveDocument(document.Id) to remove the document from the solution when it contains only one unused type. Updated test framework to handle document removal. All 109 tests passing.

var allTypes = GetAllTypeDeclarations(compilationUnit);

// If this is the only type in the file, remove the document from the solution
if (allTypes.Count == 1 && allTypes.Contains(typeDeclarationSyntax))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot I think it should also test for assembly attribute [assembly: xyz]. In this case, it shouldn't remove the file. Please add a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 2e8a1d4. Added check for compilationUnit.AttributeLists.Count == 0 to preserve files with assembly attributes. Added test CodeFix_RemoveType_WithAssemblyAttribute to verify the behavior. All 110 tests passing.

@meziantou meziantou marked this pull request as ready for review January 21, 2026 02:19
@meziantou meziantou merged commit 625893d into main Jan 21, 2026
13 checks passed
@meziantou meziantou deleted the copilot/add-code-fixers-ma0182 branch January 21, 2026 02:21
This was referenced Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants