Skip to content

Commit 15c7564

Browse files
committed
IDISP003 don't warn when assigned with injected.
IDISP008 is the correct warning. #185
1 parent 15de5cf commit 15c7564

3 files changed

Lines changed: 71 additions & 11 deletions

File tree

IDisposableAnalyzers.Test/IDISP003DisposeBeforeReassigningTests/Valid.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,5 +1354,58 @@ public static C M(IDisposable d)
13541354

13551355
RoslynAssert.Valid(Analyzer, code);
13561356
}
1357+
1358+
[Test]
1359+
public static void Issue185()
1360+
{
1361+
var c1 = @"
1362+
namespace N
1363+
{
1364+
using System;
1365+
1366+
public class C1 : IDisposable
1367+
{
1368+
public void Dispose()
1369+
{
1370+
}
1371+
}
1372+
}";
1373+
var code = @"
1374+
namespace N
1375+
{
1376+
using System;
1377+
1378+
public class C
1379+
{
1380+
C1 _c1;
1381+
1382+
public C() { }
1383+
1384+
public C(C1 c1)
1385+
{
1386+
_c1 = c1;
1387+
}
1388+
1389+
public C1 M1() => _c1;
1390+
1391+
public C1 M2() => _c1 = new C1();
1392+
1393+
public C1 GetOrCreateFoo()
1394+
{
1395+
var c1 = this.M1();
1396+
if (c1 != null)
1397+
return c1;
1398+
1399+
c1 = M2();
1400+
if (c1 != null)
1401+
return c1;
1402+
1403+
throw new Exception();
1404+
}
1405+
}
1406+
}";
1407+
1408+
RoslynAssert.Valid(Analyzer, c1, code);
1409+
}
13571410
}
13581411
}

IDisposableAnalyzers/Analyzers/AssignmentAnalyzer.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,18 @@ private static bool IsReassignedWithCreated(AssignmentExpressionSyntax assignmen
8080
return false;
8181
}
8282

83-
if (FieldOrProperty.TryCreate(assignedSymbol, out var fieldOrProperty) &&
84-
context.Node.FirstAncestor<TypeDeclarationSyntax>() is { } containingType &&
85-
TestFixture.IsAssignedAndDisposedInSetupAndTearDown(fieldOrProperty, containingType, context.SemanticModel, context.CancellationToken))
83+
if (FieldOrProperty.TryCreate(assignedSymbol, out var fieldOrProperty))
8684
{
87-
return false;
85+
if (context.Node.FirstAncestor<TypeDeclarationSyntax>() is { } containingType &&
86+
TestFixture.IsAssignedAndDisposedInSetupAndTearDown(fieldOrProperty, containingType, context.SemanticModel, context.CancellationToken))
87+
{
88+
return false;
89+
}
90+
91+
if (Disposable.IsAssignedWithInjected(fieldOrProperty.Symbol, assignment, context.SemanticModel, context.CancellationToken))
92+
{
93+
return false;
94+
}
8895
}
8996

9097
if (IsNullChecked(assignedSymbol, assignment, context.SemanticModel, context.CancellationToken))

IDisposableAnalyzers/Helpers/Disposable.IsInjected.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ internal static Result IsAnyCachedOrInjected(RecursiveValues values, SemanticMod
9090
return result;
9191
}
9292

93+
internal static bool IsAssignedWithInjected(ISymbol symbol, ExpressionSyntax location, SemanticModel semanticModel, CancellationToken cancellationToken)
94+
{
95+
using var assignedValues = AssignedValueWalker.Borrow(symbol, location, semanticModel, cancellationToken);
96+
using var recursive = RecursiveValues.Borrow(assignedValues, semanticModel, cancellationToken);
97+
return IsAnyCachedOrInjected(recursive, semanticModel, cancellationToken).IsEither(Result.Yes, Result.AssumeYes);
98+
}
99+
93100
private static Result IsInjectedCore(ISymbol symbol)
94101
{
95102
if (symbol == null)
@@ -149,12 +156,5 @@ private static Result IsInjectedCore(ISymbol symbol)
149156

150157
return Result.No;
151158
}
152-
153-
private static bool IsAssignedWithInjected(ISymbol symbol, ExpressionSyntax location, SemanticModel semanticModel, CancellationToken cancellationToken)
154-
{
155-
using var assignedValues = AssignedValueWalker.Borrow(symbol, location, semanticModel, cancellationToken);
156-
using var recursive = RecursiveValues.Borrow(assignedValues, semanticModel, cancellationToken);
157-
return IsAnyCachedOrInjected(recursive, semanticModel, cancellationToken).IsEither(Result.Yes, Result.AssumeYes);
158-
}
159159
}
160160
}

0 commit comments

Comments
 (0)