Repository navigation
[linter-miner] Add reflect-deepequal-usage linter #66239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Package reflectdeepequalusage implements a Go analysis linter that flags | ||
| // reflect.DeepEqual() usage in conditionals or comparisons that should use | ||
| // typed equality operators or type-specific comparison functions for performance | ||
| // and type safety. | ||
| package reflectdeepequalusage | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "go/ast" | ||
|
|
||
| "golang.org/x/tools/go/analysis" | ||
|
|
||
| "github.com/github/gh-aw/pkg/linters/internal/analyzerutil" | ||
| "github.com/github/gh-aw/pkg/linters/internal/astutil" | ||
| "github.com/github/gh-aw/pkg/linters/internal/filecheck" | ||
| "github.com/github/gh-aw/pkg/linters/internal/nolint" | ||
| "github.com/github/gh-aw/pkg/logger" | ||
| ) | ||
|
|
||
| var pkgLog = logger.New("linters:reflectdeepequalusage") | ||
|
|
||
| // Analyzer is the reflect-deepequal-usage analysis pass. | ||
| var Analyzer = analyzerutil.New( | ||
| "reflectdeepequalusage", | ||
| "reports reflect.DeepEqual() usage in conditionals or comparisons that should use typed equality operators or type-specific comparison functions for performance and type safety", | ||
| run, | ||
| ) | ||
|
|
||
| // URL points to documentation for this linter. | ||
| var _ = func() struct{} { | ||
| Analyzer.URL = "https://github.com/github/gh-aw/tree/main/pkg/linters/reflect-deepequal-usage" | ||
| return struct{}{} | ||
| }() | ||
|
|
||
| func run(pass *analysis.Pass) (any, error) { | ||
| pkgLog.Printf("analyzing package %s", pass.Pkg.Path()) | ||
|
|
||
| noLintIndex, generatedFiles, err := analyzerutil.Indexes(pass) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| nodeFilter := []ast.Node{(*ast.CallExpr)(nil)} | ||
| return analyzerutil.Preorder(pass, nodeFilter, func(n ast.Node) { | ||
| analyzeCall(pass, n, generatedFiles, noLintIndex) | ||
| }) | ||
| } | ||
|
|
||
| // analyzeCall checks whether a call is reflect.DeepEqual() and reports a diagnostic if so. | ||
| func analyzeCall(pass *analysis.Pass, n ast.Node, generatedFiles filecheck.GeneratedIndex, noLintIndex nolint.DirectiveIndex) { | ||
| call, ok := n.(*ast.CallExpr) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| // Get the file position for skipping generated files | ||
| pos := pass.Fset.PositionFor(call.Pos(), false) | ||
| if filecheck.ShouldSkipFilename(pos.Filename, generatedFiles) { | ||
| return | ||
| } | ||
| if nolint.HasDirectiveForLinter(pos, noLintIndex, "reflectdeepequalusage") { | ||
| return | ||
| } | ||
|
|
||
| // Check if this is a call to reflect.DeepEqual | ||
| pkgPath, funcName, ok := astutil.PackageCall(pass, call) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reports every direct 💡 Narrow the rule before wiring it into
|
||
| if !ok || pkgPath != "reflect" || funcName != "DeepEqual" { | ||
| return | ||
| } | ||
|
|
||
| pkgLog.Printf("flagging reflect.DeepEqual() call at %s:%d", pos.Filename, pos.Line) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/codebase-design] 💡 Suggested simplificationpass.Report(analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: "reflect.DeepEqual() is inefficient and should not be used in conditionals; prefer typed equality operators or type-specific comparison functions",
})This also lets you drop the @copilot please address this. |
||
| pass.Report(analysis.Diagnostic{ | ||
|
Comment on lines
+81
to
+82
|
||
| Pos: call.Pos(), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. L73: shrink: fmt.Sprintf wraps a constant diagnostic. Use the string literal directly and remove fmt. |
||
| End: call.End(), | ||
| Message: fmt.Sprintf( | ||
|
Check failure on line 75 in pkg/linters/reflect-deepequal-usage/reflect_deepequal_usage.go
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Message: "reflect.DeepEqual() is inefficient and should not be used in conditionals; prefer typed equality operators or type-specific comparison functions",@copilot please address this. |
||
| "reflect.DeepEqual() is inefficient and should not be used in conditionals; prefer typed equality operators or type-specific comparison functions", | ||
| ), | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //go:build !integration | ||
|
|
||
| // Package reflectdeepequalusage_test provides tests for the reflect-deepequal-usage analyzer. | ||
| package reflectdeepequalusage_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "golang.org/x/tools/go/analysis/analysistest" | ||
|
|
||
| "github.com/github/gh-aw/pkg/linters/reflect-deepequal-usage" | ||
|
Check failure on line 11 in pkg/linters/reflect-deepequal-usage/reflect_deepequal_usage_test.go
|
||
| ) | ||
|
|
||
| func TestReflectDeepEqualUsage(t *testing.T) { | ||
| t.Parallel() | ||
| analysistest.Run(t, analysistest.TestData(), reflectdeepequalusage.Analyzer, "a", "b") | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Package a is the test fixture for positive cases of the reflect-deepequal-usage analyzer. | ||
| package a | ||
|
|
||
| import ( | ||
| "reflect" | ||
| ) | ||
|
|
||
| // testDeepEqualInCondition should be flagged - using reflect.DeepEqual in if condition | ||
| func testDeepEqualInCondition(a, b interface{}) { | ||
| if reflect.DeepEqual(a, b) { // want `reflect.DeepEqual\(\) is inefficient` | ||
| println("equal") | ||
| } | ||
| } | ||
|
|
||
| // testDeepEqualInAssignment should be flagged - assigning reflect.DeepEqual result | ||
| func testDeepEqualInAssignment(a, b interface{}) bool { | ||
| result := reflect.DeepEqual(a, b) // want `reflect.DeepEqual\(\) is inefficient` | ||
| return result | ||
| } | ||
|
|
||
| // testDeepEqualInReturn should be flagged - returning reflect.DeepEqual result | ||
| func testDeepEqualInReturn(a, b interface{}) bool { | ||
| return reflect.DeepEqual(a, b) // want `reflect.DeepEqual\(\) is inefficient` | ||
| } | ||
|
|
||
| // testDeepEqualNegation should be flagged - negating reflect.DeepEqual result | ||
| func testDeepEqualNegation(a, b interface{}) bool { | ||
| return !reflect.DeepEqual(a, b) // want `reflect.DeepEqual\(\) is inefficient` | ||
| } | ||
|
|
||
| // testDeepEqualWithAlias should be flagged - using an alias for reflect package | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The comment says 💡 Fix optionsEither:
As written the test silently passes while documenting an intent the code doesn't fulfill, which will confuse future maintainers per @copilot please address this. |
||
| func testDeepEqualWithAlias(a, b interface{}) bool { | ||
| r := reflect.DeepEqual // reference to function | ||
| return r(a, b) | ||
|
Comment on lines
+33
to
+34
|
||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment says "should be flagged" but there's no Please either:
As written, the comment contradicts the test assertion, which is confusing for future maintainers. @copilot please address this. |
||
|
|
||
| // testDeepEqualInComparison should be flagged - comparing result in expression | ||
| func testDeepEqualInComparison(a, b, c, d interface{}) bool { | ||
| return reflect.DeepEqual(a, b) && reflect.DeepEqual(c, d) // want `reflect.DeepEqual\(\) is inefficient` `reflect.DeepEqual\(\) is inefficient` | ||
| } | ||
|
|
||
| // testDeepEqualStandalone should be flagged - standalone call that discards result | ||
| func testDeepEqualStandalone(a, b interface{}) { | ||
| reflect.DeepEqual(a, b) // want `reflect.DeepEqual\(\) is inefficient` | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Package b is the test fixture for negative cases of the reflect-deepequal-usage analyzer. | ||
| package b | ||
|
|
||
| import ( | ||
| "reflect" | ||
| ) | ||
|
|
||
| // testTypedEquality should NOT be flagged - using typed equality | ||
| func testTypedEquality(a, b int) bool { | ||
| return a == b | ||
| } | ||
|
|
||
| // testStringEquality should NOT be flagged - comparing strings directly | ||
| func testStringEquality(a, b string) bool { | ||
| return a == b | ||
| } | ||
|
|
||
| // testSliceEquality should NOT be flagged - comparing slices with other methods | ||
| func testSliceEquality(a, b []int) bool { | ||
| if len(a) != len(b) { | ||
| return false | ||
| } | ||
| for i := range a { | ||
| if a[i] != b[i] { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // testMapEquality should NOT be flagged - comparing maps without reflect.DeepEqual | ||
| func testMapEquality(a, b map[string]int) bool { | ||
| if len(a) != len(b) { | ||
| return false | ||
| } | ||
| for k, v := range a { | ||
| if b[k] != v { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // testReflectNotDeepEqual should NOT be flagged - using reflect but different function | ||
| func testReflectNotDeepEqual(t interface{}) bool { | ||
| return reflect.TypeOf(t) != nil | ||
| } | ||
|
|
||
| // testReflectValueEqual should NOT be flagged - using reflect.Value.Equal | ||
| func testReflectValueEqual(a, b reflect.Value) bool { | ||
| return a.Equal(b) | ||
| } | ||
|
|
||
| // testStructEquality should NOT be flagged - comparing structs with typed equality | ||
| func testStructEquality(a, b struct{ x int }) bool { | ||
| return a == b | ||
| } | ||
|
|
||
| // testNilComparison should NOT be flagged - comparing with nil | ||
| func testNilComparison(a interface{}) bool { | ||
| return a == nil | ||
| } | ||
|
|
||
| func testWithNolintDirective(a, b interface{}) bool { | ||
| // This should NOT be flagged because of the nolint directive | ||
| return reflect.DeepEqual(a, b) //nolint:reflectdeepequalusage | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ import ( | |
| "github.com/github/gh-aw/pkg/linters/packagelevelmutableslicemap" | ||
| panicinlibrarycode "github.com/github/gh-aw/pkg/linters/panic-in-library-code" | ||
| "github.com/github/gh-aw/pkg/linters/rawloginlib" | ||
| reflectdeepequalusage "github.com/github/gh-aw/pkg/linters/reflect-deepequal-usage" | ||
| "github.com/github/gh-aw/pkg/linters/regexpcompileinfunction" | ||
| "github.com/github/gh-aw/pkg/linters/regexpdynamicpattern" | ||
| "github.com/github/gh-aw/pkg/linters/seenmapbool" | ||
|
|
@@ -133,6 +134,7 @@ var allAnalyzers = []*analysis.Analyzer{ | |
| packagelevelmutableslicemap.Analyzer, | ||
| panicinlibrarycode.Analyzer, | ||
| rawloginlib.Analyzer, | ||
| reflectdeepequalusage.Analyzer, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/grill-with-docs] Running the compiled binary against this repo, 💡 What to checkEither:
Right now the PR claims "Successfully integrated with the multichecker" but doesn't clarify it has zero effect in the actual CI gate. @copilot please address this. |
||
| regexpcompileinfunction.Analyzer, | ||
| regexpdynamicpattern.Analyzer, | ||
| ssljson.Analyzer, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/codebase-design] Don't hand-roll the
Analyzer.URLpatch with an IIFE; use the existinganalyzerutil.NewAtPathhelper, which is built exactly for dash-named package directories (seepanic-in-library-code.go).💡 Suggested fix
This removes the need for the awkward package-scope
var _ = func() struct{} { ... }()block entirely and matches the conventionpanicinlibrarycode.Analyzeralready establishes for the same directory-naming mismatch.@copilot please address this.