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工具!