|
| 1 | +package analyzers |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "go/ast" |
| 7 | + "go/format" |
| 8 | + "go/token" |
| 9 | + "go/types" |
| 10 | + |
| 11 | + "golang.org/x/tools/go/analysis" |
| 12 | +) |
| 13 | + |
| 14 | +var SetupLogNilErrorCheck = &analysis.Analyzer{ |
| 15 | + Name: "setuplognilerrorcheck", |
| 16 | + Doc: "Detects improper usage of logger.Error() calls, ensuring the first argument is a non-nil error.", |
| 17 | + Run: runSetupLogNilErrorCheck, |
| 18 | +} |
| 19 | + |
| 20 | +func runSetupLogNilErrorCheck(pass *analysis.Pass) (interface{}, error) { |
| 21 | + for _, f := range pass.Files { |
| 22 | + ast.Inspect(f, func(n ast.Node) bool { |
| 23 | + callExpr, ok := n.(*ast.CallExpr) |
| 24 | + if !ok { |
| 25 | + return true |
| 26 | + } |
| 27 | + |
| 28 | + // Ensure function being called is logger.Error |
| 29 | + selectorExpr, ok := callExpr.Fun.(*ast.SelectorExpr) |
| 30 | + if !ok || selectorExpr.Sel.Name != "Error" { |
| 31 | + return true |
| 32 | + } |
| 33 | + |
| 34 | + // Ensure receiver (logger) is identified |
| 35 | + ident, ok := selectorExpr.X.(*ast.Ident) |
| 36 | + if !ok { |
| 37 | + return true |
| 38 | + } |
| 39 | + |
| 40 | + // Verify if the receiver is logr.Logger |
| 41 | + obj := pass.TypesInfo.ObjectOf(ident) |
| 42 | + if obj == nil { |
| 43 | + return true |
| 44 | + } |
| 45 | + |
| 46 | + named, ok := obj.Type().(*types.Named) |
| 47 | + if !ok || named.Obj().Pkg() == nil || named.Obj().Pkg().Path() != "github.com/go-logr/logr" || named.Obj().Name() != "Logger" { |
| 48 | + return true |
| 49 | + } |
| 50 | + |
| 51 | + if len(callExpr.Args) == 0 { |
| 52 | + return true |
| 53 | + } |
| 54 | + |
| 55 | + // Check if the first argument of the error log is nil |
| 56 | + firstArg, ok := callExpr.Args[0].(*ast.Ident) |
| 57 | + if !ok || firstArg.Name != "nil" { |
| 58 | + return true |
| 59 | + } |
| 60 | + |
| 61 | + // Extract error message for the suggestion |
| 62 | + suggestedError := "errors.New(\"kind error (i.e. configuration error)\")" |
| 63 | + suggestedMessage := "\"error message describing the failed operation\"" |
| 64 | + |
| 65 | + if len(callExpr.Args) > 1 { |
| 66 | + if msgArg, ok := callExpr.Args[1].(*ast.BasicLit); ok && msgArg.Kind == token.STRING { |
| 67 | + suggestedMessage = msgArg.Value |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Get the actual source code line where the issue occurs |
| 72 | + var srcBuffer bytes.Buffer |
| 73 | + if err := format.Node(&srcBuffer, pass.Fset, callExpr); err == nil { |
| 74 | + sourceLine := srcBuffer.String() |
| 75 | + pass.Reportf(callExpr.Pos(), |
| 76 | + "Incorrect usage of 'logger.Error(nil, ...)'. The first argument must be a non-nil 'error'. "+ |
| 77 | + "Passing 'nil' results in silent failures, making debugging harder.\n\n"+ |
| 78 | + "\U0001F41B **What is wrong?**\n"+ |
| 79 | + fmt.Sprintf(" %s\n\n", sourceLine)+ |
| 80 | + "\U0001F4A1 **How to solve? Return the error, i.e.:**\n"+ |
| 81 | + fmt.Sprintf(" logger.Error(%s, %s, \"key\", value)\n\n", suggestedError, suggestedMessage)) |
| 82 | + } |
| 83 | + |
| 84 | + return true |
| 85 | + }) |
| 86 | + } |
| 87 | + return nil, nil |
| 88 | +} |
0 commit comments