Skip to content

Commit 75db225

Browse files
authored
Accept DeclarationPattern as irrefutable (#14)
Allows a DeclarationPatternSyntax when switching against a Closed class
1 parent 864077e commit 75db225

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/StaticCs.Analyzers/ClosedTypeCompletenessSuppressor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public override void ReportSuppressions(SuppressionAnalysisContext context)
7171
subTypes.Remove(namedType);
7272
}
7373
}
74+
else if (arm.Pattern is DeclarationPatternSyntax { Type: { } patternSyntax })
75+
{
76+
var symbolInfo = model.GetSymbolInfo(patternSyntax);
77+
if (symbolInfo.Symbol is INamedTypeSymbol namedType)
78+
{
79+
subTypes.Remove(namedType);
80+
}
81+
}
7482
else if (arm.Pattern is RecursivePatternSyntax
7583
{
7684
Type: { } patternTypeSyntax,

src/StaticCs.Analyzers/StaticCs.Analyzers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<PropertyGroup>
1010
<PackageId>StaticCS</PackageId>
11-
<Version>0.3.0</Version>
11+
<Version>0.3.1</Version>
1212
<IsPackable>true</IsPackable>
1313
<PackageOutputPath>$(ArtifactsPath)pack</PackageOutputPath>
1414
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>

test/ClosedTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,31 @@ await VerifyDiagnostics<ClosedTypeCompletenessSuppressor>(src,
272272
DiagnosticResult.CompilerWarning("CS8509").WithSpan(10, 31, 10, 37).WithArguments("Option<int>.Some(0) { }"));
273273
}
274274

275+
[Fact]
276+
public async Task ClosedRecordTypeTestWithVariable()
277+
{
278+
var src = """
279+
[StaticCs.Closed]
280+
abstract record Base
281+
{
282+
private Base() { }
283+
public record A : Base;
284+
public record B : Base;
285+
}
286+
class C
287+
{
288+
int M(Base b) => b switch
289+
{
290+
Base.A a => a.GetHashCode(),
291+
Base.B => 1,
292+
};
293+
}
294+
""";
295+
await VerifyDiagnostics<ClosedTypeCompletenessSuppressor>(src,
296+
// /0/Test0.cs(10,24): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '_' is not covered.
297+
DiagnosticResult.CompilerWarning("CS8509").WithSpan(10, 24, 10, 30).WithArguments("_").WithIsSuppressed(true));
298+
}
299+
275300
private static readonly DiagnosticResult ClosedEnumConversion = CSharpAnalyzerVerifier<EnumClosedConversionAnalyzer, XUnitVerifier>.Diagnostic(DiagId.ClosedEnumConversion.ToIdString());
276301
private static readonly DiagnosticResult ClassOrRecordMustBeClosed = CSharpAnalyzerVerifier<ClosedDeclarationChecker, XUnitVerifier>.Diagnostic(DiagId.ClassOrRecordMustBeClosed.ToIdString());
277302

0 commit comments

Comments
 (0)