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

IDISP004 False positive when IDisposable struct is used as Enumerator #520

Open
Houtamelo opened this issue Aug 15, 2023 · 2 comments · May be fixed by #574
Open

IDISP004 False positive when IDisposable struct is used as Enumerator #520

Houtamelo opened this issue Aug 15, 2023 · 2 comments · May be fixed by #574

Comments

@Houtamelo
Copy link

If you have a struct that implicitly acts as an Enumerator:

public struct EnumeratorStruct : IDisposable
{
    public int Current { get; }

    public bool MoveNext() => foo;

    public EnumeratorStruct GetEnumerator() => this;

    public void Dispose() => Console.WriteLine("Disposed");
}

If you use EnumeratorStruct as the supplier in a foreach statement, EnumeratorStruct.Dispose() is called but the analyzer still gives the IDISP004 warning.

All examples bellow are non-compliant despite being false-positives.

foreach (int element in new EnumeratorStruct())
{
    ...
}
public static EnumeratorStruct Enumerate(this Foo source) => new EnumeratorStruct(source);


Foo source = new Foo();
foreach (int element in source.Enumerate())
{
    ...
}
EnumeratorStruct enumerator = new EnumeratorStruct();
foreach (int element in enumerator)
{
    ...
}

@Corniel
Copy link

Corniel commented Oct 18, 2024

I can confirm this issue.

@Corniel
Copy link

Corniel commented Oct 18, 2024

I also created a PR to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants