Skip to content

Simplify and modernize code style configuration (.editorconfig + .DotSettings) #55

@craigsmitham

Description

@craigsmitham

Summary

Simplify and unify code style configuration by removing redundant settings that match Roslyn defaults, adding modern C# 12/13 style rules, resolving formatting tool conflicts, and trimming .DotSettings overlap.

Background

The current .editorconfig has ~50 settings that match Roslyn defaults exactly and can be removed. Several modern C# 12/13 style rules are missing. The .DotSettings file duplicates some settings already in .editorconfig and Directory.Build.props. Additionally, dotnet format and jb cleanupcode produce different output for the same config, causing formatting churn.

Plan

Phase 1: Remove redundant settings (~50 lines)

Remove all settings where both value and severity match Roslyn defaults:

Formatting rules (all match defaults, ~30 settings):

  • All 7 csharp_new_line_* settings
  • All 6 csharp_indent_* settings
  • All 18 csharp_space_* settings
  • csharp_preserve_single_line_blocks

Code style settings matching defaults:

  • dotnet_separate_import_directive_groups
  • All 4 dotnet_style_parentheses_* settings
  • csharp_style_expression_bodied_lambdas
  • csharp_style_unused_value_expression_statement_preference

Severity consolidation: Replace ~35 rules that share the pattern "default value, elevated to :suggestion" with a single bulk severity line:

dotnet_analyzer_diagnostic.category-Style.severity = suggestion

Then only override specific rules that need warning, silent, or none.

Phase 2: Add modern C# 12/13 rules

Rule Setting Suggested
Primary constructors (C# 12) csharp_style_prefer_primary_constructors true:suggestion
Collection expressions (C# 12) dotnet_style_prefer_collection_expression when_types_loosely_match:suggestion
Extended property pattern (C# 10) csharp_style_prefer_extended_property_pattern true:suggestion
Readonly struct csharp_style_prefer_readonly_struct true:suggestion
Readonly struct member csharp_style_prefer_readonly_struct_member true:suggestion
Method group conversion csharp_style_prefer_method_group_conversion true:suggestion
Null check over type check csharp_style_prefer_null_check_over_type_check true:suggestion
Top-level statements csharp_style_prefer_top_level_statements true:silent

Phase 3: Resolve formatting tool conflicts

  • Add targeted resharper_* settings to .editorconfig to make JB match dotnet format output for:
    • Attribute placement on enum members
    • Object/array initializer wrapping
  • Use --no-builtin-settings with cleanupcode in CI
  • Verify idempotency: dotnet formatjb cleanupcodedotnet format --verify-no-changesjb cleanupcodegit diff --exit-code

Phase 4: Trim .DotSettings

  • Remove mandatory imports that duplicate Directory.Build.props <Using> items (JetBrains.Annotations, System.Diagnostics.CodeAnalysis, GraphZen.Infrastructure)
  • Remove/comment ReSharper inspection severities that have .editorconfig equivalents (e.g., ConvertToCollectionExpression → IDE0300, ConvertToUsingDeclaration → IDE0063)
  • Document what remains in .DotSettings and why (cleanup profile, file templates, inspection excludes)

Settings that must stay (non-default values)

Setting Project Value Roslyn Default
dotnet_sort_system_directives_first true false
csharp_style_var_* (all 3) true false
csharp_style_expression_bodied_methods true false
csharp_style_expression_bodied_constructors true false
csharp_style_expression_bodied_operators true false
csharp_style_expression_bodied_local_functions true false
csharp_style_namespace_declarations file_scoped block_scoped
csharp_preserve_single_line_statements false true

Verification

  • dotnet format --verify-no-changes passes
  • dotnet jb cleanupcode ... && git diff --exit-code passes
  • dotnet build with EnforceCodeStyleInBuild=true shows no new warnings
  • .editorconfig is significantly shorter with no loss of enforcement

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions