DotNetAnalyzers/IDisposableAnalyzers

IDISP013 does not trigger for using declaration

Closed this issue · 2 comments

Bouke commented

A using statement does throw IDISP013 as expected:

using (var scope = CreateScope())
    return wrapped.HandleAsync(command, token);

However rewriting this to a using declaration does not trigger it, but should:

using var scope = CreateScope();
return wrapped.HandleAsync(command, token);

Failing test:

        [Test]
        public static void UsingDeclaration()
        {
            var code = @"
namespace N
{
    using System.Threading.Tasks;

    public static class C
    {
        public static ValueTask<int> MAsync()
        {
            using var file = System.IO.File.OpenRead(string.Empty);
            return ↓InnerAsync();
        }

        private static async ValueTask<int> InnerAsync()
        {
            await Task.Delay(10).ConfigureAwait(false);
            return 1;
        }
    }
}";
            RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
        }

Great issue, thanks!

Bouke commented

Thank you as well for your work on this analyzer. I've tried 4.0.4. on our codebase and it reported an issue, but (luckily?) it was a false positive: #473.