Skip to content

Commit 37af8ff

Browse files
Merge remote-tracking branch 'upstream/main' into useLock
2 parents b66ed7b + 105491f commit 37af8ff

File tree

8 files changed

+510
-472
lines changed

8 files changed

+510
-472
lines changed

src/Analyzers/CSharp/Analyzers/ConvertNamespace/ConvertNamespaceAnalysis.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Collections.Immutable;
65
using System.Diagnostics.CodeAnalysis;
76
using System.Linq;
87
using Microsoft.CodeAnalysis.CodeStyle;
@@ -68,16 +67,14 @@ internal static bool CanOfferUseFileScoped(
6867
if (!canOffer)
6968
return false;
7069

71-
// even if we could offer this here, we have to make sure it would be legal. A file scoped namespace is
72-
// only legal if it's the only namespace in the file and there are no top level statements.
73-
var tooManyNamespaces = root.DescendantNodesAndSelf(n => n is CompilationUnitSyntax or BaseNamespaceDeclarationSyntax)
74-
.OfType<BaseNamespaceDeclarationSyntax>()
75-
.Take(2)
76-
.Count() != 1;
77-
if (tooManyNamespaces)
70+
// even if we could offer this here, we have to make sure it would be legal.
71+
72+
// A file scoped namespace is only legal if it's the only top level member in the file.
73+
if (root.Members is not [var singleMember] || singleMember != namespaceDeclaration)
7874
return false;
7975

80-
if (root.Members.Any(m => m is GlobalStatementSyntax))
76+
// A file scoped namespace is only legal if it's the only namespace in the file.
77+
if (namespaceDeclaration.Members.Any(static m => m is BaseNamespaceDeclarationSyntax))
8178
return false;
8279

8380
return true;

src/Analyzers/CSharp/Tests/ConvertNamespace/ConvertToFileScopedNamespaceAnalyzerTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,4 +1544,46 @@ limit 1
15441544
}
15451545
}.RunAsync();
15461546
}
1547+
1548+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/74214")]
1549+
public async Task TestNotWithClassAfter()
1550+
{
1551+
await new VerifyCS.Test
1552+
{
1553+
TestCode = """
1554+
namespace N
1555+
{
1556+
class Inner { }
1557+
}
1558+
1559+
class Outer { }
1560+
""",
1561+
LanguageVersion = LanguageVersion.CSharp10,
1562+
Options =
1563+
{
1564+
{ CSharpCodeStyleOptions.NamespaceDeclarations, NamespaceDeclarationPreference.FileScoped }
1565+
}
1566+
}.RunAsync();
1567+
}
1568+
1569+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/74214")]
1570+
public async Task TestNotWithClassBefore()
1571+
{
1572+
await new VerifyCS.Test
1573+
{
1574+
TestCode = """
1575+
class Outer { }
1576+
1577+
namespace N
1578+
{
1579+
class Inner { }
1580+
}
1581+
""",
1582+
LanguageVersion = LanguageVersion.CSharp10,
1583+
Options =
1584+
{
1585+
{ CSharpCodeStyleOptions.NamespaceDeclarations, NamespaceDeclarationPreference.FileScoped }
1586+
}
1587+
}.RunAsync();
1588+
}
15471589
}

src/Features/CSharp/Portable/ConvertNamespace/ConvertNamespaceCodeRefactoringProvider.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ namespace Microsoft.CodeAnalysis.CSharp.ConvertNamespace;
2424
using static ConvertNamespaceTransform;
2525

2626
[ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.ConvertNamespace), Shared]
27-
internal class ConvertNamespaceCodeRefactoringProvider : SyntaxEditorBasedCodeRefactoringProvider
27+
[method: ImportingConstructor]
28+
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
29+
internal class ConvertNamespaceCodeRefactoringProvider() : SyntaxEditorBasedCodeRefactoringProvider
2830
{
29-
[ImportingConstructor]
30-
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
31-
public ConvertNamespaceCodeRefactoringProvider()
32-
{
33-
}
34-
3531
protected override ImmutableArray<FixAllScope> SupportedFixAllScopes
3632
=> [FixAllScope.Project, FixAllScope.Solution];
3733

0 commit comments

Comments
 (0)