Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[linter-miner] linter: add bytescomparestring — flag string(a)==string(b) where a,b are []byte #44389
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
Uh oh!
There was an error while loading. Please reload this page.
[linter-miner] linter: add bytescomparestring — flag string(a)==string(b) where a,b are []byte #44389
Changes from all commits
0e3b40d8b36602846a440File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
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.
[/tdd] Missing edge case: what happens when either
lTextorrTextis empty (line 69 guard returns early) — the diagnostic is silently suppressed. Add a test fixture with a complex expression likestring(f())orstring(buf.Bytes())to verify the linter fires even whenastutil.NodeTextmight produce multi-token text, and confirm the early-return is never reachable for valid Go ASTs.\n\n@copilot please address this.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] The
if/elseonbin.Op(lines 77–105) duplicates theanalysis.Diagnosticstructure; both branches differ only in message template and replacement text prefix. Extracting a small helper reduces the divergence surface.💡 Suggested refactor
Extract a helper that accepts the op-specific strings and returns
analysis.Diagnostic, then call it from both branches. This mirrors howappendbytestringkeeps its reporting in a singlebuildFixfunction.@copilot please address this.
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.
SuggestedFix emits
bytes.Equal(...)without adding the"bytes"import, producing uncompilable code on every real violation in this repo.💡 Details and fix
The
TextEditreplaces the binary expression withbytes.Equal(a, b)or!bytes.Equal(a, b), but never inserts animport "bytes"declaration. Applying the fix to any file that does not already import"bytes"results in a compile error (undefined: bytes).All three live callsites identified in the PR description have been verified to lack the
"bytes"import:pkg/parser/import_bfs.go— importsencoding/json,errors,fmt,maps,path,path/filepath,strings— no"bytes"pkg/parser/tools_merger.go— importsencoding/json,fmt,maps,strings— no"bytes"pkg/cli/packages.go— importsbufio,fmt,os,path/filepath,regexp,strings— no"bytes"So every suggested fix the linter would propose in this codebase is broken out of the box.
Fix: The
analysis.SuggestedFixcan include multipleTextEditentries. Add a second edit that inserts"bytes"into the file's import block. Usegolang.org/x/tools/go/ast/astutil.AddImportto locate the right position and generate the edit, or locate the end of the existingimport (...)block and inject\t\"bytes\"\nthere.Note: the
appendbytestringpeer linter avoids this entirely because its fix removes a conversion rather than adding a new package reference.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.
Blocking: The
SuggestedFixrewrites the expression tobytes.Equal(...)but does not add a"bytes"import if the file doesn't already have one. All 3 real-world instances flagged in the PR description (import_bfs.go,tools_merger.go,packages.go) do not import"bytes", so applying this fix viago fixwould produce non-compiling code.The test fixture passes only because it pre-imports
"bytes", masking this gap.Either:
TextEditthat inserts the"bytes"import (usegolang.org/x/tools/go/ast/astutil.AddImportto get the correct position), orbytesis not pre-imported to confirmanalysistest.RunWithSuggestedFixesworks end-to-end.Without this,
go fix ./...will break compilation for the very instances this linter flags.@copilot please address this.
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.
Test fixture masks the missing-import bug: it already imports
"bytes", soRunWithSuggestedFixesnever exercises the broken path.💡 Details and suggested test
The testdata file imports
"bytes"on line 3 solely becausegoodBytesEqualusesbytes.Equal. This meansanalysistest.RunWithSuggestedFixesapplies the generatedTextEdits to a file that already has the import, and the golden-file comparison passes even though the linter never emits an import-adding edit.There is no test for the real-world scenario where the file with the bad pattern has no pre-existing
"bytes"import — which is true of all three live violations in this repo.Suggested addition: add a second fixture package (e.g.
testdata/src/noimport/noimport.go) that has the bad pattern but no"bytes"import:And a corresponding
.goldenfile that adds the"bytes"import:This test will fail until the import-insertion
TextEditis added to theSuggestedFix, enforcing the fix is correct end-to-end.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.
[/tdd] The test covers named types (
myBytes) for the==case but not for!=. AbadNamedTypeNotEqualcase would close that gap and confirm the operator-dispatch logic is symmetric for non-[]byteunderlying types.\n\n@copilot please address this.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.
[/tdd] Missing negative test:
string(a) == string(b)where one argument is[]uint8and the other is a named[]bytealias. TheisByteSlicehelper accepts both because it checkstypes.Byte, but having a fixture case makes that guarantee explicit and prevents future regressions.\n\n@copilot please address this.Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.