DotNetAnalyzers/IDisposableAnalyzers

IDISP004 triggered in unit test that should throw

lonix1 opened this issue · 2 comments

Consider this standard code in a unit test:

Action act = () => _ = new SomethingDisposable("foo");          // IDISP004
act.Should().Throw<InvalidOperationException>();

That triggers IDISP004 - "Don't ignore created IDisposable".

Technically that is correct. But it is a major inconvenience to suppress this, because it's so common.

Is there a "nice" way to deal with this?

I'll special case it so we don't get tests full of noise.

Thanks for reporting!

If that's the only way, then we should give it more thought to avoid unnecessary work! 😃 Because there are various cases where you find that problem, e.g.:

Action action = () => ...
Func<T> func = () => ...
Func<Task> func = () => ...

etc. Maybe there are more?