Skip to content

Review impact of file-scoped namespace declarations on existing rules #3983

Closed

Activity

  1. added theissue type on Nov 29, 2025
  2. sharwell commented on Dec 3, 2025

    @sharwell
    MemberAuthor

    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:

    namespace My.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:

      namespace My.Project.Bar;
      using Foo;

      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:

      namespace Foo;
      using MyProject;
      using System;

    Implementation

    • Included file-scoped namespaces in SA1208’s analysis.
    • Now, if a System using appears after a non-System using inside 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:

      namespace Foo;
      using Z;
      using A;

      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:

      namespace TestNamespace;
      internal class B { }
      public class A { }

      produced no SA1202 diagnostic.

    Implementation

    • Updated SA1202 to treat the contents of a file-scoped namespace as a valid scope for ordering.
    • Types inside file-scoped namespaces are now checked for access ordering.

    Documentation

    • Concept remains the same; “within a namespace” now explicitly includes file-scoped namespaces.

    Tests

    • Added tests where:

      namespace TestNamespace;
      internal enum B { }
      public enum A { }

      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:

      namespace TestNamespace;
      class C { }
      static class S { }

      with no SA1204 diagnostics.

    Implementation

    • Updated SA1204 to consider the contents of file-scoped namespaces.
    • A static type declared after a non-static type at the file level will now produce SA1204 just as inside braced namespaces.

    Documentation

    • No major changes; clarified that the rule applies in the new syntax as well.

    Tests

    • Added tests where static classes follow non-static classes inside a file-scoped namespace.
    • Verified that the diagnostic is raised and that the code fix moves the static class above instance classes.

    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:

      namespace Z;
      class X { }
      class Y { }

      did not trigger SA1402.

    Implementation

    • Updated the rule so it counts multiple non-nested type declarations inside a file-scoped namespace as violations.
    • Exception: C# 11 file-local types are handled differently (outside C# 10 scope).

    Documentation

    • 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.

    Tests

    • Added tests such as:

      // File: MyClass.cs
      namespace N;
      public class NotMatching { }

      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:

      • After a file-scoped namespace declaration.
      • Between using directives and the first type.

      Example that previously produced no diagnostic:

      namespace Foo;
      using System;
      class Bar { }

    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.

    References

  3. self-assigned this
    on Dec 3, 2025
  4. moved this from Backlog to In progress in StyleCopAnalyzerson Dec 6, 2025
  5. moved this from In progress to Done in StyleCopAnalyzerson Dec 31, 2025
  6. added this to the 1.2-beta.next milestone on Dec 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions