Skip to content

Commit 2f435bf

Browse files
Add suppressor for GetScope() (#88)
1 parent e325550 commit 2f435bf

10 files changed

Lines changed: 564 additions & 14 deletions

File tree

.editorconfig

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ dotnet_analyzer_diagnostic.category-Style.severity = warning
279279

280280
# XML Documentation
281281
dotnet_diagnostic.CS0105.severity = error # CS0105: Using directive is unnecessary.
282-
dotnet_diagnostic.CS1573.severity = error # CS1573: Missing XML comment for parameter
283-
dotnet_diagnostic.CS1591.severity = error # CS1591: Missing XML comment for publicly visible type or member
284-
dotnet_diagnostic.CS1712.severity = error # CS1712: Type parameter has no matching typeparam tag in the XML comment (but other type parameters do)
282+
dotnet_diagnostic.CS1573.severity = none # CS1573: Missing XML comment for parameter
283+
dotnet_diagnostic.CS1591.severity = none # CS1591: Missing XML comment for publicly visible type or member
284+
dotnet_diagnostic.CS1712.severity = none # CS1712: Type parameter has no matching typeparam tag in the XML comment (but other type parameters do)
285285

286286
# Async
287-
dotnet_diagnostic.CS1998.severity = error # CS1998: Async method lacks 'await' operators and will run synchronously
287+
dotnet_diagnostic.CS1998.severity = none # CS1998: Async method lacks 'await' operators and will run synchronously
288288
dotnet_diagnostic.CS4014.severity = error # CS4014: Because this call is not awaited, execution of the current method continues before the call is completed
289289
dotnet_diagnostic.CA2007.severity = none # CA2007: Consider calling ConfigureAwait on the awaited task
290290

@@ -298,3 +298,22 @@ dotnet_diagnostic.CA5394.severity = none # CA5394: Random is an i
298298
dotnet_diagnostic.CA2000.severity = error # CA2000: Dispose objects before losing scope
299299

300300
dotnet_diagnostic.CA1515.severity = none # CA1515: Consider making public types internal
301+
302+
# Meziantou.Analyzers
303+
MA0053.public_class_should_be_sealed = true
304+
MA0053.exceptions_should_be_sealed = true
305+
306+
dotnet_diagnostic.MA0004.severity = none
307+
dotnet_diagnostic.MA0048.severity = none
308+
dotnet_diagnostic.MA0051.severity = none
309+
dotnet_diagnostic.MA0053.severity = warning
310+
311+
[src/Immediate.Cache.Shared/**.cs]
312+
313+
# XML Documentation
314+
dotnet_diagnostic.CS1573.severity = error # CS1573: Missing XML comment for parameter
315+
dotnet_diagnostic.CS1591.severity = error # CS1591: Missing XML comment for publicly visible type or member
316+
dotnet_diagnostic.CS1712.severity = error # CS1712: Type parameter has no matching typeparam tag in the XML comment (but other type parameters do)
317+
318+
# Async
319+
dotnet_diagnostic.CA2007.severity = error # CA2007: Consider calling ConfigureAwait on the awaited task

Directory.Packages.props

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44
</PropertyGroup>
55

66
<ItemGroup>
7+
<PackageVersion Include="Basic.Reference.Assemblies.Net80" Version="1.6.0" />
78
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
89
<PackageVersion Include="coverlet.msbuild" Version="6.0.4" />
910
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="2.0.2" />
11+
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="6.0.0" />
12+
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="4.14.0" />
13+
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
14+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
15+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.3" />
16+
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" />
17+
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
1018
<PackageVersion Include="Immediate.Handlers" Version="3.2.0" />
1119
<PackageVersion Include="MinVer" Version="7.0.0" />
1220
<PackageVersion Include="xunit.v3.mtp-v2" Version="3.2.2" />

Immediate.Cache.slnx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
<File Path=".github/workflows/release.yml" />
1414
</Folder>
1515
<Folder Name="/Source/">
16+
<Project Path="src/Immediate.Cache.Analyzers/Immediate.Cache.Analyzers.csproj" />
1617
<Project Path="src/Immediate.Cache.Shared/Immediate.Cache.Shared.csproj" />
1718
<Project Path="src/Immediate.Cache/Immediate.Cache.csproj" />
1819
</Folder>
1920
<Folder Name="/Tests/">
2021
<Project Path="tests/Immediate.Cache.FunctionalTests/Immediate.Cache.FunctionalTests.csproj" />
22+
<Project Path="tests/Immediate.Cache.Tests/Immediate.Cache.Tests.csproj" />
2123
</Folder>
2224
</Solution>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
5+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
6+
<IsRoslynComponent>true</IsRoslynComponent>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="DotNet.ReproducibleBuilds" PrivateAssets="All" />
11+
<PackageReference Include="Microsoft.Bcl.HashCode" />
12+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="All" />
13+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
14+
<PackageReference Include="MinVer" PrivateAssets="all" />
15+
</ItemGroup>
16+
17+
<PropertyGroup Label="MinVer">
18+
<MinVerAutoIncrement>minor</MinVerAutoIncrement>
19+
<MinVerDefaultPreReleaseIdentifiers>preview.0</MinVerDefaultPreReleaseIdentifiers>
20+
<MinVerTagPrefix>v</MinVerTagPrefix>
21+
</PropertyGroup>
22+
23+
</Project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Collections.Immutable;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
using Microsoft.CodeAnalysis.Diagnostics;
5+
6+
namespace Immediate.Cache.Analyzers;
7+
8+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
9+
public sealed class OwnedDisposableScopeSuppressor : DiagnosticSuppressor
10+
{
11+
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions =>
12+
ImmutableArray.Create([
13+
new SuppressionDescriptor(
14+
id: "OwnedDisposableScopeSuppression",
15+
suppressedDiagnosticId: "CA2000",
16+
justification: "Suppress disposable not being disposed when used from `Owned<T>.GetScope(out T)`."
17+
),
18+
]);
19+
public override void ReportSuppressions(SuppressionAnalysisContext context)
20+
{
21+
var token = context.CancellationToken;
22+
23+
foreach (var diagnostic in context.ReportedDiagnostics)
24+
{
25+
token.ThrowIfCancellationRequested();
26+
27+
var syntaxTree = diagnostic.Location.SourceTree;
28+
29+
if (syntaxTree
30+
?.GetRoot(token)
31+
.FindNode(diagnostic.Location.SourceSpan) is not ArgumentSyntax
32+
{
33+
Parent.Parent: InvocationExpressionSyntax
34+
{
35+
Expression: MemberAccessExpressionSyntax
36+
{
37+
Name: IdentifierNameSyntax
38+
{
39+
Identifier.Text: "GetScope",
40+
},
41+
42+
Expression: { } expression,
43+
},
44+
},
45+
})
46+
{
47+
continue;
48+
}
49+
50+
var typeInfo = context.GetSemanticModel(syntaxTree).GetTypeInfo(expression, token);
51+
var symbol = typeInfo.Type ?? typeInfo.ConvertedType;
52+
53+
if (symbol is not INamedTypeSymbol
54+
{
55+
Arity: 1,
56+
Name: "Owned",
57+
ContainingNamespace:
58+
{
59+
Name: "Cache",
60+
ContainingNamespace:
61+
{
62+
Name: "Immediate",
63+
ContainingNamespace.IsGlobalNamespace: true,
64+
},
65+
},
66+
})
67+
{
68+
continue;
69+
}
70+
71+
context.ReportSuppression(
72+
Suppression.Create(
73+
SupportedSuppressions[0],
74+
diagnostic
75+
)
76+
);
77+
}
78+
}
79+
}

src/Immediate.Cache/Immediate.Cache.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Description>A collection of classes that simplify caching responses from Immediate.Handlers handlers.</Description>
1111

1212
<Authors>Immediate.Cache Developers</Authors>
13-
<Copyright>© 2024 Immediate.Cache Developers</Copyright>
13+
<Copyright>© 2024-2026 Immediate.Cache Developers</Copyright>
1414

1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1616
<PackageReadmeFile>readme.md</PackageReadmeFile>
@@ -29,11 +29,13 @@
2929
</ItemGroup>
3030

3131
<ItemGroup>
32+
<ProjectReference Include="../Immediate.Cache.Analyzers/Immediate.Cache.Analyzers.csproj" ReferenceOutputAssembly="false" />
3233
<ProjectReference Include="../Immediate.Cache.Shared/Immediate.Cache.Shared.csproj" PrivateAssets="All" />
3334
</ItemGroup>
3435

3536
<ItemGroup>
3637
<None Include="../../readme.md" Pack="true" PackagePath="/" />
38+
<None Include="../Immediate.Cache.Analyzers/bin/$(Configuration)/netstandard2.0/Immediate.Cache.Analyzers.dll" Pack="true" PackagePath="analyzers/dotnet/roslyn4.8/cs" Visible="true" />
3739

3840
<TfmSpecificPackageFile
3941
Include="../Immediate.Cache.Shared/bin/$(Configuration)/$(TargetFramework)/Immediate.Cache.Shared.dll"

tests/Immediate.Cache.FunctionalTests/.editorconfig

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net8.0</TargetFrameworks>
5+
<OutputType>Exe</OutputType>
6+
<_SkipUpgradeNetAnalyzersNuGetWarning>true</_SkipUpgradeNetAnalyzersNuGetWarning>
7+
</PropertyGroup>
8+
9+
<ItemGroup Label="Usings">
10+
<Using Include="Xunit" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Basic.Reference.Assemblies.Net80" />
15+
<PackageReference Include="coverlet.collector" PrivateAssets="All" />
16+
<PackageReference Include="coverlet.msbuild" PrivateAssets="All" />
17+
<PackageReference Include="Microsoft.CodeAnalysis.Common" VersionOverride="4.9.2" />
18+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" VersionOverride="4.9.2" />
19+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" />
20+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" VersionOverride="4.9.2" />
21+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
22+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
23+
<PackageReference Include="xunit.v3.mtp-v2" />
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="../../src/Immediate.Cache.Shared/Immediate.Cache.Shared.csproj" />
28+
<ProjectReference Include="../../src/Immediate.Cache.Analyzers/Immediate.Cache.Analyzers.csproj" />
29+
</ItemGroup>
30+
31+
<ItemGroup>
32+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" PrivateAssets="all" GeneratePathProperty="true" />
33+
<Reference Include="$(PkgMicrosoft_CodeAnalysis_NetAnalyzers)\analyzers\dotnet\cs\Microsoft.CodeAnalysis.NetAnalyzers.dll" />
34+
<Reference Include="$(PkgMicrosoft_CodeAnalysis_NetAnalyzers)\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll" />
35+
</ItemGroup>
36+
37+
</Project>

0 commit comments

Comments
 (0)