diff --git a/src/Analyzers/CSharp/Tests/MatchFolderAndNamespace/CSharpMatchFolderAndNamespaceTests.cs b/src/Analyzers/CSharp/Tests/MatchFolderAndNamespace/CSharpMatchFolderAndNamespaceTests.cs index 7f423f75ec32b..d63497c7420ce 100644 --- a/src/Analyzers/CSharp/Tests/MatchFolderAndNamespace/CSharpMatchFolderAndNamespaceTests.cs +++ b/src/Analyzers/CSharp/Tests/MatchFolderAndNamespace/CSharpMatchFolderAndNamespaceTests.cs @@ -174,6 +174,27 @@ class Class1 directory: folder); } + [Fact] + public async Task CodeStyleOptionIsFalse() + { + var folder = CreateFolderPath("B", "C"); + var code = +@"namespace A.B +{ + class Class1 + { + } +}"; + + await RunTestAsync( + fileName: "Class1.cs", + fileContents: code, + directory: folder, + editorConfig: EditorConfig + @" +dotnet_style_namespace_match_folder = false" +); + } + [Fact] public async Task SingleDocumentNoReference() { diff --git a/src/Analyzers/Core/Analyzers/MatchFolderAndNamespace/AbstractMatchFolderAndNamespaceDiagnosticAnalyzer.cs b/src/Analyzers/Core/Analyzers/MatchFolderAndNamespace/AbstractMatchFolderAndNamespaceDiagnosticAnalyzer.cs index ed27e55f04ede..d668af8faffd5 100644 --- a/src/Analyzers/Core/Analyzers/MatchFolderAndNamespace/AbstractMatchFolderAndNamespaceDiagnosticAnalyzer.cs +++ b/src/Analyzers/Core/Analyzers/MatchFolderAndNamespace/AbstractMatchFolderAndNamespaceDiagnosticAnalyzer.cs @@ -51,6 +51,12 @@ public override DiagnosticAnalyzerCategory GetAnalyzerCategory() private void AnalyzeNamespaceNode(SyntaxNodeAnalysisContext context) { + var option = context.GetAnalyzerOptions().PreferNamespaceAndFolderMatchStructure; + if (!option.Value) + { + return; + } + // It's ok to not have a rootnamespace property, but if it's there we want to use it correctly context.Options.AnalyzerConfigOptionsProvider.GlobalOptions.TryGetValue(MatchFolderAndNamespaceConstants.RootNamespaceOption, out var rootNamespace);