Skip to content

Commit e536de4

Browse files
committed
generalize AmOptional003 to switch expressions (close #44)
1 parent 0b2957f commit e536de4

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<SupportedFrameworks>net10.0</SupportedFrameworks>
44
<DotNetVersion>net10.0</DotNetVersion>
5-
<PackVersion>0.4.1</PackVersion>
5+
<PackVersion>0.4.2</PackVersion>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
88
</Project>

analyzer/Ametrin.Optional.Analyzer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</PropertyGroup>
2626

2727
<ItemGroup>
28-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" PrivateAssets="all" />
28+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.3.0" PrivateAssets="all" />
2929
</ItemGroup>
3030

3131
<ItemGroup>

analyzer/AmetrinOptionalAnalyzer.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static readonly DiagnosticDescriptor UnnecessaryRequire
2020
= new(id: "AmOptional002", title: "Unnecessary Require call", messageFormat: "type already is {0}", category: "Usage", DiagnosticSeverity.Warning, isEnabledByDefault: true);
2121

2222
public static readonly DiagnosticDescriptor WrongConditionalType
23-
= new(id: "AmOptional003", title: "Wrong conditional return type", messageFormat: "default means {0} instead of {1}", category: "Usage", DiagnosticSeverity.Info, isEnabledByDefault: true);
23+
= new(id: "AmOptional003", title: "Potentially wrong return type", messageFormat: "default means {0} instead of {1}", category: "Usage", DiagnosticSeverity.Info, isEnabledByDefault: true);
2424

2525
public static readonly DiagnosticDescriptor ImpossibleAs
2626
= new(id: "AmOptional004", title: "Impossible As call", messageFormat: "Cannot safely up-cast {0} to {1}. Use Require<T>.", category: "Usage", DiagnosticSeverity.Error, isEnabledByDefault: true);
@@ -195,18 +195,28 @@ public override void Initialize(AnalysisContext context)
195195

196196
if (conversion.Type is not INamedTypeSymbol target || !IsOptionalType(target)) return;
197197

198-
if (conversion.Operand is not IConditionalOperation condition || IsOptionalType(condition.Type!)) return;
199-
200-
if (IsDefaultLiteral(condition.WhenTrue))
198+
if (conversion.Operand is IConditionalOperation condition && !IsOptionalType(condition.Type!))
201199
{
202-
context.ReportDiagnostic(Diagnostic.Create(WrongConditionalType, condition.WhenTrue.Syntax.GetLocation(), condition.Type!.ToDisplayString(), conversion.Type.ToDisplayString()));
203-
}
200+
if (IsDefaultLiteral(condition.WhenTrue))
201+
{
202+
context.ReportDiagnostic(Diagnostic.Create(WrongConditionalType, condition.WhenTrue.Syntax.GetLocation(), condition.Type!.ToDisplayString(), conversion.Type.ToDisplayString()));
203+
}
204204

205-
if (condition.WhenFalse is not null && IsDefaultLiteral(condition.WhenFalse))
205+
if (condition.WhenFalse is not null && IsDefaultLiteral(condition.WhenFalse))
206+
{
207+
context.ReportDiagnostic(Diagnostic.Create(WrongConditionalType, condition.WhenFalse.Syntax.GetLocation(), condition.Type!.ToDisplayString(), conversion.Type.ToDisplayString()));
208+
}
209+
}
210+
else if (conversion.Operand is ISwitchExpressionOperation switchExpression && !IsOptionalType(switchExpression.Type!))
206211
{
207-
context.ReportDiagnostic(Diagnostic.Create(WrongConditionalType, condition.WhenFalse.Syntax.GetLocation(), condition.Type!.ToDisplayString(), conversion.Type.ToDisplayString()));
212+
foreach (var arm in switchExpression.Arms)
213+
{
214+
if (IsDefaultLiteral(arm.Value))
215+
{
216+
context.ReportDiagnostic(Diagnostic.Create(WrongConditionalType, arm.Value.Syntax.GetLocation(), switchExpression.Type!.ToDisplayString(), conversion.Type.ToDisplayString()));
217+
}
218+
}
208219
}
209-
210220
}, OperationKind.Conversion);
211221

212222
context.RegisterOperationAction(static context =>

generator/Ametrin.Optional.Generator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" />
17+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.3.0" />
1818
</ItemGroup>
1919

2020
</Project>

0 commit comments

Comments
 (0)