nakedret
v2.0.3
Nakedret是一個GO靜態分析工具,可在功能中找到大於指定函數長度的裸返回。
安裝Nakedret通過GO安裝:
go install github.com/alexkohler/nakedret/v2/cmd/nakedret @ latest如果您尚未將GOPATH/bin目錄添加到PATH環境變量中,則需要這樣做。
Windows(CMD):
set PATH = %PATH% ;C:yourGOPATHbinbash(您可以驗證已設置路徑):
# Check if nakedret is on PATH
which nakedret
export PATH= $PATH :/your/GOPATH/bin # to set path if it does not exist 類似於其他GO靜態Anaylsis工具(例如golint , go vet ),Nakedret可以使用一個或多個以其導入路徑命名的文件名,目錄或軟件包調用。 Nakedret還支持...通配符。
nakedret [flags] files/directories/packages
當前,唯一支持的標誌是-l,它是一個可選的數字標誌,用於指定函數可以是最大長度(按線長度)。如果未指定,則默認為5。
也可以使用go vet運行:
go vet -vettool= $( which nakedret ) ./...如GO的代碼評論評論中所述:
如果功能是少數線路,則裸返回是可以的。一旦它是一個中等大小的函數,請使用返回值明確。推論:僅僅因為它使您可以使用裸返回而命名結果參數是不值得的。文檔的清晰度總是比在功能中保存一兩行更重要。
該工具旨在收集非平凡功能的裸露回報。
讓我們以GO源中的types軟件包為例:
$ nakedret -l 25 types/
types/check.go:245 checkFiles naked returns on 26 line function
types/typexpr.go:443 collectParams naked returns on 53 line function
types/stmt.go:275 caseTypes naked returns on 27 line function
types/lookup.go:275 MissingMethod naked returns on 39 line function下面是nakedret在types/lookup.go中裸返回的不太直觀用途之一(nakedret將返回功能中最後一個裸返回的行號):
func MissingMethod ( V Type , T * Interface , static bool ) ( method * Func , wrongType bool ) {
// fast path for common case
if T . Empty () {
return
}
// TODO(gri) Consider using method sets here. Might be more efficient.
if ityp , _ := V . Underlying ().( * Interface ); ityp != nil {
// TODO(gri) allMethods is sorted - can do this more efficiently
for _ , m := range T . allMethods {
_ , obj := lookupMethod ( ityp . allMethods , m . pkg , m . name )
switch {
case obj == nil :
if static {
return m , false
}
case ! Identical ( obj . Type (), m . typ ):
return m , true
}
}
return
}
// A concrete type implements T if it implements all methods of T.
for _ , m := range T . allMethods {
obj , _ , _ := lookupFieldOrMethod ( V , false , m . pkg , m . name )
f , _ := obj .( * Func )
if f == nil {
return m , false
}
if ! Identical ( f . typ , m . typ ) {
return m , true
}
}
return
}build.Context.UseAllFiles的build.context.useallfiles可能對某些人有用。歡迎拉請求!
如果您喜歡Nakedret,請看一下我的其他靜態Anaylsis工具!