Skip to content

Commit

Permalink
SA5001: clarify message
Browse files Browse the repository at this point in the history
Clarify the message by putting the function name in there.

Closes: dominikhgh-1489
Closes: dominikhgh-1554 [via git-merge-pr]
  • Loading branch information
arp242 authored and dominikh committed Jun 1, 2024
1 parent 871dee4 commit d39a04f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion staticcheck/sa5001/sa5001.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ func run(pass *analysis.Pass) (interface{}, error) {
if sel.Sel.Name != "Close" {
continue
}
report.Report(pass, def, fmt.Sprintf("should check returned error before deferring %s", report.Render(pass, def.Call)))
report.Report(pass, def, fmt.Sprintf("should check error returned from %s() before deferring %s",
report.Render(pass, call.Fun), report.Render(pass, def.Call)))
}
}
code.Preorder(pass, fn, (*ast.BlockStmt)(nil))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pkg

import "io"
import (
"io"
"os"
)

func fn1() (io.ReadCloser, error) {
return nil, nil
Expand All @@ -10,13 +13,19 @@ type T struct {
rc io.ReadCloser
}

type T2 struct{}

func (T2) create() (io.ReadCloser, error) {
return nil, nil
}

func fn3() (T, error) {
return T{}, nil
}

func fn2() {
rc, err := fn1()
defer rc.Close() //@ diag(`should check returned error before deferring rc.Close`)
defer rc.Close() //@ diag(`should check error returned from fn1() before deferring rc.Close`)
if err != nil {
println()
}
Expand All @@ -31,7 +40,20 @@ func fn2() {
defer rc.Close()

t, err := fn3()
defer t.rc.Close() //@ diag(`should check returned error before deferring t.rc.Close`)
defer t.rc.Close() //@ diag(`should check error returned from fn3() before deferring t.rc.Close`)
if err != nil {
println()
}

fp, err := os.Open("path")
defer fp.Close() //@ diag(`should check error returned from os.Open() before deferring fp.Close()`)
if err != nil {
println()
}

var t2 T2
rc2, err := t2.create()
defer rc2.Close() //@ diag(`should check error returned from t2.create() before deferring rc2.Close()`)
if err != nil {
println()
}
Expand Down

0 comments on commit d39a04f

Please sign in to comment.