-
Notifications
You must be signed in to change notification settings - Fork 0
/
shush.go
56 lines (50 loc) · 1.61 KB
/
shush.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Package shush defines an Analyzer that reports usage of `fmt.Println`
// statements which may have been unintentionally left in the code for
// debugging purposes.
package shush
import (
"fmt"
"go/ast"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const Doc = `report fmt.Println statements
Find fmt.Println statements which may have been unintentionally left in the
code for debugging purposes.`
// Analyzer defines the analysis function to find and report fmt.Println
// statements. It can be referenced in an analysis driver.
var Analyzer = &analysis.Analyzer{
Name: "shush",
Doc: Doc,
Requires: []*analysis.Analyzer{inspect.Analyzer},
RunDespiteErrors: true,
Run: run,
}
func run(pass *analysis.Pass) (any, error) {
inspector, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
// explicit check: should never be false due to inspect.Analyzer dependency
return nil, fmt.Errorf("unexpected type: %T", pass.ResultOf[inspect.Analyzer])
}
nodeFilter := []ast.Node{
(*ast.SelectorExpr)(nil),
}
inspector.Preorder(nodeFilter, func(node ast.Node) {
sel := node.(*ast.SelectorExpr)
pkgIdent, ok := sel.X.(*ast.Ident)
if !ok {
// explicit check: though nil map lookup below would catch this too
return
}
pkgName, ok := pass.TypesInfo.Uses[pkgIdent].(*types.PkgName)
if !ok || pkgName.Imported().Path() != "fmt" {
return
}
if sel.Sel.Name == "Println" {
pass.Reportf(sel.Pos(), "extraneous fmt.Println statement")
}
})
return nil, nil
}