Skip to content

Commit a1dee8b

Browse files
committed
add experimental analyzer
1 parent 4f61661 commit a1dee8b

9 files changed

+115
-1491
lines changed

UniTask.NetCore.sln

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29613.14
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31606.5
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniTask.NetCoreTests", "src\UniTask.NetCoreTests\UniTask.NetCoreTests.csproj", "{B3E311A4-70D8-4131-9965-C073A99D201A}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UniTask.NetCoreTests", "src\UniTask.NetCoreTests\UniTask.NetCoreTests.csproj", "{B3E311A4-70D8-4131-9965-C073A99D201A}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UniTask.NetCore", "src\UniTask.NetCore\UniTask.NetCore.csproj", "{16EE20D0-7FB1-483A-8467-A5EEDBF1F5BF}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniTask.NetCoreSandbox", "src\UniTask.NetCoreSandbox\UniTask.NetCoreSandbox.csproj", "{3915E72E-33E0-4A14-A6D8-872702200E58}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UniTask.NetCoreSandbox", "src\UniTask.NetCoreSandbox\UniTask.NetCoreSandbox.csproj", "{3915E72E-33E0-4A14-A6D8-872702200E58}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniTask.Analyzer", "src\UniTask.Analyzer\UniTask.Analyzer.csproj", "{0AC6F052-A255-4EE3-9E05-1C02D49AB1C2}"
1113
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,6 +29,10 @@ Global
2729
{3915E72E-33E0-4A14-A6D8-872702200E58}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{3915E72E-33E0-4A14-A6D8-872702200E58}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{3915E72E-33E0-4A14-A6D8-872702200E58}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{0AC6F052-A255-4EE3-9E05-1C02D49AB1C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{0AC6F052-A255-4EE3-9E05-1C02D49AB1C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{0AC6F052-A255-4EE3-9E05-1C02D49AB1C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{0AC6F052-A255-4EE3-9E05-1C02D49AB1C2}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"UniTask.Analyzer": {
4+
"commandName": "DebugRoslynComponent",
5+
"targetProject": "..\\UniTask.NetCoreSandbox\\UniTask.NetCoreSandbox.csproj"
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>library</OutputType>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<Nullable>enable</Nullable>
7+
<IsRoslynComponent>true</IsRoslynComponent>
8+
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);PackBuildOutputs</TargetsForTfmSpecificContentInPackage>
9+
<IncludeBuildOutput>false</IncludeBuildOutput>
10+
<IncludeSymbols>false</IncludeSymbols>
11+
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
12+
<DevelopmentDependency>true</DevelopmentDependency>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
18+
<PrivateAssets>all</PrivateAssets>
19+
</PackageReference>
20+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
21+
</ItemGroup>
22+
23+
<Target Name="PackBuildOutputs" DependsOnTargets="SatelliteDllsProjectOutputGroup;DebugSymbolsProjectOutputGroup">
24+
<ItemGroup>
25+
<TfmSpecificPackageFile Include="$(TargetDir)\*.dll" PackagePath="analyzers\dotnet\cs" />
26+
<TfmSpecificPackageFile Include="@(SatelliteDllsProjectOutputGroupOutput->'%(FinalOutputPath)')" PackagePath="analyzers\dotnet\cs\%(SatelliteDllsProjectOutputGroupOutput.Culture)\" />
27+
</ItemGroup>
28+
</Target>
29+
</Project>
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma warning disable RS2008
2+
3+
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.Diagnostics;
5+
using Microsoft.CodeAnalysis.Operations;
6+
using System.Collections.Immutable;
7+
using System.Threading;
8+
9+
namespace UniTask.Analyzer
10+
{
11+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
12+
public class UniTaskAnalyzer : DiagnosticAnalyzer
13+
{
14+
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
15+
id: "UNITASK001",
16+
title: "UniTaskAnalyzer001: Must pass CancellationToken",
17+
messageFormat: "Must pass CancellationToken",
18+
category: "Usage",
19+
defaultSeverity: DiagnosticSeverity.Error,
20+
isEnabledByDefault: true,
21+
description: "Pass CancellationToken or CancellationToken.None.");
22+
23+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
24+
25+
public override void Initialize(AnalysisContext context)
26+
{
27+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
28+
context.EnableConcurrentExecution();
29+
30+
context.RegisterOperationAction(AnalyzeSymbol, OperationKind.Invocation);
31+
}
32+
33+
private static void AnalyzeSymbol(OperationAnalysisContext context)
34+
{
35+
var token = context.Compilation.GetTypeByMetadataName(typeof(CancellationToken).FullName);
36+
if (token == null) return;
37+
38+
if (context.Operation is IInvocationOperation invocation)
39+
{
40+
foreach (var arg in invocation.Arguments)
41+
{
42+
if (arg.ArgumentKind == ArgumentKind.DefaultValue)
43+
{
44+
if (SymbolEqualityComparer.Default.Equals(arg.Parameter.Type, token))
45+
{
46+
var diagnostic = Diagnostic.Create(Rule, arg.Syntax.GetLocation());
47+
context.ReportDiagnostic(diagnostic);
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}

src/UniTask.NetCoreSandbox/AllocationCheck.cs

-258
This file was deleted.

0 commit comments

Comments
 (0)