Skip to content

SA1402: Remove unused usings from extracted files - #4307

Merged
AArnott merged 5 commits into
masterfrom
aarnott-sa1402-relevant-usings
Oct 14, 2026
Merged

AArnott merged 5 commits into
masterfrom
aarnott-sa1402-relevant-usings

Conversation

@AArnott

@AArnott AArnott commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Fixes #3109

The SA1402 code fix copied every using directive into a new file, including directives only needed by the type that remained in the original file. It now removes compiler-identified unnecessary imports from each extracted document, independently for linked project copies, while retaining required imports and formatting.

The regression test failed before the product change because Bar.cs incorrectly contained using System.Collections.Generic;. The complete SA1402 suite passes on the C# 6 and C# 15 test projects (65 and 124 tests respectively). The analyzer is unchanged, so there is no analyzer performance impact. One existing expected output was updated because the extracted type no longer needs using System;.

Keep extracted files focused on the imports their type requires.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 14, 2026 17:32
Copilot AI balanced review requested due to automatic review settings October 14, 2026 17:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Import removal introduces formatting regressions, and the semantic-diagnostics pass does not honor cancellation.

3 open findings
What changed in this PR

Updates the SA1402 extraction code fix to remove unnecessary imports from new files, addressing #3109 without changing the analyzer.

Changes:

  • Uses compiler diagnostics to prune imports independently for each linked project.
  • Adds an import-filtering regression test and updates an existing expected output.
File Description
test/​StyleCop.Analyzers.Test/​MaintainabilityRules/​SA1402ForClassUnitTests.cs Tests import filtering and adjusts formatting expectations.
src/​StyleCop.Analyzers.CodeFixes/​MaintainabilityRules/​SA1402CodeFixProvider.cs Removes unnecessary imports and cleans up surrounding whitespace.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.59877% with 35 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.62%. Comparing base (3410bc0) to head (eb7f4ce).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4307      +/-   ##
==========================================
- Coverage   97.63%   97.62%   -0.01%     
==========================================
  Files        1308     1308              
  Lines      141543   142188     +645     
  Branches     4695     4746      +51     
==========================================
+ Hits       138193   138809     +616     
- Misses       2137     2154      +17     
- Partials     1213     1225      +12     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Honor cancellation when pruning unused imports and preserve spacing around namespace imports and file headers.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 17:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Trivia normalization can delete documentation or directives, modify unrelated namespaces, and introduce formatting violations.

3 open findings
3 resolved since last review

🧠 Review effort: Balanced

Normalize namespace trivia by syntax identity and retain comments and conditional directives around extracted types.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 18:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Header normalization can delete preprocessor directives and corrupt multi-line file headers.

2 open findings
3 resolved since last review

🧠 Review effort: Balanced

Normalize only the separator following the full file header and retain all subsequent directives and comments.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 18:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Import removal can break conditional compilation, and header normalization can incorrectly separate import comments from their directives.

2 open findings
2 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Unused imports are removed despite inactive-code references

src/​StyleCop.Analyzers.CodeFixes/​MaintainabilityRules/​SA1402CodeFixProvider.cs:183

CS8019 only accounts for active code. If the extracted class uses List<int> solely inside an inactive #if FEATURE block, its ordinary using System.Collections.Generic; is removed: the existing directive guards only inspect the using itself. The extracted file then fails to compile when FEATURE is enabled. Conservatively retain imports when the extracted tree contains disabled text, and add a regression covering both symbol configurations.

🧠 Review effort: Balanced

Skip using directives containing directives and distinguish the file header from following import comments.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 14, 2026 18:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Import removal can discard interior comments and leave extra blank lines after namespace-scoped import comments.

0 open findings

2 resolved since last review
Previously missed (2)

In code that hasn't changed since last review

Medium severity Preserve comments inside unused imports

src/​StyleCop.Analyzers.CodeFixes/​MaintainabilityRules/​SA1402CodeFixProvider.cs:199

An unused import such as using /* Important comment */ System; passes this guard, but KeepExteriorTrivia at line 314 discards the comment between its tokens. Preserve interior comments or leave these imports intact. Add a regression verifying that an internally commented unused import survives extraction.

Medium severity Normalize separators after commented namespace imports

src/​StyleCop.Analyzers.CodeFixes/​MaintainabilityRules/​SA1402CodeFixProvider.cs:334

The separator cleanup misses comments on namespace-scoped imports. With // Import comment before two unused usings inside a namespace, KeepExteriorTrivia transfers the comment and the imports' line endings to the extracted class. Both trimming passes stop at the comment, and this header-only cleanup skips the namespace, leaving consecutive blank lines that trigger SA1507. Normalize removal-created separators after preserved namespace import comments too, without changing declaration comments or directives, and add a fixed-output regression.

🧠 Review effort: Balanced

This was referenced Nov 21, 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.

Code fix: SA1402 Include only relevant using statements

2 participants