Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

Visual Studio 2022 .editorconfig を使ってコーディング規約を運用する

0
Posted at

はじめに

備忘録です。.editorconfig についてまとめます。

改訂履歴

  • 2026/05/04 : 初版公開。

本文

1. 環境

  • .NET 8.0
  • Visual Studio Community 2022

2. コーディング規約

いつも .NET 公式のコーディング規則を採用しています。

3. 実際に使用している .editorconfig

昔は dotnet/docs のものを元ネタにしていました。

が、コーディングには微妙だったので、最近は dotnet/runtime のものを少しアレンジして使用しています。

以下、現在使用している .editorconfig です。:suggestion:warning= suggestion= warning にまとめて置換した後、個別で制御を追加しています。

.editorconfig
# editorconfig.org

# top-most EditorConfig file
root = true

# Default settings:
# A newline ending every file
# Use 4 spaces as indentation
[*]
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

# Specify UTF-8 without byte-order mark
[*.{csproj,locproj,nativeproj,proj,resx,slnx,vbproj}]
charset = utf-8

# Generated code
[*{_AssemblyInfo.cs,.notsupported.cs,AsmOffsets.cs}]
generated_code = true

# C# files
[*.cs]
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

# Indentation preferences
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = false
csharp_indent_switch_labels = true
csharp_indent_labels = one_less_than_current

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async:warning

# avoid this. unless absolutely necessary
dotnet_style_qualification_for_field = false:warning
dotnet_style_qualification_for_property = false:warning
dotnet_style_qualification_for_method = false:warning
dotnet_style_qualification_for_event = false:warning

# Types: use keywords instead of BCL types, and permit var only when the type is clear
csharp_style_var_for_built_in_types = false:warning

# ▼▼▼ [MOD] 型が明らかな場合は var を使ってよい ▼▼▼
# csharp_style_var_when_type_is_apparent = false:none
csharp_style_var_when_type_is_apparent = true:warning
# ▲▲▲ [MOD] 型が明らかな場合は var を使ってよい ▲▲▲

csharp_style_var_elsewhere = false:warning
dotnet_style_predefined_type_for_locals_parameters_members = true:warning
dotnet_style_predefined_type_for_member_access = true:warning

# name all constant fields using PascalCase
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = warning
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols  = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style    = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds   = field
dotnet_naming_symbols.constant_fields.required_modifiers = const
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# static fields should have s_ prefix
dotnet_naming_rule.static_fields_should_have_prefix.severity = warning
dotnet_naming_rule.static_fields_should_have_prefix.symbols  = static_fields
dotnet_naming_rule.static_fields_should_have_prefix.style    = static_prefix_style
dotnet_naming_symbols.static_fields.applicable_kinds   = field
dotnet_naming_symbols.static_fields.required_modifiers = static
dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
dotnet_naming_style.static_prefix_style.required_prefix = s_
dotnet_naming_style.static_prefix_style.capitalization = camel_case

# internal and private fields should be _camelCase
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = warning
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols  = private_internal_fields
dotnet_naming_rule.camel_case_for_private_internal_fields.style    = camel_case_underscore_style
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
dotnet_naming_style.camel_case_underscore_style.required_prefix = _
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case

# ▼▼▼ [ADD] インターフェースは I + PascalCase とする ▼▼▼
dotnet_naming_rule.interfaces_should_have_i_prefix.severity = warning
dotnet_naming_rule.interfaces_should_have_i_prefix.symbols = interfaces
dotnet_naming_rule.interfaces_should_have_i_prefix.style = interface_style
dotnet_naming_symbols.interfaces.applicable_kinds = interface
dotnet_naming_style.interface_style.required_prefix = I
dotnet_naming_style.interface_style.capitalization = pascal_case
# ▲▲▲ [ADD] インターフェースは I + PascalCase とする ▲▲▲

# ▼▼▼ [ADD] メソッド・型・プロパティ・イベントは PascalCase とする ▼▼▼
dotnet_naming_rule.methods_should_be_pascal_case.severity = warning
dotnet_naming_rule.methods_should_be_pascal_case.symbols = methods
dotnet_naming_rule.methods_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.methods.applicable_kinds = method
dotnet_naming_rule.types_should_be_pascal_case.severity = warning
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.types.applicable_kinds = class, struct, enum, delegate
dotnet_naming_rule.properties_should_be_pascal_case.severity = warning
dotnet_naming_rule.properties_should_be_pascal_case.symbols = properties
dotnet_naming_rule.properties_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.properties.applicable_kinds = property
dotnet_naming_rule.events_should_be_pascal_case.severity = warning
dotnet_naming_rule.events_should_be_pascal_case.symbols = events
dotnet_naming_rule.events_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.events.applicable_kinds = event
# ▲▲▲ [ADD] メソッド・型・プロパティ・イベントは PascalCase とする ▲▲▲

# ▼▼▼ [ADD] パラメータ・ローカル変数は camelCase とする ▼▼▼
dotnet_naming_rule.parameters_should_be_camel_case.severity = warning
dotnet_naming_rule.parameters_should_be_camel_case.symbols = parameters
dotnet_naming_rule.parameters_should_be_camel_case.style = camel_case_style
dotnet_naming_symbols.parameters.applicable_kinds = parameter
dotnet_naming_rule.locals_should_be_camel_case.severity = warning
dotnet_naming_rule.locals_should_be_camel_case.symbols = locals
dotnet_naming_rule.locals_should_be_camel_case.style = camel_case_style
dotnet_naming_symbols.locals.applicable_kinds = local
dotnet_naming_style.camel_case_style.capitalization = camel_case
# ▲▲▲ [ADD] パラメータ・ローカル変数は camelCase とする ▲▲▲

# Code style defaults
csharp_using_directive_placement = outside_namespace:warning

# ▼▼▼ [ADD] namespace はファイルスコープ形式とする ▼▼▼
csharp_style_namespace_declarations = file_scoped:warning
# ▲▲▲ [ADD] namespace はファイルスコープ形式とする ▲▲▲

dotnet_sort_system_directives_first = true
csharp_prefer_braces = true:silent
csharp_preserve_single_line_blocks = true:none
csharp_preserve_single_line_statements = false:none
csharp_prefer_static_local_function = true:warning
csharp_prefer_simple_using_statement = false:none
csharp_style_prefer_switch_expression = true:warning
dotnet_style_readonly_field = true:warning

# Expression-level preferences
dotnet_style_object_initializer = true:warning
dotnet_style_collection_initializer = true:warning
dotnet_style_prefer_collection_expression = when_types_exactly_match
dotnet_style_explicit_tuple_names = true:warning
dotnet_style_coalesce_expression = true:warning
dotnet_style_null_propagation = true
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning
dotnet_style_prefer_inferred_tuple_names = true:warning
dotnet_style_prefer_inferred_anonymous_type_member_names = true:warning
dotnet_style_prefer_auto_properties = true:warning
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
csharp_prefer_simple_default_expression = true:warning

# Expression-bodied members
csharp_style_expression_bodied_methods = true:silent
csharp_style_expression_bodied_constructors = true:silent
csharp_style_expression_bodied_operators = true:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = true:silent

# Pattern matching
csharp_style_pattern_matching_over_is_with_cast_check = true:warning
csharp_style_pattern_matching_over_as_with_null_check = true:warning
csharp_style_inlined_variable_declaration = true:warning

# Null checking preferences
csharp_style_throw_expression = true:warning
csharp_style_conditional_delegate_call = true:warning

# Other features
csharp_style_prefer_index_operator = false:none
csharp_style_prefer_range_operator = false:none
csharp_style_pattern_local_over_anonymous_function = false:none

# ▼▼▼ [ADD] ファイルヘッダーの要求は不要 ▼▼▼
dotnet_diagnostic.IDE0073.severity = none
# ▲▲▲ [ADD] ファイルヘッダーの要求は不要 ▲▲▲

# ▼▼▼ [ADD] 不要な using は警告表示 ▼▼▼
dotnet_diagnostic.IDE0005.severity = warning
# ▲▲▲ [ADD] 不要な using は警告表示 ▲▲▲

# ▼▼▼ [ADD] public な変数や関数には XML コメントが必要 ▼▼▼
# .csproj に <GenerateDocumentationFile>true</GenerateDocumentationFile> を追加すると ON になる
dotnet_diagnostic.CS1591.severity = warning
# ▲▲▲ [ADD] public な変数や関数には XML コメントが必要 ▲▲▲

# IDE0071: Simplify interpolation - keep as silent hint since ReadOnlySpan<char>.ToString() is required for netstandard targets
dotnet_diagnostic.IDE0071.severity = silent

# IDE0031: Use null propagation - keep as silent hint to avoid build errors with TreatWarningsAsErrors
dotnet_diagnostic.IDE0031.severity = silent

# Space preferences
csharp_space_after_cast = false
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

# ▼▼▼ [DEL] ファイルヘッダーは不要 ▼▼▼
# # License header
# file_header_template = Licensed to the .NET Foundation under one or more agreements.\nThe .NET Foundation licenses this file to you under the MIT license.
# ▲▲▲ [DEL] ファイルヘッダーは不要 ▲▲▲

# xUnit1051: recommends TestContext.Current.CancellationToken (v3 pattern) which
# is not yet adopted; suppress until a full v3 migration is performed.
dotnet_diagnostic.xUnit1051.severity = none

[src/libraries/System.Net.Http/src/System/Net/Http/{SocketsHttpHandler/Http3RequestStream.cs,BrowserHttpHandler/BrowserHttpHandler.cs}]
# disable CA2025, the analyzer throws a NullReferenceException when processing this file: https://github.com/dotnet/roslyn-analyzers/issues/7652
dotnet_diagnostic.CA2025.severity = none

# C++ Files
[*.{cpp,h,in}]
curly_bracket_next_line = true
indent_brace_style = Allman

# Xml project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
indent_size = 2

# Xml build files
[*.builds]
indent_size = 2

# Xml files
[*.{resx,ruleset,slnx,stylecop,xml}]
indent_size = 2

# Xml resource files
[*.resx]
# match Visual Studio behavior
insert_final_newline = false
trim_trailing_whitespace = false

# Xml config files
[*.{props,targets,config,nuspec}]
indent_size = 2

# Data serialization
[*.{json,yaml,yml}]
indent_size = 2

# Shell scripts
[*.sh]
end_of_line = lf
[*.{cmd,bat}]
end_of_line = crlf

おわりに

ソースを清潔に保ちましょう。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?