StaticcheckGo 語(yǔ)言靜態(tài)代碼分析
Staticcheck 是一個(gè)開源的 Go 語(yǔ)言靜態(tài)代碼分析工具。
例如用來(lái)分析無(wú)用的變量:
package main
import (
"errors"
"fmt"
"log"
)
type Result struct {
Entries []string
}
func Query() (Result, error) {
return Result{
Entries: []string{},
}, nil
}
func ResultEntries() (Result, error) {
err := errors.New("no entries found")
result, err := Query()
if err != nil {
return Result{}, err
}
if len(result.Entries) == 0 {
return Result{}, err
}
return result, nil
}
func main() {
result, err := ResultEntries()
if err != nil {
log.Fatal(err)
}
fmt.Printf("result=%v, err=%v", result, err)
}
分析結(jié)果:
$ staticcheck main.go
main.go:20:2: this value of err is never used (SA4006)
main.go:20:19: New is a pure function but its return value is ignored (SA4017)
評(píng)論
圖片
表情
