You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Review impact of file-scoped namespace declarations on existing rules #3983
Affected StyleCopAnalyzers Rules and File-Scoped Namespace Impact
SA0102 – Syntax error detected
Impact
Early versions of StyleCopAnalyzers did not recognize C# 10 file-scoped namespaces. A top-of-file declaration like:
namespaceMy.Example.Namespace;
was incorrectly parsed as a syntax error and reported as SA0102.
Implementation
Updated the syntax handling to recognize FileScopedNamespaceDeclaration nodes.
File-scoped namespaces no longer register as syntax errors.
Documentation
No substantive change required; SA0102 is a general “parse error” rule.
May optionally note that C# 10 file-scoped namespaces are supported.
Tests
Added tests to verify that file-scoped namespaces do not produce SA0102 diagnostics.
SA1135 – Using directives must be fully qualified
Impact
Initially, SA1135 failed to run for file-scoped namespaces.
Example:
namespaceMy.Project.Bar;usingFoo;
In early versions, SA1135 did not warn about the unqualified using Foo; in this context.
Implementation
Updated the analyzer to recognize using directives appearing under a file-scoped namespace.
The same checks now apply regardless of whether the namespace is block-scoped or file-scoped.
Documentation
The rule description still applies as-is.
Clarified that SA1135 applies regardless of namespace declaration style (block vs file-scoped).
Tests
Added unit tests using file-scoped namespaces.
Verified that unqualified using directives inside file-scoped namespaces are flagged and the code fix correctly qualifies them.
SA1200 – Using directives must be placed correctly (inside/outside namespace)
Impact
With C# 10 file-scoped namespaces (namespace X;), the meaning of “inside” vs “outside” needed to be revisited.
Originally, SA1200 could erroneously flag using directives before a file-scoped namespace as being “outside a namespace”, even though the rest of the file is implicitly within the namespace.
Implementation
Updated SA1200 to treat file-scoped namespaces as valid namespace containers.
Behavior now respects the project’s configuration:
If the convention is “usings inside namespaces”, SA1200 expects using directives to appear after the namespace X; line.
If the convention is “usings outside namespaces”, it allows using directives above the namespace X; line.
Special-cased C# 10 global using so that global usings are never flagged by SA1200, even when “inside” placement is enforced.
Documentation
Clarified how “inside” vs “outside” applies when using file-scoped namespaces.
Explicitly notes that namespace Name; declares the namespace container for the entire file.
Tests
Added tests covering:
Usings before the file-scoped namespace when “inside” is required (should be flagged; code fix moves them).
Usings after the file-scoped namespace when “outside” is required (should be flagged accordingly).
Global usings to ensure SA1200 ignores them.
SA1208 – System using directives must come before others
Impact
Initially, StyleCopAnalyzers did not check using order within file-scoped namespaces.
In effect, SA1208 did nothing for:
namespaceFoo;usingMyProject;usingSystem;
Implementation
Included file-scoped namespaces in SA1208’s analysis.
Now, if a Systemusing appears after a non-Systemusinginside a file-scoped namespace, SA1208 reports it.
Documentation
Intent unchanged: System namespace usings must be grouped before other usings.
Implicitly applies to file-scoped namespace files as well.
Tests
Added tests where file-scoped namespaces contain mixed using directives to ensure SA1208 reports them when out of order, and the code fix reorders correctly.
SA1209 – Using-alias directives placement
Impact
SA1209 enforces that alias using directives (using X = ...;) come after regular using directives.
In file-scoped namespaces, this check initially did not run, so an alias using before a normal using was not flagged.
Implementation
Extended ordering logic (already used in braced namespaces) to file-scoped contexts.
Alias using directives that appear before non-alias using directives in a file-scoped namespace are now flagged.
Documentation
No major changes; wording already covers the relative ordering.
Clarified that the rule applies equally when using namespace X; syntax.
Tests
Added tests with file-scoped namespaces where:
Alias usings appear before normal usings (diagnostic expected; fix moves alias usings down).
Global alias usings are present; they are handled as separate groups and not ordered together with local usings.
SA1210 – Using directives must be ordered alphabetically
Impact
Initially, SA1210 did not run against using directives inside file-scoped namespaces, so out-of-order usings were ignored.
Implementation
Updated SA1210 to include file-scoped namespaces in its scope.
All using directives following a namespace X; declaration are now sorted and checked for alphabetical order just like in traditional namespaces.
Documentation
Main rule remains: using directives must be in alphabetical order by namespace.
C# 10 syntax is now explicitly supported.
Tests
Added tests such as:
namespaceFoo;usingZ;usingA;
This should trigger SA1210 and get a code fix that sorts using directives alphabetically.
SA1202 – Elements must be ordered by access
Impact
SA1202 enforces ordering from most accessible to least accessible within a container.
In file-scoped namespaces, top-level type declarations were initially not checked:
Description (“each file should contain at most one type”) now implicitly includes file-scoped namespace files.
No special casing required, since C# allows at most one file-scoped namespace per file anyway.
Tests
Added tests where file-scoped namespaces include multiple types.
Verified that SA1402 is reported and that the code fix moves each type into its own file, preserving the file-scoped namespace in the new files as appropriate.
SA1649 – File name must match first type name
Impact
SA1649 must locate the first type in a file to compare the type name with the file name.
Originally, it did not correctly descend into file-scoped namespaces, so types declared inside a namespace X; could be missed or cause errors.
Implementation
Updated syntax tree analysis to descend into FileScopedNamespaceDeclaration nodes when looking for the first type or delegate.
SA1649 now correctly finds the first type inside a file-scoped namespace and compares its identifier to the file name.
Documentation
Rule text is unchanged (“file name should match the first type’s name”).
Clarified that this applies regardless of namespace style.
Included coverage for new top-level types like record structs in C# 10 and ensured no crashes occur.
SA1516 – Elements must be separated by a blank line
Impact
File-scoped namespaces remove the enclosing braces from namespace declarations.
Initially, SA1516 did not enforce blank lines:
After a file-scoped namespace declaration.
Between using directives and the first type.
Example that previously produced no diagnostic:
namespaceFoo;usingSystem;classBar{}
Implementation
Updated SA1516 to treat the end of a namespace Foo; declaration as a point after which a blank line is required before the next element.
In file-scoped files, the rule now enforces:
A blank line after the namespace line before any using, extern alias, or type.
Blank lines between groups where appropriate (e.g., between extern aliases, using directives, and the first type).
Documentation
Clarified that a file-scoped namespace should be followed by a blank line, similar to leaving a blank line after the closing brace of a traditional namespace.
Tests
Extensive C# 10 tests added, covering:
using directives immediately following the file-scoped namespace.
Type declarations immediately following.
Combinations of extern aliases and using directives.
Verified SA1516 diagnostics and code fixes (inserting appropriate blank lines) in all these scenarios.
Summary
All of the above rules have been updated to fully support C# 10 file-scoped namespaces. Implementations recognize the new syntax, documentation has been clarified where needed, and tests were added to ensure that files using namespace Name; receive the same analysis as traditional braced namespaces.
Activity
sharwell commentedon Dec 3, 2025
Audit by Deep Research
Affected StyleCopAnalyzers Rules and File-Scoped Namespace Impact
SA0102 – Syntax error detected
Impact
Early versions of StyleCopAnalyzers did not recognize C# 10 file-scoped namespaces. A top-of-file declaration like:
was incorrectly parsed as a syntax error and reported as SA0102.
Implementation
FileScopedNamespaceDeclarationnodes.Documentation
Tests
SA1135 – Using directives must be fully qualified
Impact
Initially, SA1135 failed to run for file-scoped namespaces.
Example:
In early versions, SA1135 did not warn about the unqualified
using Foo;in this context.Implementation
usingdirectives appearing under a file-scoped namespace.Documentation
Tests
usingdirectives inside file-scoped namespaces are flagged and the code fix correctly qualifies them.SA1200 – Using directives must be placed correctly (inside/outside namespace)
Impact
namespace X;), the meaning of “inside” vs “outside” needed to be revisited.usingdirectives before a file-scoped namespace as being “outside a namespace”, even though the rest of the file is implicitly within the namespace.Implementation
Updated SA1200 to treat file-scoped namespaces as valid namespace containers.
Behavior now respects the project’s configuration:
usingdirectives to appear after thenamespace X;line.usingdirectives above thenamespace X;line.Special-cased C# 10
global usingso that global usings are never flagged by SA1200, even when “inside” placement is enforced.Documentation
namespace Name;declares the namespace container for the entire file.Tests
Added tests covering:
SA1208 – System using directives must come before others
Impact
Initially, StyleCopAnalyzers did not check
usingorder within file-scoped namespaces.In effect, SA1208 did nothing for:
Implementation
Systemusingappears after a non-Systemusinginside a file-scoped namespace, SA1208 reports it.Documentation
Systemnamespace usings must be grouped before other usings.Tests
usingdirectives to ensure SA1208 reports them when out of order, and the code fix reorders correctly.SA1209 – Using-alias directives placement
Impact
usingdirectives (using X = ...;) come after regularusingdirectives.usingbefore a normalusingwas not flagged.Implementation
usingdirectives that appear before non-aliasusingdirectives in a file-scoped namespace are now flagged.Documentation
namespace X;syntax.Tests
Added tests with file-scoped namespaces where:
SA1210 – Using directives must be ordered alphabetically
Impact
usingdirectives inside file-scoped namespaces, so out-of-order usings were ignored.Implementation
usingdirectives following anamespace X;declaration are now sorted and checked for alphabetical order just like in traditional namespaces.Documentation
usingdirectives must be in alphabetical order by namespace.Tests
Added tests such as:
This should trigger SA1210 and get a code fix that sorts
usingdirectives alphabetically.SA1202 – Elements must be ordered by access
Impact
SA1202 enforces ordering from most accessible to least accessible within a container.
In file-scoped namespaces, top-level type declarations were initially not checked:
produced no SA1202 diagnostic.
Implementation
Documentation
Tests
Added tests where:
should produce SA1202 indicating that the public element must come before the internal one.
Verified the code fix reorders elements correctly.
SA1204 – Static elements must appear before instance elements
Impact
SA1204 enforces that static elements appear before instance elements.
Initially, file-level declarations within file-scoped namespaces were not considered, leading to scenarios like:
with no SA1204 diagnostics.
Implementation
Documentation
Tests
SA1402 – File may only contain a single type
Impact
SA1402 enforces that each file contains at most one top-level type (unless configuration allows more).
With file-scoped namespaces, multiple types inside the same namespace were not counted, so multiple top-level types in:
did not trigger SA1402.
Implementation
file-local types are handled differently (outside C# 10 scope).Documentation
Tests
SA1649 – File name must match first type name
Impact
namespace X;could be missed or cause errors.Implementation
FileScopedNamespaceDeclarationnodes when looking for the first type or delegate.Documentation
Tests
Added tests such as:
to ensure SA1649 reports the mismatch.
Included coverage for new top-level types like record structs in C# 10 and ensured no crashes occur.
SA1516 – Elements must be separated by a blank line
Impact
File-scoped namespaces remove the enclosing braces from namespace declarations.
Initially, SA1516 did not enforce blank lines:
usingdirectives and the first type.Example that previously produced no diagnostic:
Implementation
Updated SA1516 to treat the end of a
namespace Foo;declaration as a point after which a blank line is required before the next element.In file-scoped files, the rule now enforces:
namespaceline before anyusing,extern alias, or type.usingdirectives, and the first type).Documentation
Tests
Extensive C# 10 tests added, covering:
usingdirectives immediately following the file-scoped namespace.usingdirectives.Verified SA1516 diagnostics and code fixes (inserting appropriate blank lines) in all these scenarios.
Summary
All of the above rules have been updated to fully support C# 10 file-scoped namespaces. Implementations recognize the new syntax, documentation has been clarified where needed, and tests were added to ensure that files using
namespace Name;receive the same analysis as traditional braced namespaces.References
SA0102 appears to be falsely reported for file scoped namespaces? (1.2.0-beta.556) #3868
SA1135 doesn't fire when using file-scoped namespace syntax #3415
SA1200 wrongly firing with File Scoped Namespaces? #3578
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/ff14d1107673b4ffefba0162077e6a8084eb5bb7/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1200CSharp10UnitTests.cs
SA1210 and SA1208 are ignored when using statements are inside a file-scoped namespace. #3764
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/ff14d1107673b4ffefba0162077e6a8084eb5bb7/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1209CSharp10UnitTests.cs
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/ff14d1107673b4ffefba0162077e6a8084eb5bb7/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10UnitTests.cs
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/ff14d1107673b4ffefba0162077e6a8084eb5bb7/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1202CSharp10UnitTests.cs
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/ff14d1107673b4ffefba0162077e6a8084eb5bb7/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1204CSharp10UnitTests.cs
SA1402 is not raised when declaring multiple types in a file-scoped namespace #3851
https://github.com/bjornhellander/NewStyleCopAnalyzers/blob/3f6f7dbc166848b9824b652f082a547ee0f3a247/StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1649FileNameMustMatchTypeName.cs
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/ff14d1107673b4ffefba0162077e6a8084eb5bb7/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/LayoutRules/SA1516CSharp10UnitTests.cs
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/releases