Skip to content

Commit 93475ee

Browse files
committed
BUGFIX IDISP005 when local function
Fix #172.
1 parent 6d3aab9 commit 93475ee

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

IDisposableAnalyzers.Test/IDISP005ReturnTypeShouldBeIDisposableTests/Valid.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,5 +972,33 @@ public static object M()
972972

973973
RoslynAssert.Valid(Analyzer, Descriptor, DisposableCode, code);
974974
}
975+
976+
[Test]
977+
public static void LocalFactory()
978+
{
979+
var code = @"
980+
namespace N
981+
{
982+
using System;
983+
984+
public class C
985+
{
986+
public static object M()
987+
{
988+
using (var disposable = Create())
989+
{
990+
}
991+
992+
return null;
993+
IDisposable Create()
994+
{
995+
return new Disposable();
996+
}
997+
}
998+
}
999+
}";
1000+
1001+
RoslynAssert.Valid(Analyzer, Descriptor, DisposableCode, code);
1002+
}
9751003
}
9761004
}

IDisposableAnalyzers/Analyzers/ReturnValueAnalyzer.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,15 @@ private static bool IsIgnored(ISymbol symbol)
223223

224224
private static ITypeSymbol? ReturnType(SyntaxNodeAnalysisContext context)
225225
{
226-
var anonymousFunction = context.Node.FirstAncestorOrSelf<AnonymousFunctionExpressionSyntax>();
227-
if (anonymousFunction != null)
226+
if (context.Node.FirstAncestorOrSelf<AnonymousFunctionExpressionSyntax>() is {} lambda)
228227
{
229-
var method = context.SemanticModel.GetSymbolSafe(anonymousFunction, context.CancellationToken) as IMethodSymbol;
228+
var method = context.SemanticModel.GetSymbolSafe(lambda, context.CancellationToken) as IMethodSymbol;
229+
return method?.ReturnType;
230+
}
231+
232+
if (context.Node.TryFirstAncestor(out LocalFunctionStatementSyntax? local))
233+
{
234+
var method = context.SemanticModel.GetDeclaredSymbol(local, context.CancellationToken) as IMethodSymbol;
230235
return method?.ReturnType;
231236
}
232237

0 commit comments

Comments
 (0)