Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/daily-team-evolution-insights.lock.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions .github/workflows/mcp-inspector.lock.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions pkg/linters/bytescomparestring/bytescomparestring.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ func analyzeBinaryExpr(pass *analysis.Pass, n ast.Node, generatedFiles filecheck
if nolint.HasDirectiveForLinter(pos, noLintIndex, "bytescomparestring") {
return
}
lhsArg, ok := extractByteSliceStringConv(pass, bin.X)
lhsArg, lhsType, ok := extractByteSliceStringConv(pass, bin.X)
if !ok {
return
}
rhsArg, ok := extractByteSliceStringConv(pass, bin.Y)
rhsArg, rhsType, ok := extractByteSliceStringConv(pass, bin.Y)
if !ok {
return
}
lText := astutil.NodeText(pass.Fset, lhsArg)
rText := astutil.NodeText(pass.Fset, rhsArg)
if lText == "" || rText == "" {
lTypeText := astutil.NodeText(pass.Fset, lhsType)
rTypeText := astutil.NodeText(pass.Fset, rhsType)
if lText == "" || rText == "" || lTypeText == "" || rTypeText == "" {
return
}
if !coverage.ShouldApply(pass, bin.Pos(), *hotThreshold) {
Expand All @@ -90,7 +92,7 @@ func analyzeBinaryExpr(pass *analysis.Pass, n ast.Node, generatedFiles filecheck
pass.Report(analysis.Diagnostic{
Pos: bin.Pos(),
End: bin.End(),
Message: fmt.Sprintf("string(%s) == string(%s) is a []byte comparison written the long way; use bytes.Equal(%s, %s) for clearer intent", lText, rText, lText, rText),
Message: fmt.Sprintf("%s(%s) == %s(%s) is a []byte comparison written the long way; use bytes.Equal(%s, %s) for clearer intent", lTypeText, lText, rTypeText, rText, lText, rText),
SuggestedFixes: fixes,
})
} else {
Expand All @@ -101,7 +103,7 @@ func analyzeBinaryExpr(pass *analysis.Pass, n ast.Node, generatedFiles filecheck
pass.Report(analysis.Diagnostic{
Pos: bin.Pos(),
End: bin.End(),
Message: fmt.Sprintf("string(%s) != string(%s) is a []byte comparison written the long way; use !bytes.Equal(%s, %s) for clearer intent", lText, rText, lText, rText),
Message: fmt.Sprintf("%s(%s) != %s(%s) is a []byte comparison written the long way; use !bytes.Equal(%s, %s) for clearer intent", lTypeText, lText, rTypeText, rText, lText, rText),
SuggestedFixes: fixes,
})
}
Expand Down Expand Up @@ -216,35 +218,35 @@ func buildBytesImportTextEdit(pass *analysis.Pass, file *ast.File, seenImportFil
}, true
}

// extractByteSliceStringConv checks whether expr is a string(x) conversion
// where x has underlying type []byte. If so, it returns x and true.
func extractByteSliceStringConv(pass *analysis.Pass, expr ast.Expr) (ast.Expr, bool) {
// extractByteSliceStringConv checks whether expr is a string-like type conversion
// where x has underlying type []byte. If so, it returns x, the conversion type, and true.
func extractByteSliceStringConv(pass *analysis.Pass, expr ast.Expr) (ast.Expr, ast.Expr, bool) {
call, ok := expr.(*ast.CallExpr)
if !ok || len(call.Args) != 1 {
return nil, false
return nil, nil, false
}

// Must be a type conversion, not a function call.
funInfo, ok := pass.TypesInfo.Types[call.Fun]
if !ok || !funInfo.IsType() {
return nil, false
return nil, nil, false
}

// The outer conversion must produce a string.
resultInfo, ok := pass.TypesInfo.Types[call]
if !ok {
return nil, false
return nil, nil, false
}
basic, ok := resultInfo.Type.Underlying().(*types.Basic)
if !ok || basic.Kind() != types.String {
return nil, false
return nil, nil, false
}

// The argument must be []byte (or []uint8).
arg := call.Args[0]
if !astutil.IsByteSlice(pass, arg) {
return nil, false
return nil, nil, false
}

return arg, true
return arg, call.Fun, true
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func badNamedType(a, b myBytes) bool {
return string(a) == string(b) // want `string\(a\) == string\(b\) is a \[\]byte comparison written the long way; use bytes\.Equal\(a, b\) for clearer intent`
}

type Password string

func badNamedStringType(a, b []byte) bool {
return Password(a) == Password(b) // want `Password\(a\) == Password\(b\) is a \[\]byte comparison written the long way; use bytes\.Equal\(a, b\) for clearer intent`
}

func goodBytesEqual(a, b []byte) bool {
// Correct usage — no diagnostic expected.
return bytes.Equal(a, b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func badNamedType(a, b myBytes) bool {
return bytes.Equal(a, b) // want `string\(a\) == string\(b\) is a \[\]byte comparison written the long way; use bytes\.Equal\(a, b\) for clearer intent`
}

type Password string

func badNamedStringType(a, b []byte) bool {
return bytes.Equal(a, b) // want `Password\(a\) == Password\(b\) is a \[\]byte comparison written the long way; use bytes\.Equal\(a, b\) for clearer intent`
}

func goodBytesEqual(a, b []byte) bool {
// Correct usage — no diagnostic expected.
return bytes.Equal(a, b)
Expand Down
Loading