Skip to content

Commit 214eb7e

Browse files
authored
NFC: enforce more C# code style + enable it in Visual Studio (#1687)
1 parent 4d3a992 commit 214eb7e

File tree

11 files changed

+150
-23
lines changed

11 files changed

+150
-23
lines changed

crates/bindings-csharp/.editorconfig

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,111 @@
11
root = true
22

33
[*.cs]
4-
csharp_using_directive_placement = inside_namespace
5-
csharp_style_namespace_declarations = file_scoped
4+
csharp_using_directive_placement = inside_namespace:warning
5+
csharp_style_namespace_declarations = file_scoped:warning
6+
csharp_prefer_simple_using_statement = true:warning
7+
csharp_prefer_braces = true:silent
8+
csharp_style_prefer_method_group_conversion = true:silent
9+
csharp_style_prefer_top_level_statements = true:silent
10+
csharp_style_prefer_primary_constructors = true:warning
11+
csharp_style_expression_bodied_methods = when_on_single_line:silent
12+
csharp_style_expression_bodied_constructors = when_on_single_line:silent
13+
csharp_style_expression_bodied_operators = when_on_single_line:silent
14+
csharp_style_expression_bodied_properties = when_on_single_line:silent
15+
csharp_style_expression_bodied_indexers = when_on_single_line:silent
16+
csharp_style_expression_bodied_accessors = when_on_single_line:silent
17+
csharp_style_expression_bodied_lambdas = when_on_single_line:silent
18+
csharp_style_expression_bodied_local_functions = when_on_single_line:silent
19+
csharp_indent_labels = one_less_than_current
20+
csharp_style_inlined_variable_declaration = true:warning
21+
csharp_style_deconstructed_variable_declaration = true:suggestion
22+
csharp_style_var_for_built_in_types = true:warning
23+
csharp_style_var_when_type_is_apparent = true:warning
24+
csharp_style_var_elsewhere = true:suggestion
25+
csharp_style_prefer_extended_property_pattern = true:suggestion
26+
csharp_style_prefer_not_pattern = true:suggestion
27+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
28+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
29+
csharp_style_prefer_pattern_matching = true:warning
30+
csharp_style_prefer_switch_expression = true:warning
31+
csharp_style_conditional_delegate_call = true:warning
32+
csharp_style_prefer_readonly_struct_member = true:warning
33+
csharp_style_prefer_readonly_struct = true:warning
34+
csharp_prefer_static_anonymous_function = true:suggestion
35+
csharp_prefer_static_local_function = true:suggestion
36+
csharp_space_around_binary_operators = before_and_after
37+
38+
[*.{cs,vb}]
39+
#### Naming styles ####
40+
41+
# Naming rules
42+
43+
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
44+
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
45+
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
46+
47+
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
48+
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
49+
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
50+
51+
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
52+
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
53+
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
54+
55+
# Symbol specifications
56+
57+
dotnet_naming_symbols.interface.applicable_kinds = interface
58+
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
59+
dotnet_naming_symbols.interface.required_modifiers =
60+
61+
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
62+
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
63+
dotnet_naming_symbols.types.required_modifiers =
64+
65+
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
66+
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
67+
dotnet_naming_symbols.non_field_members.required_modifiers =
68+
69+
# Naming styles
70+
71+
dotnet_naming_style.begins_with_i.required_prefix = I
72+
dotnet_naming_style.begins_with_i.required_suffix =
73+
dotnet_naming_style.begins_with_i.word_separator =
74+
dotnet_naming_style.begins_with_i.capitalization = pascal_case
75+
76+
dotnet_naming_style.pascal_case.required_prefix =
77+
dotnet_naming_style.pascal_case.required_suffix =
78+
dotnet_naming_style.pascal_case.word_separator =
79+
dotnet_naming_style.pascal_case.capitalization = pascal_case
80+
81+
dotnet_naming_style.pascal_case.required_prefix =
82+
dotnet_naming_style.pascal_case.required_suffix =
83+
dotnet_naming_style.pascal_case.word_separator =
84+
dotnet_naming_style.pascal_case.capitalization = pascal_case
85+
dotnet_style_coalesce_expression = true:suggestion
86+
dotnet_style_null_propagation = true:suggestion
87+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
88+
dotnet_style_prefer_auto_properties = true:silent
89+
dotnet_style_object_initializer = true:suggestion
90+
dotnet_style_collection_initializer = true:suggestion
91+
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
92+
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
93+
dotnet_style_prefer_conditional_expression_over_return = true:silent
94+
dotnet_style_explicit_tuple_names = true:suggestion
95+
dotnet_style_operator_placement_when_wrapping = beginning_of_line
96+
tab_width = 4
97+
indent_size = 4
98+
dotnet_style_qualification_for_event = false:silent
99+
dotnet_style_qualification_for_method = false:silent
100+
dotnet_style_qualification_for_property = false:silent
101+
dotnet_style_qualification_for_field = false:silent
102+
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
103+
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
104+
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
105+
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
106+
dotnet_code_quality_unused_parameters = all:suggestion
107+
dotnet_style_require_accessibility_modifiers = all:silent
108+
dotnet_style_namespace_match_folder = true:suggestion
109+
110+
# https://csharpier.com/docs/IntegratingWithLinters
111+
dotnet_diagnostic.IDE0055.severity = none

crates/bindings-csharp/BSATN.Codegen/Type.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
257257
{
258258
// structs and classes should be always processed
259259
if (node is not EnumDeclarationSyntax enumType)
260+
{
260261
return true;
262+
}
261263

262264
// Ensure variants are contiguous as SATS enums don't support explicit tags.
263265
if (enumType.Members.Any(m => m.EqualsValue is not null))

crates/bindings-csharp/BSATN.Runtime/Attrs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System.Runtime.CompilerServices;
1+
namespace SpacetimeDB;
22

3-
namespace SpacetimeDB;
3+
using System.Runtime.CompilerServices;
44

55
[AttributeUsage(
66
AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Enum,

crates/bindings-csharp/BSATN.Runtime/BSATN/Runtime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System.Text;
2-
31
namespace SpacetimeDB.BSATN;
42

3+
using System.Text;
4+
55
public interface IStructuralReadWrite
66
{
77
void ReadFields(BinaryReader reader);

crates/bindings-csharp/BSATN.Runtime/Builtins.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
namespace SpacetimeDB;
2+
13
using System.Diagnostics;
24
using SpacetimeDB.BSATN;
35
using SpacetimeDB.Internal;
46

5-
namespace SpacetimeDB;
6-
77
public readonly partial struct Unit
88
{
99
// Custom BSATN that returns an inline empty product type that can be recognised by SpacetimeDB.

crates/bindings-csharp/BSATN.Runtime/Internal/ByteArrayComparer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System.Runtime.CompilerServices;
2-
31
namespace SpacetimeDB.Internal;
42

3+
using System.Runtime.CompilerServices;
4+
55
// Note: this utility struct is used by the C# client SDK so it needs to be public.
66
public readonly struct ByteArrayComparer : IEqualityComparer<byte[]>
77
{
@@ -25,7 +25,7 @@ public bool Equals(byte[]? left, byte[]? right)
2525
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2626
static bool EqualsUnvectorized(byte[] left, byte[] right)
2727
{
28-
for (int i = 0; i < left.Length; i++)
28+
for (var i = 0; i < left.Length; i++)
2929
{
3030
if (left[i] != right[i])
3131
{
@@ -38,8 +38,8 @@ static bool EqualsUnvectorized(byte[] left, byte[] right)
3838

3939
public int GetHashCode(byte[] obj)
4040
{
41-
int hash = 17;
42-
foreach (byte b in obj)
41+
var hash = 17;
42+
foreach (var b in obj)
4343
{
4444
hash = hash * 31 + b;
4545
}

crates/bindings-csharp/Codegen.Tests/fixtures/client/Lib.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
53
using System.Runtime.InteropServices;
64
using SpacetimeDB;
75

6+
#pragma warning disable CA1050 // Declare types in namespaces - this is a test fixture, no need for a namespace.
7+
88
[SpacetimeDB.Type]
99
public partial struct CustomStruct
1010
{

crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Runtime.InteropServices;
22
using SpacetimeDB;
33

4+
#pragma warning disable CA1050 // Declare types in namespaces - this is a test fixture, no need for a namespace.
5+
46
[SpacetimeDB.Type]
57
public partial struct CustomStruct
68
{

crates/bindings-csharp/Codegen/Module.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
424424
var addReducers = tuple.Right.Sort((a, b) => a.Key.CompareTo(b.Key));
425425
// Don't generate the FFI boilerplate if there are no tables or reducers.
426426
if (tableNames.IsEmpty && addReducers.IsEmpty)
427+
{
427428
return;
429+
}
428430
context.AddSource(
429431
"FFI.cs",
430432
$$"""

crates/bindings-csharp/Directory.Build.props

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@
1717
<ImplicitUsings>enable</ImplicitUsings>
1818
<Nullable>enable</Nullable>
1919
<AnalysisLevel>latest-Minimum</AnalysisLevel>
20+
<AnalysisModeStyle>Recommended</AnalysisModeStyle>
2021
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
2122
</PropertyGroup>
2223

24+
<!-- Detect unused usings; see https://github.com/dotnet/roslyn/issues/41640. -->
25+
<PropertyGroup>
26+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
27+
<NoWarn>$(NoWarn);CS1591;CS1574</NoWarn>
28+
</PropertyGroup>
29+
2330
<ItemGroup>
2431
<None Include="$(MSBuildThisFileDirectory)/logo.png" Pack="true" PackagePath="" />
2532
<None Include="$(MSBuildThisFileDirectory)/LICENSE" Pack="true" PackagePath="" />

0 commit comments

Comments
 (0)