Skip to content

Commit 4802639

Browse files
authored
Merge pull request #1609 from json-api-dotnet/code-quality-refresh
Code quality refresh
2 parents 4404e89 + 7061e4c commit 4802639

File tree

324 files changed

+4290
-3479
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

324 files changed

+4290
-3479
lines changed

.editorconfig

Lines changed: 120 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,119 @@ root = true
44
[*]
55
indent_style = space
66
indent_size = 4
7+
tab-width = 4
78
charset = utf-8
89
trim_trailing_whitespace = true
910
insert_final_newline = true
1011

11-
[*.{config,csproj,css,js,json,props,ruleset,xslt,html}]
12+
[*.{config,csproj,css,js,json,props,targets,xml,ruleset,xsd,xslt,html,yml,yaml}]
1213
indent_size = 2
14+
tab-width = 2
15+
max_line_length = 160
16+
17+
[*.{cs,cshtml,ascx,aspx}]
1318

14-
[*.{cs}]
1519
#### C#/.NET Coding Conventions ####
1620

21+
# Default severity for IDE* analyzers with category 'Style'
22+
# Note: specific rules below use severity silent, because Resharper code cleanup auto-fixes them.
23+
dotnet_analyzer_diagnostic.category-Style.severity = warning
24+
1725
# 'using' directive preferences
1826
dotnet_sort_system_directives_first = true
19-
csharp_using_directive_placement = outside_namespace:suggestion
27+
csharp_using_directive_placement = outside_namespace:silent
28+
# IDE0005: Remove unnecessary import
29+
dotnet_diagnostic.IDE0005.severity = silent
2030

2131
# Namespace declarations
22-
csharp_style_namespace_declarations = file_scoped:suggestion
32+
csharp_style_namespace_declarations = file_scoped:silent
33+
# IDE0160: Use block-scoped namespace
34+
dotnet_diagnostic.IDE0160.severity = silent
35+
# IDE0161: Use file-scoped namespace
36+
dotnet_diagnostic.IDE0161.severity = silent
2337

2438
# this. preferences
25-
dotnet_style_qualification_for_field = false:suggestion
26-
dotnet_style_qualification_for_property = false:suggestion
27-
dotnet_style_qualification_for_method = false:suggestion
28-
dotnet_style_qualification_for_event = false:suggestion
39+
dotnet_style_qualification_for_field = false:silent
40+
dotnet_style_qualification_for_property = false:silent
41+
dotnet_style_qualification_for_method = false:silent
42+
dotnet_style_qualification_for_event = false:silent
43+
# IDE0003: Remove this or Me qualification
44+
dotnet_diagnostic.IDE0003.severity = silent
45+
# IDE0009: Add this or Me qualification
46+
dotnet_diagnostic.IDE0009.severity = silent
2947

3048
# Language keywords vs BCL types preferences
31-
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
32-
dotnet_style_predefined_type_for_member_access = true:suggestion
49+
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
50+
dotnet_style_predefined_type_for_member_access = true:silent
51+
# IDE0049: Use language keywords instead of framework type names for type references
52+
dotnet_diagnostic.IDE0049.severity = silent
3353

3454
# Modifier preferences
35-
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion
36-
csharp_preferred_modifier_order = public, private, protected, internal, new, static, abstract, virtual, sealed, readonly, override, extern, unsafe, volatile, async:suggestion
37-
csharp_style_pattern_local_over_anonymous_function = false:silent
55+
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
56+
# IDE0040: Add accessibility modifiers
57+
dotnet_diagnostic.IDE0040.severity = silent
58+
csharp_preferred_modifier_order = public, private, protected, internal, new, static, abstract, virtual, sealed, readonly, override, extern, unsafe, volatile, async:silent
59+
# IDE0036: Order modifiers
60+
dotnet_diagnostic.IDE0036.severity = silent
3861

3962
# Expression-level preferences
4063
dotnet_style_operator_placement_when_wrapping = end_of_line
41-
dotnet_style_prefer_auto_properties = true:suggestion
42-
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
43-
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
44-
csharp_style_unused_value_expression_statement_preference = discard_variable:suggestion
64+
dotnet_style_prefer_auto_properties = true:silent
65+
# IDE0032: Use auto property
66+
dotnet_diagnostic.IDE0032.severity = silent
67+
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
68+
# IDE0045: Use conditional expression for assignment
69+
dotnet_diagnostic.IDE0045.severity = silent
70+
dotnet_style_prefer_conditional_expression_over_return = true:silent
71+
# IDE0046: Use conditional expression for return
72+
dotnet_diagnostic.IDE0046.severity = silent
73+
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
74+
# IDE0058: Remove unused expression value
75+
dotnet_diagnostic.IDE0058.severity = silent
76+
77+
# Collection expression preferences (note: partially turned off in Directory.Build.props)
78+
dotnet_style_prefer_collection_expression = when_types_exactly_match
4579

4680
# Parameter preferences
47-
dotnet_code_quality_unused_parameters = non_public:suggestion
81+
dotnet_code_quality_unused_parameters = non_public
82+
83+
# Local functions vs lambdas
84+
csharp_style_prefer_local_over_anonymous_function = false:silent
85+
# IDE0039: Use local function instead of lambda
86+
dotnet_diagnostic.IDE0039.severity = silent
4887

4988
# Expression-bodied members
50-
csharp_style_expression_bodied_accessors = true:suggestion
51-
csharp_style_expression_bodied_constructors = false:suggestion
52-
csharp_style_expression_bodied_indexers = true:suggestion
53-
csharp_style_expression_bodied_lambdas = true:suggestion
54-
csharp_style_expression_bodied_local_functions = false:suggestion
55-
csharp_style_expression_bodied_methods = false:suggestion
56-
csharp_style_expression_bodied_operators = false:suggestion
57-
csharp_style_expression_bodied_properties = true:suggestion
89+
csharp_style_expression_bodied_accessors = true:silent
90+
# IDE0027: Use expression body for accessors
91+
dotnet_diagnostic.IDE0027.severity = silent
92+
csharp_style_expression_bodied_constructors = false:silent
93+
# IDE0021: Use expression body for constructors
94+
dotnet_diagnostic.IDE0021.severity = silent
95+
csharp_style_expression_bodied_indexers = true:silent
96+
# IDE0026: Use expression body for indexers
97+
dotnet_diagnostic.IDE0026.severity = silent
98+
csharp_style_expression_bodied_lambdas = true:silent
99+
# IDE0053: Use expression body for lambdas
100+
dotnet_diagnostic.IDE0053.severity = silent
101+
csharp_style_expression_bodied_local_functions = false:silent
102+
# IDE0061: Use expression body for local functions
103+
dotnet_diagnostic.IDE0061.severity = silent
104+
csharp_style_expression_bodied_methods = false:silent
105+
# IDE0022: Use expression body for methods
106+
dotnet_diagnostic.IDE0022.severity = silent
107+
csharp_style_expression_bodied_operators = false:silent
108+
# IDE0023: Use expression body for conversion operators
109+
dotnet_diagnostic.IDE0023.severity = silent
110+
# IDE0024: Use expression body for operators
111+
dotnet_diagnostic.IDE0024.severity = silent
112+
csharp_style_expression_bodied_properties = true:silent
113+
# IDE0025: Use expression body for properties
114+
dotnet_diagnostic.IDE0025.severity = silent
58115

59116
# Code-block preferences
60-
csharp_prefer_braces = true:suggestion
117+
csharp_prefer_braces = true:silent
118+
# IDE0011: Add braces
119+
dotnet_diagnostic.IDE0011.severity = silent
61120

62121
# Indentation preferences
63122
csharp_indent_case_contents_when_block = false
@@ -66,19 +125,42 @@ csharp_indent_case_contents_when_block = false
66125
csharp_preserve_single_line_statements = false
67126

68127
# 'var' usage preferences
69-
csharp_style_var_for_built_in_types = false:none
70-
csharp_style_var_when_type_is_apparent = true:none
71-
csharp_style_var_elsewhere = false:none
128+
csharp_style_var_for_built_in_types = false:silent
129+
csharp_style_var_when_type_is_apparent = true:silent
130+
csharp_style_var_elsewhere = false:silent
131+
# IDE0007: Use var instead of explicit type
132+
dotnet_diagnostic.IDE0007.severity = silent
133+
# IDE0008: Use explicit type instead of var
134+
dotnet_diagnostic.IDE0008.severity = silent
72135

73136
# Parentheses preferences
74-
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:suggestion
75-
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:suggestion
76-
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:suggestion
77-
78-
# Expression value is never used
79-
dotnet_diagnostic.IDE0058.severity = none
80-
81-
#### Naming Style ####
137+
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:silent
138+
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
139+
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:silent
140+
# IDE0047: Remove unnecessary parentheses
141+
dotnet_diagnostic.IDE0047.severity = silent
142+
# IDE0048: Add parentheses for clarity
143+
dotnet_diagnostic.IDE0048.severity = silent
144+
145+
# IDE0010: Add missing cases to switch statement
146+
dotnet_diagnostic.IDE0010.severity = silent
147+
# IDE0072: Add missing cases to switch expression
148+
dotnet_diagnostic.IDE0072.severity = silent
149+
150+
# IDE0029: Null check can be simplified
151+
dotnet_diagnostic.IDE0029.severity = silent
152+
# IDE0030: Null check can be simplified
153+
dotnet_diagnostic.IDE0030.severity = silent
154+
# IDE0270: Null check can be simplified
155+
dotnet_diagnostic.IDE0270.severity = silent
156+
157+
# JSON002: Probable JSON string detected
158+
dotnet_diagnostic.JSON002.severity = silent
159+
160+
# CA1062: Validate arguments of public methods
161+
dotnet_code_quality.CA1062.excluded_symbol_names = Accept|DefaultVisit|Visit*|Apply*
162+
163+
#### .NET Naming Style ####
82164

83165
dotnet_diagnostic.IDE1006.severity = warning
84166

CodingGuidelines.ruleset

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RuleSet Name="Coding Guidelines" Description=" " ToolsVersion="16.0">
2+
<RuleSet Name="Coding Guidelines" Description=" " ToolsVersion="17.0">
33
<Rules AnalyzerId="CSharpGuidelinesAnalyzer" RuleNamespace="CSharpGuidelinesAnalyzer">
4+
<Rule Id="AV1008" Action="Warning" />
5+
<Rule Id="AV1135" Action="None" />
46
<Rule Id="AV1500" Action="None" />
57
<Rule Id="AV1505" Action="Warning" />
68
<Rule Id="AV1506" Action="Warning" />
79
<Rule Id="AV1507" Action="Warning" />
8-
<Rule Id="AV1008" Action="Warning" />
9-
<Rule Id="AV1130" Action="Warning" />
10-
<Rule Id="AV1135" Action="Info" />
1110
<Rule Id="AV1536" Action="None" />
1211
<Rule Id="AV1537" Action="None" />
13-
<Rule Id="AV1551" Action="Info" />
14-
<Rule Id="AV1555" Action="Warning" />
15-
<Rule Id="AV1564" Action="Info" />
12+
<Rule Id="AV1551" Action="None" />
13+
<Rule Id="AV1564" Action="None" />
1614
<Rule Id="AV1568" Action="Warning" />
1715
<Rule Id="AV1580" Action="None" />
18-
<Rule Id="AV1706" Action="Warning" />
19-
<Rule Id="AV1710" Action="Info" />
20-
<Rule Id="AV1711" Action="Info" />
16+
<Rule Id="AV1704" Action="None" />
17+
<Rule Id="AV1710" Action="None" />
18+
<Rule Id="AV1711" Action="None" />
2119
<Rule Id="AV1738" Action="Warning" />
2220
<Rule Id="AV1739" Action="Warning" />
2321
<Rule Id="AV1745" Action="Warning" />
24-
<Rule Id="AV1755" Action="Warning" />
25-
<Rule Id="AV2210" Action="Warning" />
2622
<Rule Id="AV2220" Action="Warning" />
2723
<Rule Id="AV2230" Action="Info" />
2824
<Rule Id="AV2305" Action="None" />
29-
<Rule Id="AV2310" Action="Info" />
3025
<Rule Id="AV2318" Action="Warning" />
3126
</Rules>
27+
<Rules AnalyzerId="Microsoft.CodeAnalysis.NetAnalyzers" RuleNamespace="Microsoft.CodeAnalysis.NetAnalyzers">
28+
<Rule Id="CA1027" Action="Warning" />
29+
<Rule Id="CA1033" Action="Warning" />
30+
<Rule Id="CA1062" Action="Warning" />
31+
<Rule Id="CA1063" Action="Warning" />
32+
<Rule Id="CA1064" Action="Warning" />
33+
<Rule Id="CA1065" Action="Warning" />
34+
<Rule Id="CA1066" Action="Warning" />
35+
<Rule Id="CA1305" Action="None" />
36+
<Rule Id="CA1716" Action="None" />
37+
<Rule Id="CA1720" Action="None" />
38+
<Rule Id="CA1725" Action="None" />
39+
<Rule Id="CA1810" Action="Warning" />
40+
<Rule Id="CA1820" Action="Warning" />
41+
<Rule Id="CA1822" Action="None" />
42+
<Rule Id="CA1849" Action="Warning" />
43+
<Rule Id="CA2000" Action="Warning" />
44+
<Rule Id="CA2119" Action="Warning" />
45+
<Rule Id="CA2207" Action="Warning" />
46+
<Rule Id="CA2217" Action="Warning" />
47+
</Rules>
48+
<Rules AnalyzerId="System.Text.RegularExpressions.Generator" RuleNamespace="System.Text.RegularExpressions.Generator">
49+
<Rule Id="SYSLIB1045" Action="Warning" />
50+
</Rules>
51+
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.NetAnalyzers" RuleNamespace="Microsoft.CodeAnalysis.CSharp.NetAnalyzers">
52+
<Rule Id="CA1508" Action="Info" />
53+
</Rules>
3254
</RuleSet>

Directory.Build.props

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
<Project>
2+
<PropertyGroup>
3+
<Nullable>enable</Nullable>
4+
<LangVersion>latest</LangVersion>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<IsPackable>false</IsPackable>
7+
<WarnOnPackingNonPackableProject>false</WarnOnPackingNonPackableProject>
8+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
9+
<AnalysisMode>Recommended</AnalysisMode>
10+
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)CodingGuidelines.ruleset</CodeAnalysisRuleSet>
11+
<RunSettingsFilePath>$(MSBuildThisFileDirectory)tests.runsettings</RunSettingsFilePath>
12+
<JsonApiDotNetCoreVersionPrefix>5.6.1</JsonApiDotNetCoreVersionPrefix>
13+
</PropertyGroup>
14+
15+
<PropertyGroup>
16+
<!--
17+
Disabled IDE analyzers for "Use collection expression" because they are dangerous in a subtle way.
18+
For example, a suggestion for the following code is raised:
19+
public IList<string> Items { get; } = new List<string>();
20+
Taking the suggestion TODAY produces List<string>, but the spec doesn't require that, so the compiler may change it over time.
21+
As a result, callers that cast back to List<string> will face a breaking change.
22+
-->
23+
<UseCollectionExpressionRules>IDE0028;IDE0300;IDE0301;IDE0302;IDE0303;IDE0304;IDE0305</UseCollectionExpressionRules>
24+
<NoWarn>$(NoWarn);$(UseCollectionExpressionRules)</NoWarn>
25+
</PropertyGroup>
26+
227
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
328
<NoWarn>$(NoWarn);AV2210</NoWarn>
429
</PropertyGroup>
@@ -13,20 +38,23 @@
1338
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
1439
</PropertyGroup>
1540

41+
<PropertyGroup Condition="$(MSBuildProjectName.EndsWith('Tests'))">
42+
<NoWarn>$(NoWarn);CA1707;CA1062</NoWarn>
43+
</PropertyGroup>
44+
45+
<PropertyGroup
46+
Condition="$(MSBuildProjectName.EndsWith('Example')) Or '$(MSBuildProjectName)' == 'TestBuildingBlocks' Or '$(MSBuildProjectName)' == 'Benchmarks'">
47+
<NoWarn>$(NoWarn);CA1062</NoWarn>
48+
</PropertyGroup>
49+
50+
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
51+
<!-- Workaround for https://github.com/dotnet/runtime/issues/72263 -->
52+
<NoWarn>$(NoWarn);SYSLIB1006</NoWarn>
53+
</PropertyGroup>
54+
1655
<ItemGroup>
1756
<PackageReference Include="JetBrains.Annotations" Version="2023.3.*" PrivateAssets="All" />
1857
<PackageReference Include="CSharpGuidelinesAnalyzer" Version="3.8.*" PrivateAssets="All" />
1958
<AdditionalFiles Include="$(MSBuildThisFileDirectory)CSharpGuidelinesAnalyzer.config" Visible="False" />
2059
</ItemGroup>
21-
22-
<PropertyGroup>
23-
<Nullable>enable</Nullable>
24-
<LangVersion>latest</LangVersion>
25-
<ImplicitUsings>enable</ImplicitUsings>
26-
<IsPackable>false</IsPackable>
27-
<WarnOnPackingNonPackableProject>false</WarnOnPackingNonPackableProject>
28-
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)CodingGuidelines.ruleset</CodeAnalysisRuleSet>
29-
<RunSettingsFilePath>$(MSBuildThisFileDirectory)tests.runsettings</RunSettingsFilePath>
30-
<JsonApiDotNetCoreVersionPrefix>5.6.1</JsonApiDotNetCoreVersionPrefix>
31-
</PropertyGroup>
3260
</Project>

benchmarks/Deserialization/DeserializationBenchmarkBase.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Benchmarks.Deserialization;
1313

14-
public abstract class DeserializationBenchmarkBase
14+
public abstract class DeserializationBenchmarkBase : IDisposable
1515
{
16-
protected readonly JsonSerializerOptions SerializerReadOptions;
17-
protected readonly DocumentAdapter DocumentAdapter;
16+
private readonly ServiceContainer _serviceProvider = new();
17+
18+
protected JsonSerializerOptions SerializerReadOptions { get; }
19+
protected DocumentAdapter DocumentAdapter { get; }
1820

1921
protected DeserializationBenchmarkBase()
2022
{
@@ -23,12 +25,11 @@ protected DeserializationBenchmarkBase()
2325
options.SerializerOptions.Converters.Add(new ResourceObjectConverter(resourceGraph));
2426
SerializerReadOptions = ((IJsonApiOptions)options).SerializerReadOptions;
2527

26-
var serviceContainer = new ServiceContainer();
27-
var resourceFactory = new ResourceFactory(serviceContainer);
28-
var resourceDefinitionAccessor = new ResourceDefinitionAccessor(resourceGraph, serviceContainer);
28+
var resourceFactory = new ResourceFactory(_serviceProvider);
29+
var resourceDefinitionAccessor = new ResourceDefinitionAccessor(resourceGraph, _serviceProvider);
2930

30-
serviceContainer.AddService(typeof(IResourceDefinitionAccessor), resourceDefinitionAccessor);
31-
serviceContainer.AddService(typeof(IResourceDefinition<IncomingResource, int>), new JsonApiResourceDefinition<IncomingResource, int>(resourceGraph));
31+
_serviceProvider.AddService(typeof(IResourceDefinitionAccessor), resourceDefinitionAccessor);
32+
_serviceProvider.AddService(typeof(IResourceDefinition<IncomingResource, int>), new JsonApiResourceDefinition<IncomingResource, int>(resourceGraph));
3233

3334
// ReSharper disable once VirtualMemberCallInConstructor
3435
JsonApiRequest request = CreateJsonApiRequest(resourceGraph);
@@ -53,6 +54,22 @@ protected DeserializationBenchmarkBase()
5354

5455
protected abstract JsonApiRequest CreateJsonApiRequest(IResourceGraph resourceGraph);
5556

57+
public void Dispose()
58+
{
59+
Dispose(true);
60+
GC.SuppressFinalize(this);
61+
}
62+
63+
#pragma warning disable CA1063 // Implement IDisposable Correctly
64+
private void Dispose(bool disposing)
65+
#pragma warning restore CA1063 // Implement IDisposable Correctly
66+
{
67+
if (disposing)
68+
{
69+
_serviceProvider.Dispose();
70+
}
71+
}
72+
5673
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
5774
public sealed class IncomingResource : Identifiable<int>
5875
{

0 commit comments

Comments
 (0)