æ¦è¦
以ä¸ãèªåç¨ã®ã¡ã¢ã§ãããããªã«ä½¿ãå ´é¢ã¯ç¡ãã®ã§ããããã¾ã«ä½¿ãã¨ããããã®ã§ãã¤ãã§ã«ããã«ã¡ã¢ã¡ã¢ãã
Goã§ãã¾ã«åºããã°ã®ä¸ã¤ã«ã·ã£ãã¦ã¤ã³ã°ãããã¾ãã
ãããªã®ã§ãã
package main import "fmt" func main() { i := 0 if alwaysTrue() { i := 1 fmt.Println(i) } fmt.Println(i) } func alwaysTrue() bool { return true }
ä¸ãå®è¡ããã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
$ go version go version go1.19.3 linux/amd64 $ go run main.go 1 0
ãããï¼ã£ã¦ãªããã¤ã§ããã
ãããæ®éã« go vet ããå ´åã¯è¦åãåºåããã¾ããã
$ go vet main.go
ã·ã£ãã¦ã¤ã³ã°ãgo vetã§ãã§ãã¯ãããå ´å㯠shadow ã¨ããã³ãã³ãã追å ã§ã¤ã³ã¹ãã¼ã«ãã¦å®è¡ãã¾ãã
$ go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
ã¤ã³ã¹ãã¼ã«ãããã go vet ã« -vettool
ã¨ãããªãã·ã§ã³ãä»ä¸ãã¦å®è¡ãã¾ãã
$ go vet -vettool=$(command -v shadow) # app ./main.go:8:3: declaration of "i" shadows declaration at line 6
ä»åº¦ã¯æ¤åºããã¾ããã
åèæ å ±
éå»ã®è¨äºã«ã¤ãã¦ã¯ã以ä¸ã®ãã¼ã¸ãããåç §ä¸ããã
ãµã³ãã«ã³ã¼ãã¯ã以ä¸ã®å ´æã§å ¬éãã¦ãã¾ãã