Skip to content

Commit 1ea1f3f

Browse files
committed
Add SonarAnalyzer.CSharp and unified analyzer rulesets.
Adds SonarAnalyzer.CSharp 10.27.0.140913 to all projects. Replaces existing Source.ruleset and Test.ruleset with unified versions shared across the Autofac organization. Ensures all projects have AnalysisMode=AllEnabledByDefault and EnforceCodeStyleInBuild=true.
1 parent 4cd64e0 commit 1ea1f3f

6 files changed

Lines changed: 124 additions & 41 deletions

File tree

bench/Autofac.Extras.DynamicProxy.Benchmarks/Autofac.Extras.DynamicProxy.Benchmarks.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<PackageId>Autofac.Extras.DynamicProxy.Benchmarks</PackageId>
1212
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
1313
<CodeAnalysisRuleSet>../../build/Test.ruleset</CodeAnalysisRuleSet>
14+
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
15+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
1416
</PropertyGroup>
1517

1618
<ItemGroup>
@@ -27,6 +29,10 @@
2729
</PackageReference>
2830
<PackageReference Include="xunit" Version="2.4.2" />
2931
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
32+
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.27.0.140913">
33+
<PrivateAssets>all</PrivateAssets>
34+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
35+
</PackageReference>
3036
</ItemGroup>
3137

3238
<ItemGroup>

build/Source.ruleset

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<RuleSet Name="Autofac Analyzer Rules" Description="Analyzer rules for Autofac assemblies." ToolsVersion="16.0">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RuleSet Name="Autofac Analyzer Rules" Description="Analyzer rules for Autofac source assemblies." ToolsVersion="16.0">
33
<IncludeAll Action="Warning" />
4-
<Rules AnalyzerId="Microsoft.Usage" RuleNamespace="Microsoft.Usage">
5-
<!-- Implement standard exception constructors - not all of the exception constructors (e.g., parameterless) are desired in our system. -->
4+
<Rules AnalyzerId="Microsoft.CodeAnalysis.Analyzers" RuleNamespace="Microsoft.CodeAnalysis.Analyzers">
5+
<!-- Implement standard exception constructors - not all of the exception constructors are desired. -->
66
<Rule Id="CA1032" Action="None" />
77
<!-- Avoid excessive inheritance (must be explicitly enabled). -->
88
<Rule Id="CA1501" Action="Warning" />
@@ -12,23 +12,41 @@
1212
<Rule Id="CA1505" Action="Warning" />
1313
<!-- Avoid excessive class coupling (must be explicitly enabled). -->
1414
<Rule Id="CA1506" Action="Warning" />
15-
<!-- Use ArgumentNullException.ThrowIfNull - this isn't available until we stop targeting netstandard. -->
15+
<!-- Use ArgumentNullException.ThrowIfNull - not available in netstandard. -->
1616
<Rule Id="CA1510" Action="None" />
17-
<!-- Use ArgumentOutOfRangeException.ThrowIfNegative - this isn't available until we stop targeting anything below net8.0. -->
17+
<!-- Use ArgumentOutOfRangeException.ThrowIfNegative - not available below net8.0. -->
1818
<Rule Id="CA1512" Action="None" />
19-
<!-- Use ObjectDisposedException.ThrowIf - this isn't available until we stop targeting anything below net8.0. -->
19+
<!-- Use ObjectDisposedException.ThrowIf - not available below net8.0. -->
2020
<Rule Id="CA1513" Action="None" />
21-
<!-- Change names to avoid reserved word overlaps (e.g., Delegate, GetType, etc.) - too many of these in the public API, we'd break if we fixed it. -->
21+
<!-- Change names to avoid reserved word overlaps - would break public API. -->
2222
<Rule Id="CA1716" Action="None" />
23-
<!-- Cache a CompositeFormat object for use in String.Format - this isn't available until we stop targeting netstandard, and we only String.Format when throwing exceptions so the work/complexity isn't justified to increase perf just for those situations. -->
23+
<!-- Cache a CompositeFormat - not available in netstandard. -->
2424
<Rule Id="CA1863" Action="None" />
25-
<!-- Implement serialization constructors - false positive when building .NET Core. -->
25+
<!-- Implement serialization constructors - false positive in .NET Core. -->
2626
<Rule Id="CA2229" Action="None" />
27-
<!-- Mark ISerializable types with SerializableAttribute - false positive when building .NET Core. -->
27+
<!-- Mark ISerializable types with SerializableAttribute - false positive in .NET Core. -->
2828
<Rule Id="CA2237" Action="None" />
29-
<!-- Prefer generic overloads to using Type parameters - many aren't available in earlier frameworks; and we do a lot of reflection work in Autofac. -->
29+
<!-- Prefer generic overloads - conflicts with reflection work in Autofac. -->
3030
<Rule Id="CA2263" Action="None" />
3131
</Rules>
32+
<Rules AnalyzerId="SonarAnalyzer.CSharp" RuleNamespace="SonarAnalyzer.CSharp">
33+
<!-- "Sonar Way" rules to match SonarQube scans. -->
34+
<Rule Id="S107" Action="Warning" />
35+
<Rule Id="S1192" Action="Warning" />
36+
<Rule Id="S1313" Action="Warning" />
37+
<Rule Id="S2259" Action="Warning" />
38+
<Rule Id="S2583" Action="Warning" />
39+
<Rule Id="S2589" Action="Warning" />
40+
<!-- Verify reflection accessibility bypass - DI containers do extensive reflection by design. -->
41+
<Rule Id="S3011" Action="None" />
42+
<!-- Loops should use LINQ - DI container hot paths intentionally avoid LINQ allocations. -->
43+
<Rule Id="S3267" Action="None" />
44+
<Rule Id="S3776" Action="Warning" />
45+
<!-- Split method for params check + iterator - overly prescriptive for our patterns. -->
46+
<Rule Id="S4456" Action="None" />
47+
<Rule Id="S6418" Action="Warning" />
48+
<Rule Id="S6444" Action="Warning" />
49+
</Rules>
3250
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
3351
<!-- Prefix local calls with this. -->
3452
<Rule Id="SA1101" Action="None" />

build/Test.ruleset

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<RuleSet Name="Autofac Analyzer Rules" Description="Analyzer rules for Autofac assemblies." ToolsVersion="16.0">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RuleSet Name="Autofac Analyzer Rules" Description="Analyzer rules for Autofac test assemblies." ToolsVersion="16.0">
33
<IncludeAll Action="Warning" />
4-
<Rules AnalyzerId="Microsoft.Usage" RuleNamespace="Microsoft.Usage">
5-
<!-- Avoid excessive parameters on generic types (must be explicitly enabled). -->
6-
<Rule Id="CA1005" Action="Warning" />
7-
<!-- Don't catch general exceptions - test scenarios sometimes require general exception handling. -->
4+
<Rules AnalyzerId="Microsoft.CodeAnalysis.Analyzers" RuleNamespace="Microsoft.CodeAnalysis.Analyzers">
5+
<!-- Don't catch general exceptions - test scenarios sometimes require it. -->
86
<Rule Id="CA1031" Action="None" />
9-
<!-- Implement standard exception constructors - not all of the exception constructors (e.g., parameterless) are desired in our system. -->
10-
<Rule Id="CA1032" Action="None" />
11-
<!-- Avoid empty interfaces - in unit tests for service resolution, this happens a lot. -->
7+
<!-- Avoid empty interfaces - happens a lot in unit tests for service resolution. -->
128
<Rule Id="CA1040" Action="None" />
13-
<!-- Do not pass literals as localized parameters - tests don't need to localize. -->
9+
<!-- Do not pass literals as localized parameters - tests don't need to localize. -->
1410
<Rule Id="CA1303" Action="None" />
1511
<!-- Avoid excessive inheritance (must be explicitly enabled). -->
1612
<Rule Id="CA1501" Action="Warning" />
@@ -20,49 +16,91 @@
2016
<Rule Id="CA1505" Action="Warning" />
2117
<!-- Avoid excessive class coupling (must be explicitly enabled). -->
2218
<Rule Id="CA1506" Action="Warning" />
23-
<!-- Use ArgumentNullException.ThrowIfNull - this isn't available until we stop targeting netstandard. -->
24-
<Rule Id="CA1510" Action="None" />
25-
<!-- Make API types internal - causes problems with tests and test assemblies. -->
19+
<!-- Make API types internal - causes problems with tests. -->
2620
<Rule Id="CA1515" Action="None" />
27-
<!-- Remove the underscores from member name - unit test scenarios may use underscores. -->
21+
<!-- Remove underscores from member name - test naming conventions use underscore. -->
2822
<Rule Id="CA1707" Action="None" />
29-
<!-- Change names to avoid reserved word overlaps (e.g., Delegate, GetType, etc.) - too many of these in the public API, we'd break if we fixed it. -->
30-
<Rule Id="CA1716" Action="None" />
31-
<!-- Internal class that appears to never be instantiated - lots of false positives here because they're test stubs created by Autofac registrations. -->
23+
<!-- Internal class that appears to never be instantiated - false positives from DI. -->
3224
<Rule Id="CA1812" Action="None" />
33-
<!-- Change Dispose() to call GC.SuppressFinalize - in tests we don't really care and it can impact readability. -->
25+
<!-- Change Dispose() to call GC.SuppressFinalize - unimportant in tests. -->
3426
<Rule Id="CA1816" Action="None" />
35-
<!-- Mark members static - test methods may not access member data but also can't be static. -->
27+
<!-- Mark members static - test methods may not access member data. -->
3628
<Rule Id="CA1822" Action="None" />
37-
<!-- Seal internal types for performance - in tests we don't really care and it gets painful to enforce. -->
29+
<!-- Use the LoggerMessage delegates - noise in tests, performance irrelevant. -->
30+
<Rule Id="CA1848" Action="None" />
31+
<!-- Seal internal types - unimportant in tests. -->
3832
<Rule Id="CA1852" Action="None" />
39-
<!-- Prefer static readonly fields over constant array arguments - constant array arguments happen a lot in unit tests for assertions and test setup. -->
33+
<!-- Prefer static readonly fields over constant arrays - happens in test setup. -->
4034
<Rule Id="CA1861" Action="None" />
41-
<!-- Cache a CompositeFormat object for use in String.Format - this makes unit tests harder to read, and performance isn't an issue. -->
35+
<!-- Cache a CompositeFormat - makes tests harder to read. -->
4236
<Rule Id="CA1863" Action="None" />
43-
<!-- Call ConfigureAwait on tasks - you shouldn't do this in unit test libraries; XUnit has an opposite analyzer. -->
37+
<!-- Call ConfigureAwait - shouldn't in test libraries; XUnit has opposing analyzer. -->
4438
<Rule Id="CA2007" Action="None" />
45-
<!-- Implement serialization constructors - false positive when building .NET Core. -->
39+
<!-- Implement serialization constructors - false positive in .NET Core. -->
4640
<Rule Id="CA2229" Action="None" />
4741
<!-- Use Uri instead of string parameters - strings are easier for testing. -->
4842
<Rule Id="CA2234" Action="None" />
49-
<!-- Mark ISerializable types with SerializableAttribute - false positive when building .NET Core. -->
43+
<!-- Mark ISerializable types with SerializableAttribute - false positive in .NET Core. -->
5044
<Rule Id="CA2237" Action="None" />
51-
<!-- Prefer generic overloads to using Type parameters - we do a lot of reflection work in Autofac that needs to be tested. -->
45+
<!-- Prefer generic overloads - reflection work in Autofac needs to be tested. -->
5246
<Rule Id="CA2263" Action="None" />
5347
</Rules>
48+
<Rules AnalyzerId="SonarAnalyzer.CSharp" RuleNamespace="SonarAnalyzer.CSharp">
49+
<!-- Don't use hardcoded paths or URIs - needed in several test cases. -->
50+
<Rule Id="S1075" Action="None" />
51+
<!-- Utility class should not be instantiated - test fixture classes with nested tests can't be static. -->
52+
<Rule Id="S1118" Action="None" />
53+
<!-- Don't call GC.Collect - required for weak reference and disposal tests. -->
54+
<Rule Id="S1215" Action="None" />
55+
<!-- Remove unused private members - reflection tests need non-public members. -->
56+
<Rule Id="S1144" Action="None" />
57+
<!-- Empty method bodies - test stubs often have no-op implementations. -->
58+
<Rule Id="S1186" Action="None" />
59+
<!-- Remove unused method parameters - reflection tests declare parameters not used in the body. -->
60+
<Rule Id="S1172" Action="None" />
61+
<!-- Remove empty class - test stubs for DI resolution don't need implementation. -->
62+
<Rule Id="S2094" Action="None" />
63+
<!-- Make member static - test fixture members may not access instance data. -->
64+
<Rule Id="S2325" Action="None" />
65+
<!-- Remove unused type parameters - used in reflection testing. -->
66+
<Rule Id="S2326" Action="None" />
67+
<!-- Write-only properties - reflection tests verify property setter behavior. -->
68+
<Rule Id="S2376" Action="None" />
69+
<!-- Classes with only private constructors - reflection tests verify inaccessible constructors. -->
70+
<Rule Id="S3453" Action="None" />
71+
<!-- Remove unassigned auto-property - reflection tests verify non-public property behavior. -->
72+
<Rule Id="S3459" Action="None" />
73+
<!-- Complete the TODO - acceptable in tests for backlog items. -->
74+
<Rule Id="S1135" Action="None" />
75+
<!-- Make instance property static - false positives in data/document types. -->
76+
<Rule Id="S2335" Action="None" />
77+
<!-- Cognitive complexity (must be explicitly enabled). -->
78+
<Rule Id="S3776" Action="Warning" />
79+
<!-- Split method for params check + iterator - overly prescriptive for our patterns. -->
80+
<Rule Id="S4456" Action="None" />
81+
<!-- Don't use Thread.Sleep - required for concurrency and timing tests. -->
82+
<Rule Id="S2925" Action="None" />
83+
<!-- NSubstitute mock verification is complete without property access. -->
84+
<Rule Id="S2970" Action="None" />
85+
<!-- Fields only assigned in constructor - needed to prevent optimization in reflection tests. -->
86+
<Rule Id="S4487" Action="None" />
87+
<!-- Set route attributes at top of controller - false positive. -->
88+
<Rule Id="S6934" Action="None" />
89+
<!-- Use async dispose - sync dispose tests are intentional. -->
90+
<Rule Id="S6966" Action="None" />
91+
</Rules>
5492
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
5593
<!-- Prefix local calls with this. -->
5694
<Rule Id="SA1101" Action="None" />
5795
<!-- Use built-in type alias. -->
5896
<Rule Id="SA1121" Action="None" />
5997
<!-- Use String.Empty instead of "". -->
6098
<Rule Id="SA1122" Action="None" />
61-
<!-- Enforce order of class members by member type - sometimes putting test classes/data by the test helps. -->
99+
<!-- Enforce order of class members by member type. -->
62100
<Rule Id="SA1201" Action="None" />
63-
<!-- Enforce order of class members by member visibility - sometimes putting test classes/data by the test helps. -->
101+
<!-- Enforce order of class members by visibility. -->
64102
<Rule Id="SA1202" Action="None" />
65-
<!-- Enforce order of static vs. non-static members - sometimes putting test classes/data by the test helps. -->
103+
<!-- Enforce order of static vs. non-static members. -->
66104
<Rule Id="SA1204" Action="None" />
67105
<!-- Fields can't start with underscore. -->
68106
<Rule Id="SA1309" Action="None" />
@@ -82,6 +120,13 @@
82120
<Rule Id="SA1618" Action="None" />
83121
<!-- Don't copy/paste documentation. -->
84122
<Rule Id="SA1625" Action="None" />
123+
</Rules>
124+
<Rules AnalyzerId="xUnit" RuleNamespace="xUnit">
125+
<!-- Use Assert.Single when there's only one collection entry. -->
126+
<Rule Id="xUnit2023" Action="Warning" />
127+
</Rules>
128+
<!-- IDE rules for test assemblies. -->
129+
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp" RuleNamespace="Microsoft.CodeAnalysis.CSharp">
85130
<!-- Private member is unused - tests for reflection require members that may not get used. -->
86131
<Rule Id="IDE0051" Action="None" />
87132
<!-- Private member assigned value never read - tests for reflection require values that may not get used. -->

src/Autofac.Extras.DynamicProxy/Autofac.Extras.DynamicProxy.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
6969
<PrivateAssets>all</PrivateAssets>
7070
</PackageReference>
71+
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.27.0.140913">
72+
<PrivateAssets>all</PrivateAssets>
73+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
74+
</PackageReference>
7175
</ItemGroup>
7276

7377
<ItemDefinitionGroup>

test/Autofac.Extras.DynamicProxy.Test.SatelliteAssembly/Autofac.Extras.DynamicProxy.Test.SatelliteAssembly.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageId>Autofac.Extras.DynamicProxy.Test.SatelliteAssembly</PackageId>
1111
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
1212
<CodeAnalysisRuleSet>../../build/Test.ruleset</CodeAnalysisRuleSet>
13+
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
1314
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
1415
<ImplicitUsings>enable</ImplicitUsings>
1516
</PropertyGroup>
@@ -18,6 +19,10 @@
1819
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
1920
<PrivateAssets>all</PrivateAssets>
2021
</PackageReference>
22+
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.27.0.140913">
23+
<PrivateAssets>all</PrivateAssets>
24+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
25+
</PackageReference>
2126
</ItemGroup>
2227

2328
<ItemGroup>

test/Autofac.Extras.DynamicProxy.Test/Autofac.Extras.DynamicProxy.Test.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<SignAssembly>true</SignAssembly>
99
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
1010
<CodeAnalysisRuleSet>../../build/Test.ruleset</CodeAnalysisRuleSet>
11+
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
1112
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
1213
<IsPackable>false</IsPackable>
1314
<LangVersion>latest</LangVersion>
@@ -42,6 +43,10 @@
4243
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
4344
<PrivateAssets>all</PrivateAssets>
4445
</PackageReference>
46+
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.27.0.140913">
47+
<PrivateAssets>all</PrivateAssets>
48+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
49+
</PackageReference>
4550
<PackageReference Include="xunit" Version="2.9.3" />
4651
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.0">
4752
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)