Update 2022-01-27 (all scenarios setup)
JetBrains Rider does support the dotnet_diagnostic.IDE* syntax starting from version 2021.3.2.
This simplifies the setup for all possible scenarios into this:
EditorConfig
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
This will cover all the scenarios. Original answer below. Still worth to read it.
There are several different settings which you should control based on your desired state, used IDEs and workflow.
They are described in this article which I strongly recommend to read before you start building .editorconfig
for your project.
Here is a summary for File-scoped, Block-scoped usings, respectively.
EditorConfig/CSproj setup for File-scoped usings
Visual Studio (error on violation)
EditorConfig
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
Note
Syntax option = rule:severity
will be deprecated, sooner or later.
JetBrains Rider (error on violation)
EditorConfig
csharp_style_namespace_declarations = file_scoped:error
Note
Rider doesn't support dotnet_diagnostic.IDE* syntax.
CLI build e.g., CI/CD pipeline
EditorConfig
csharp_style_namespace_declarations = file_scoped
dotnet_diagnostic.IDE0161.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Recommended setup
EditorConfig
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Note
Is the current .NET EditorConfig syntax a mess? Definitely.
EditorConfig/CSproj setup for Block-scoped usings
Visual Studio (error on violation)
EditorConfig
csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
Note
Syntax option = rule:severity
will be deprecated, sooner or later.
JetBrains Rider (error on violation)
EditorConfig
csharp_style_namespace_declarations = block_scoped:error
Note
Rider doesn't support dotnet_diagnostic.IDE* syntax.
CLI build e.g., CI/CD pipeline
EditorConfig
csharp_style_namespace_declarations = block_scoped
dotnet_diagnostic.IDE0160.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
Recommended setup
EditorConfig
csharp_style_namespace_declarations = block_scoped:error
dotnet_diagnostic.IDE0160.severity = error
CSProj
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>