Skip to content

Commit 6d3aab9

Browse files
committed
BUGFIX IDISP004 when identity function.
Fix #174
1 parent acb8d64 commit 6d3aab9

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

IDisposableAnalyzers.Test/IDISP004DoNotIgnoreCreatedTests/Valid.ExtensionMethod.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace IDisposableAnalyzers.Test.IDISP004DoNotIgnoreCreatedTests
1+
namespace IDisposableAnalyzers.Test.IDISP004DoNotIgnoreCreatedTests
22
{
33
using Gu.Roslyn.Asserts;
44
using NUnit.Framework;
@@ -222,5 +222,44 @@ public void Dispose()
222222
}";
223223
RoslynAssert.Valid(Analyzer, code, extCode, DisposableCode, wrappingDisposableCode);
224224
}
225+
226+
[Test]
227+
public static void Issue174()
228+
{
229+
var code = @"
230+
namespace Gu.Inject.Tests
231+
{
232+
using System;
233+
using NUnit.Framework;
234+
235+
public class Sandbox
236+
{
237+
[Test]
238+
public void M()
239+
{
240+
using (var kernel = new Kernel().AutoBind<int>())
241+
{
242+
}
243+
}
244+
}
245+
246+
public class Kernel : IDisposable
247+
{
248+
public void Dispose()
249+
{
250+
}
251+
}
252+
253+
public static class KernelExtensions
254+
{
255+
public static Kernel AutoBind<T>(this Kernel kernel)
256+
{
257+
return kernel;
258+
}
259+
}
260+
}";
261+
262+
RoslynAssert.Valid(Analyzer, code);
263+
}
225264
}
226265
}

IDisposableAnalyzers/Helpers/Walkers/DisposableWalker.Disposes.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Threading;
44
using Gu.Roslyn.AnalyzerExtensions;
55
using Microsoft.CodeAnalysis;
6-
using Microsoft.CodeAnalysis.CSharp;
76
using Microsoft.CodeAnalysis.CSharp.Syntax;
87

98
internal sealed partial class DisposableWalker
@@ -96,8 +95,7 @@ internal static bool Disposes(ILocalSymbol local, SemanticModel semanticModel, C
9695
switch (declaration)
9796
{
9897
case { Parent: VariableDeclarationSyntax { Parent: UsingStatementSyntax _ } }:
99-
case { Parent: VariableDeclarationSyntax { Parent: LocalDeclarationStatementSyntax { UsingKeyword: var usingKeywod } } }
100-
when usingKeywod.IsKind(SyntaxKind.UsingKeyword):
98+
case { Parent: VariableDeclarationSyntax { Parent: LocalDeclarationStatementSyntax { UsingKeyword: { ValueText: "using" } } } }:
10199
return true;
102100
}
103101
}
@@ -140,7 +138,7 @@ private static bool Disposes(ExpressionSyntax candidate, Recursion recursion)
140138
{
141139
{ Parent: UsingStatementSyntax _ }
142140
=> true,
143-
{ Parent: EqualsValueClauseSyntax { Parent: VariableDeclaratorSyntax { Parent: UsingStatementSyntax _ } } }
141+
{ Parent: EqualsValueClauseSyntax { Parent: VariableDeclaratorSyntax { Parent: VariableDeclarationSyntax { Parent: UsingStatementSyntax _ } } } }
144142
=> true,
145143
{ Parent: EqualsValueClauseSyntax { Parent: VariableDeclaratorSyntax { Parent: VariableDeclarationSyntax { Parent: LocalDeclarationStatementSyntax { UsingKeyword: { ValueText: "using" } } } } } }
146144
=> true,

0 commit comments

Comments
 (0)