|
| 1 | +package rule |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "go/ast" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/mgechev/revive/internal/astutils" |
| 9 | + "github.com/mgechev/revive/lint" |
| 10 | +) |
| 11 | + |
| 12 | +// UseFmtPrintRule lints calls to print and println. |
| 13 | +type UseFmtPrintRule struct{} |
| 14 | + |
| 15 | +// Apply applies the rule to given file. |
| 16 | +func (r *UseFmtPrintRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { |
| 17 | + redefinesPrint, redefinesPrintln := r.analyzeRedefinitions(file.AST.Decls) |
| 18 | + // Here we could check if both are redefined and if it's the case return nil |
| 19 | + // but the check being false 99.9999999% of the cases we don't |
| 20 | + |
| 21 | + var failures []lint.Failure |
| 22 | + onFailure := func(failure lint.Failure) { |
| 23 | + failures = append(failures, failure) |
| 24 | + } |
| 25 | + |
| 26 | + w := lintUseFmtPrint{onFailure, redefinesPrint, redefinesPrintln} |
| 27 | + ast.Walk(w, file.AST) |
| 28 | + |
| 29 | + return failures |
| 30 | +} |
| 31 | + |
| 32 | +// Name returns the rule name. |
| 33 | +func (*UseFmtPrintRule) Name() string { |
| 34 | + return "use-fmt-print" |
| 35 | +} |
| 36 | + |
| 37 | +type lintUseFmtPrint struct { |
| 38 | + onFailure func(lint.Failure) |
| 39 | + redefinesPrint bool |
| 40 | + redefinesPrintln bool |
| 41 | +} |
| 42 | + |
| 43 | +func (w lintUseFmtPrint) Visit(node ast.Node) ast.Visitor { |
| 44 | + ce, ok := node.(*ast.CallExpr) |
| 45 | + if !ok { |
| 46 | + return w // nothing to do, the node is not a call |
| 47 | + } |
| 48 | + |
| 49 | + id, ok := (ce.Fun).(*ast.Ident) |
| 50 | + if !ok { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + name := id.Name |
| 55 | + switch name { |
| 56 | + case "print": |
| 57 | + if w.redefinesPrint { |
| 58 | + return nil // it's a call to user-defined print |
| 59 | + } |
| 60 | + case "println": |
| 61 | + if w.redefinesPrintln { |
| 62 | + return nil // it's a call to user-defined println |
| 63 | + } |
| 64 | + default: |
| 65 | + return nil // nothing to do, the call is not println(...) nor print(...) |
| 66 | + } |
| 67 | + |
| 68 | + callArgs := w.callArgsAsStr(ce.Args) |
| 69 | + w.onFailure(lint.Failure{ |
| 70 | + Confidence: 1, |
| 71 | + Node: node, |
| 72 | + Category: lint.FailureCategoryBadPractice, |
| 73 | + Failure: fmt.Sprintf(`avoid using built-in function %q, replace it by "fmt.F%s(os.Stderr, %s)"`, name, name, callArgs), |
| 74 | + }) |
| 75 | + |
| 76 | + return w |
| 77 | +} |
| 78 | + |
| 79 | +func (lintUseFmtPrint) callArgsAsStr(args []ast.Expr) string { |
| 80 | + strs := []string{} |
| 81 | + for _, expr := range args { |
| 82 | + strs = append(strs, astutils.GoFmt(expr)) |
| 83 | + } |
| 84 | + |
| 85 | + return strings.Join(strs, ", ") |
| 86 | +} |
| 87 | + |
| 88 | +func (UseFmtPrintRule) analyzeRedefinitions(decls []ast.Decl) (redefinesPrint, redefinesPrintln bool) { |
| 89 | + for _, decl := range decls { |
| 90 | + fnDecl, ok := decl.(*ast.FuncDecl) |
| 91 | + if !ok { |
| 92 | + continue // not a function declaration |
| 93 | + } |
| 94 | + |
| 95 | + if fnDecl.Recv != nil { |
| 96 | + continue // it´s a method (not function) declaration |
| 97 | + } |
| 98 | + |
| 99 | + switch fnDecl.Name.Name { |
| 100 | + case "print": |
| 101 | + redefinesPrint = true |
| 102 | + case "println": |
| 103 | + redefinesPrintln = true |
| 104 | + } |
| 105 | + } |
| 106 | + return redefinesPrint, redefinesPrintln |
| 107 | +} |
0 commit comments