Skip to content

Commit

Permalink
remove err conditional that was never met
Browse files Browse the repository at this point in the history
gopls correctly pointed out that the err==nil check was never met,
as err was assigned and we returned early when err!=nil.

This was an oversight when I wrote this; when Encode fails,
we shouldn't return, because we still want to close the file.
We don't defer because we want to check the error; explain that.
  • Loading branch information
mvdan authored and pagran committed Mar 8, 2024
1 parent d2beda1 commit 52d436d
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ func writeGobExclusive(name string, val any) error {
if err != nil {
return err
}
if err := gob.NewEncoder(f).Encode(val); err != nil {
return err
}
// Always close the file, and return the first error we get.
err = gob.NewEncoder(f).Encode(val)
if err2 := f.Close(); err == nil {
err = err2
}
Expand Down

0 comments on commit 52d436d

Please sign in to comment.