Skip to content

Commit e85c1ca

Browse files
committed
Handle switch expression.
1 parent b9ba1fd commit e85c1ca

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

IDisposableAnalyzers.Test/IDISP001DisposeCreatedTests/Diagnostics.Local.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void Dispose()
3333
[TestCase("null ?? System.IO.File.OpenRead(string.Empty)")]
3434
[TestCase("true ? null : System.IO.File.OpenRead(string.Empty)")]
3535
[TestCase("true ? System.IO.File.OpenRead(string.Empty) : null")]
36+
[TestCase("o switch { int _ => File.OpenRead(string.Empty), _ => null }")]
3637
public static void LanguageConstructs(string expression)
3738
{
3839
var code = @"
@@ -41,9 +42,9 @@ namespace N
4142
using System;
4243
using System.IO;
4344
44-
internal class C
45+
class C
4546
{
46-
internal C()
47+
void M(object o)
4748
{
4849
↓var value = new Disposable();
4950
}

IDisposableAnalyzers/Helpers/Pooled/RecursiveValues.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ private bool AddRecursiveValues(ExpressionSyntax assignedValue)
135135
var whenTrue = this.AddRecursiveValues(conditional.WhenTrue);
136136
var whenFalse = this.AddRecursiveValues(conditional.WhenFalse);
137137
return whenTrue || whenFalse;
138+
case SwitchExpressionSyntax { Arms: { } arms }:
139+
var added = false;
140+
foreach (var arm in arms)
141+
{
142+
added |= this.AddRecursiveValues(arm.Expression);
143+
}
144+
145+
return added;
138146
case AwaitExpressionSyntax awaitExpression:
139147
using (var walker = ReturnValueWalker.Borrow(awaitExpression, ReturnValueSearch.RecursiveInside, this.semanticModel, this.cancellationToken))
140148
{

0 commit comments

Comments
 (0)