-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom Linter to check 'setupLog.Error(nil, ...)'
New custom linter under hack/ci project was created to allow us to define custom linters for the project
- Loading branch information
1 parent
75bb73e
commit 1dd674a
Showing
6 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/github.com/operator-framework/operator-controller/custom-linters/setuplognilerrorcheck" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/unitchecker" | ||
) | ||
|
||
// Define the custom Linters implemented in the project | ||
var customLinters = []*analysis.Analyzer{ | ||
setuplognilerrorcheck.SetupLogNilErrorCheck, | ||
} | ||
|
||
func main() { | ||
unitchecker.Main(customLinters...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/github.com/operator-framework/operator-controller/custom-linters | ||
|
||
go 1.23.4 | ||
|
||
require golang.org/x/tools v0.29.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= | ||
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= | ||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= | ||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE= | ||
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588= |
43 changes: 43 additions & 0 deletions
43
hack/ci/custom-linters/setuplognilerrorcheck/setuplognilerrorcheck.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package setuplognilerrorcheck | ||
|
||
import ( | ||
"go/ast" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
var SetupLogNilErrorCheck = &analysis.Analyzer{ | ||
Name: "setuplognilerrorcheck", | ||
Doc: "check for setupLog.Error(nil, ...) calls with nil as the first argument", | ||
Run: runSetupLogNilErrorCheck, | ||
} | ||
|
||
func runSetupLogNilErrorCheck(pass *analysis.Pass) (interface{}, error) { | ||
for _, f := range pass.Files { | ||
ast.Inspect(f, func(n ast.Node) bool { | ||
expr, ok := n.(*ast.CallExpr) | ||
if !ok { | ||
return true | ||
} | ||
|
||
selectorExpr, ok := expr.Fun.(*ast.SelectorExpr) | ||
if !ok || selectorExpr.Sel.Name != "Error" { | ||
return true | ||
} | ||
|
||
i, ok := selectorExpr.X.(*ast.Ident) | ||
if !ok || i.Name != "setupLog" { | ||
return true | ||
} | ||
|
||
if len(expr.Args) > 0 { | ||
if arg, ok := expr.Args[0].(*ast.Ident); ok && arg.Name == "nil" { | ||
pass.Reportf(expr.Pos(), "Avoid using 'setupLog.Error(nil, ...)'. Instead, use 'errors.New()' "+ | ||
"or 'fmt.Errorf()' to ensure logs are created. Using 'nil' for errors can result in silent "+ | ||
"failures, making bugs harder to detect.") | ||
} | ||
} | ||
return true | ||
}) | ||
} | ||
return nil, nil | ||
} |