From 8bf3f8b71f47b157e2c4eb8e9212866dff322194 Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Fri, 1 Jul 2016 10:44:25 -0500 Subject: [PATCH] Immediately check for errors on fs.Open Found with github.com/dominikh/go-staticcheck --- afero_test.go | 6 +++--- util.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/afero_test.go b/afero_test.go index 26e23cd..fee91f9 100644 --- a/afero_test.go +++ b/afero_test.go @@ -394,13 +394,13 @@ func TestWriteAt(t *testing.T) { } f2, err := fs.Open(f.Name()) + if err != nil { + t.Fatalf("%v: ReadFile %s: %v", fs.Name(), f.Name(), err) + } defer f2.Close() buf := new(bytes.Buffer) buf.ReadFrom(f2) b := buf.Bytes() - if err != nil { - t.Fatalf("%v: ReadFile %s: %v", fs.Name(), f.Name(), err) - } if string(b) != "hello, WORLD\n" { t.Fatalf("after write: have %q want %q", string(b), "hello, WORLD\n") } diff --git a/util.go b/util.go index 2fa8402..84030c2 100644 --- a/util.go +++ b/util.go @@ -295,10 +295,10 @@ func IsEmpty(fs Fs, path string) (bool, error) { } if fi.IsDir() { f, err := fs.Open(path) - defer f.Close() if err != nil { return false, err } + defer f.Close() list, err := f.Readdir(-1) return len(list) == 0, nil }