Skip to content

Commit

Permalink
(enhance): Enhance custom linter setuplogerrorcheck description (#1778)
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 authored Feb 14, 2025
1 parent 7146a7a commit f7ff6bd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions hack/ci/custom-linters/analyzers/setuplognilerrorcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (

var SetupLogErrorCheck = &analysis.Analyzer{
Name: "setuplogerrorcheck",
Doc: "Detects improper usage of logger.Error() calls, ensuring the first argument is a non-nil error.",
Run: runSetupLogErrorCheck,
Doc: "Detects and reports improper usages of logger.Error() calls to enforce good practices " +
"and prevent silent failures.",
Run: runSetupLogErrorCheck,
}

func runSetupLogErrorCheck(pass *analysis.Pass) (interface{}, error) {
Expand Down Expand Up @@ -72,7 +73,7 @@ func runSetupLogErrorCheck(pass *analysis.Pass) (interface{}, error) {

pass.Reportf(callExpr.Pos(),
"Incorrect usage of 'logger.Error(nil, ...)'. The first argument must be a non-nil 'error'. "+
"Passing 'nil' results in silent failures, making debugging harder.\n\n"+
"Passing 'nil' may hide error details, making debugging harder.\n\n"+
"\U0001F41B **What is wrong?**\n %s\n\n"+
"\U0001F4A1 **How to solve? Return the error, i.e.:**\n logger.Error(%s, %s, \"key\", value)\n\n",
sourceLine, suggestedError, suggestedMessage)
Expand Down
2 changes: 1 addition & 1 deletion hack/ci/custom-linters/analyzers/testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func testLogger() {
var value int

// Case 1: Nil error - Ensures the first argument cannot be nil.
logger.Error(nil, "message") // want ".*results in silent failures, making debugging harder.*"
logger.Error(nil, "message") // want ".*may hide error details, making debugging harder*"

// Case 2: Odd number of key-value arguments - Ensures key-value pairs are complete.
logger.Error(err, "message", "key1") // want ".*Key-value pairs must be provided after the message, but an odd number of arguments was found.*"
Expand Down

0 comments on commit f7ff6bd

Please sign in to comment.