From c4b114a3e22f8e2b147d85a6d95bcf10d5249c14 Mon Sep 17 00:00:00 2001
From: Henrik Feldt <henrik@haf.se>
Date: Sun, 20 Oct 2019 19:06:19 +0200
Subject: [PATCH] Fixes #341

---
 Expecto.Tests/Bug341.fs            | 18 ++++++++++++++++++
 Expecto.Tests/Expecto.Tests.fsproj |  1 +
 Expecto/Expecto.fs                 |  6 +++---
 3 files changed, 22 insertions(+), 3 deletions(-)
 create mode 100644 Expecto.Tests/Bug341.fs

diff --git a/Expecto.Tests/Bug341.fs b/Expecto.Tests/Bug341.fs
new file mode 100644
index 00000000..4903e42b
--- /dev/null
+++ b/Expecto.Tests/Bug341.fs
@@ -0,0 +1,18 @@
+module Expecto.Bug341Test
+open Expecto
+
+type TestDir =
+  { Dir: string }
+  interface System.IDisposable with
+    member x.Dispose() = ()
+
+let example () = { Dir = "" }
+
+[<Tests>]
+let tests =
+  testList "Tests" [
+    test "test" {
+      use _ = example () // <- FS0001
+      ()
+    }
+  ]
diff --git a/Expecto.Tests/Expecto.Tests.fsproj b/Expecto.Tests/Expecto.Tests.fsproj
index ed856893..7327b91c 100644
--- a/Expecto.Tests/Expecto.Tests.fsproj
+++ b/Expecto.Tests/Expecto.Tests.fsproj
@@ -12,6 +12,7 @@
     <Compile Include="FocusedTests.fs" />
     <Compile Include="FsCheckTests.fs" />
     <Compile Include="PerformanceTests.fs" />
+    <Compile Include="Bug341.fs" />
     <Compile Include="Main.fs" />
     <None Include="paket.references" />
     <ProjectReference Include="..\Expecto.Hopac\Expecto.Hopac.fsproj" />
diff --git a/Expecto/Expecto.fs b/Expecto/Expecto.fs
index 729e09ee..6fdeee93 100644
--- a/Expecto/Expecto.fs
+++ b/Expecto/Expecto.fs
@@ -1304,9 +1304,9 @@ module Tests =
       try
         f disposable
       finally
-        match disposable with
-        | null -> ()
-        | disp -> disp.Dispose()
+        match box disposable with
+        | :? IDisposable as d when not (isNull d) -> d.Dispose()
+        | _ -> ()
     member __.For(sequence, f) =
       for i in sequence do f i
     member __.Combine(f1, f2) = f2(); f1