Skip to content

Use global using directives for the most common namespaces - #1307

Merged
meziantou merged 1 commit into
mainfrom
feature/global-using-directives-a86a7e
Aug 20, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/global-using-directives-a86a7e

Conversation

@meziantou

Copy link
Copy Markdown
Owner

The same namespaces were repeated in almost every file of the analyzer, the code fixers and the test projects. They are now declared once per project, as Using items in the Directory.Build.props of the folder, so that all the Roslyn versions get them.

Global namespaces per project

Meziantou.Analyzer (213 files) — the namespaces used by 49% to 90% of the files:

Namespace Files that had it
Microsoft.CodeAnalysis 192 (90%)
Meziantou.Framework.Roslyn 174 (81%)
Microsoft.CodeAnalysis.Diagnostics 172 (80%)
System.Collections.Immutable 170 (79%)
Microsoft.CodeAnalysis.Operations 115 (53%)
Meziantou.Analyzer.Internals 106 (49%)

Meziantou.Analyzer.CodeFixers (113 files) — same list without Microsoft.CodeAnalysis.Diagnostics, which is unused there, plus:

Namespace Files that had it
Microsoft.CodeAnalysis.CodeFixes 108 (95%)
System.Composition 106 (93%)
Microsoft.CodeAnalysis.CodeActions 107 (94%)
Microsoft.CodeAnalysis.Editing 98 (86%)
Microsoft.CodeAnalysis.CSharp.Syntax 96 (84%)
Microsoft.CodeAnalysis.CSharp 65 (57%)

Meziantou.Analyzer.Test (201 files):

Namespace Files that had it
TestHelper 189 (94%)
Meziantou.Analyzer.Rules 187 (93%)
Meziantou.Analyzer.Test.Helpers 84 (41%)

The remaining namespaces were used by less than a third of the files and are left as-is. using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; (28% of the code fixers) is also left as-is: making the SyntaxFactory members visible everywhere is more likely to change overload resolution than to help.

Notes for the reviewer

  • The files shared by Meziantou.Analyzer and Meziantou.Analyzer.CodeFixers (RuleIdentifiers.cs, Internals/*.cs, Rules/*Common.cs, ...) only lost the using directives that are global in both projects, so they keep compiling in the two projects.
  • The using directives that duplicate the implicit global usings of the SDK (System, System.Linq, Xunit, ...) are removed as well: 35 of them, in 30 files.
  • Only the using directives at the top of the files are removed, so the code snippets of the tests are untouched.
  • The declarations are Using items in the Directory.Build.props of each folder rather than a GlobalUsings.cs file, so that they follow the same rule as the rest of the configuration: every Roslyn version of the folder gets them.

Validation

  • dotnet build: succeeds for all the Roslyn versions (4.8, 4.14, 5.0, 5.6, 5.9), no new warning.
  • dotnet test: 18012 tests pass, 0 failed, 0 skipped.
  • dotnet run --project src/DocumentationGenerator: no change to the markdown files.

The same namespaces were repeated in almost every file of the analyzer,
the code fixers and the test projects. They are now declared once per
project, as `Using` items in the `Directory.Build.props` of the folder,
so that all the Roslyn versions get them.

Global namespaces per project:

- Meziantou.Analyzer: System.Collections.Immutable,
  Meziantou.Analyzer.Internals, Meziantou.Framework.Roslyn,
  Microsoft.CodeAnalysis, Microsoft.CodeAnalysis.Diagnostics,
  Microsoft.CodeAnalysis.Operations
- Meziantou.Analyzer.CodeFixers: the ones above except
  Microsoft.CodeAnalysis.Diagnostics, plus System.Composition,
  Microsoft.CodeAnalysis.CodeActions, Microsoft.CodeAnalysis.CodeFixes,
  Microsoft.CodeAnalysis.CSharp, Microsoft.CodeAnalysis.CSharp.Syntax,
  Microsoft.CodeAnalysis.Editing
- Meziantou.Analyzer.Test: TestHelper, Meziantou.Analyzer.Rules,
  Meziantou.Analyzer.Test.Helpers

The files shared by Meziantou.Analyzer and Meziantou.Analyzer.CodeFixers
only lost the using directives that are global in both projects.

Also remove the using directives that duplicate the implicit global
usings of the SDK (System, System.Linq, Xunit, ...).
@meziantou
meziantou merged commit 5ae2e78 into main Aug 20, 2026
23 of 24 checks passed
@meziantou
meziantou deleted the feature/global-using-directives-a86a7e branch August 20, 2026 00:18
This was referenced Aug 20, 2026
This was referenced Sep 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.

1 participant