Nakedret은 지정된 기능 길이보다 큰 함수에서 Naked 리턴을 찾는 GO 정적 분석 도구입니다.
GO 설치를 통해 Nakedret 설치 :
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의 코드 검토에 언급 된 바와 같이 의견 :
기능이 소수의 선인 경우 알몸 반환은 괜찮습니다. 중간 크기의 기능이면 반환 값을 명시하십시오. COOROLLARY : Naked Returns를 사용할 수 있기 때문에 결과 매개 변수 이름을 지정하는 것은 가치가 없습니다. 문서의 명확성은 기능에 한 줄을 저장하는 것보다 항상 더 중요합니다.
이 도구는 사소한 기능에 대한 알몸 수익을 포착하는 것을 목표로합니다.
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 의 토글링 지원은 일부에게 유용 할 수 있습니다.요청을 당기는 것을 환영합니다!
Nakedret을 즐겼다면 다른 정적 Anaylsis 도구를 살펴보십시오!