Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FP IDISP004: All IEnumerables are disposed after executing foreach statements #574

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
namespace IDisposableAnalyzers.Test.IDISP004DoNotIgnoreCreatedTests;

using Gu.Roslyn.Asserts;
using NUnit.Framework;

public static partial class Valid
{
[Test]
public static void StructEnumeratorCreatedWithinForEach()
{
var code = @"
using System.Collections;
using System.Collections.Generic;

namespace N
{
public class C
{
public void M()
{
foreach(var n in new Enumerator())
{
}
}
}

public struct Enumerator : IEnumerator<int>, IEnumerable<int>
{
public int Current => 42;
object IEnumerator.Current => 42;
public void Dispose() { }
public bool MoveNext() => true;
public void Reset() { }
public IEnumerator<int> GetEnumerator() => this;
IEnumerator IEnumerable.GetEnumerator() => this;
}
}";
RoslynAssert.Valid(Analyzer, code);
}

[Test]
public static void ClassEnumeratorCreatedWithinForEach()
{
var code = @"
using System.Collections;
using System.Collections.Generic;

namespace N
{
public class C
{
public void M()
{
foreach(var n in new Enumerator())
{
}
}
}

public class Enumerator : IEnumerator<int>, IEnumerable<int>
{
public int Current => 42;
object IEnumerator.Current => 42;
public void Dispose() { }
public bool MoveNext() => true;
public void Reset() { }
public IEnumerator<int> GetEnumerator() => this;
IEnumerator IEnumerable.GetEnumerator() => this;
}
}";
RoslynAssert.Valid(Analyzer, code);
}

[Test]
public static void CreatedEnumerableWithinForEach()
{
var code = @"
using System.Collections;
using System.Collections.Generic;

namespace N
{
public class C
{
public void M()
{
foreach(var n in All())
{
}
}

public IEnumerable<int> All() => new Enumerator();
}

public class Enumerator : IEnumerator<int>, IEnumerable<int>
{
public int Current => 42;
object IEnumerator.Current => 42;
public void Dispose() { }
public bool MoveNext() => true;
public void Reset() { }
public IEnumerator<int> GetEnumerator() => this;
IEnumerator IEnumerable.GetEnumerator() => this;
}
}";
RoslynAssert.Valid(Analyzer, code);
}
}
3 changes: 3 additions & 0 deletions IDisposableAnalyzers/Helpers/Disposable.Disposes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ private static bool Disposes(ExpressionSyntax candidate, Recursion recursion)
when Identity(candidate, recursion) is { } id &&
Disposes(id, recursion)
=> true,
{ Parent: ForEachStatementSyntax parent }
when parent.Expression == candidate
=> true,
{ Parent: ConditionalAccessExpressionSyntax { WhenNotNull: InvocationExpressionSyntax invocation } }
=> IsDisposeOrReturnValueDisposed(invocation),
{ Parent: MemberAccessExpressionSyntax { Parent: InvocationExpressionSyntax invocation } }
Expand Down