diff --git a/ChangeLog.md b/ChangeLog.md index b23cfccdbd..ffa3448942 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix refactoring ([RR0180](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RR0180.md)) ([#988](https://github.com/josefpihrt/roslynator/pull/988)). - Recognize `ArgumentNullException.ThrowIfNull` ([RCS1227](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1227.md)) ([#992](https://github.com/josefpihrt/roslynator/pull/992)). - Detect pattern matching in [RCS1146](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1146.md) ([#999](https://github.com/josefpihrt/roslynator/pull/999)). +- Handle `using` directive that starts with `global::` [RCS0015](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS0015.md) ([#1000](https://github.com/josefpihrt/roslynator/pull/1000)). ## [4.1.2] - 2022-10-31 diff --git a/src/CSharp/CSharp/Extensions/SyntaxExtensions.cs b/src/CSharp/CSharp/Extensions/SyntaxExtensions.cs index bfdcec66fc..04d3eb52e6 100644 --- a/src/CSharp/CSharp/Extensions/SyntaxExtensions.cs +++ b/src/CSharp/CSharp/Extensions/SyntaxExtensions.cs @@ -4211,10 +4211,15 @@ public static bool IsVoid(this TypeSyntax type) #region UsingDirectiveSyntax internal static IdentifierNameSyntax GetRootNamespace(this UsingDirectiveSyntax usingDirective) { - if (usingDirective.Name is IdentifierNameSyntax identifierName) + NameSyntax name = usingDirective.Name; + + if (name is AliasQualifiedNameSyntax aliasQualifiedName) + name = aliasQualifiedName.Name; + + if (name is IdentifierNameSyntax identifierName) return identifierName; - if (usingDirective.Name is QualifiedNameSyntax qualifiedName) + if (name is QualifiedNameSyntax qualifiedName) { NameSyntax left; @@ -4233,7 +4238,7 @@ internal static IdentifierNameSyntax GetRootNamespace(this UsingDirectiveSyntax } else { - SyntaxDebug.Fail(usingDirective.Name); + SyntaxDebug.Fail(name); } return null; diff --git a/src/Tests/Formatting.Analyzers.Tests/RCS0015BlankLineBetweenUsingDirectivesTests.cs b/src/Tests/Formatting.Analyzers.Tests/RCS0015BlankLineBetweenUsingDirectivesTests.cs index 64a7f04e8c..5b784543c3 100644 --- a/src/Tests/Formatting.Analyzers.Tests/RCS0015BlankLineBetweenUsingDirectivesTests.cs +++ b/src/Tests/Formatting.Analyzers.Tests/RCS0015BlankLineBetweenUsingDirectivesTests.cs @@ -30,6 +30,30 @@ class C using System.Threading; +class C +{ +} +", options: Options.AddConfigOption(ConfigOptionKeys.BlankLineBetweenUsingDirectives, ConfigOptionValues.BlankLineBetweenUsingDirectives_SeparateGroups)); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.BlankLineBetweenUsingDirectives)] + public async Task Test_AddEmptyLine_GlobalAlias() + { + await VerifyDiagnosticAndFixAsync(@" +using global::System;[||] +using Microsoft.CodeAnalysis;[||] +using System.Threading; + +class C +{ +} +", @" +using global::System; + +using Microsoft.CodeAnalysis; + +using System.Threading; + class C { }