From 9b29d92225ecfa7a9005871b13869b5f8d1058f6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 31 Oct 2026 03:59:47 +0000 Subject: [PATCH] Apply filtered changes --- pkg/linters/appendbytestring/appendbytestring.go | 4 +++- .../src/appendbytestring/paren_conversion.go | 5 +++++ .../src/appendbytestring/paren_conversion.go.golden | 5 +++++ pkg/linters/bytescomparestring/bytescomparestring.go | 4 +++- .../testdata/src/bytescomparestring/paren_operand.go | 5 +++++ .../src/bytescomparestring/paren_operand.go.golden | 7 +++++++ .../stringbytesroundtrip/stringbytesroundtrip.go | 4 +++- .../src/stringbytesroundtrip/stringbytesroundtrip.go | 8 ++++++++ .../testdata/src/writebytestring/paren_conversion.go | 9 +++++++++ .../src/writebytestring/paren_conversion.go.golden | 12 ++++++++++++ pkg/linters/writebytestring/writebytestring.go | 4 +++- 11 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 pkg/linters/appendbytestring/testdata/src/appendbytestring/paren_conversion.go create mode 100644 pkg/linters/appendbytestring/testdata/src/appendbytestring/paren_conversion.go.golden create mode 100644 pkg/linters/bytescomparestring/testdata/src/bytescomparestring/paren_operand.go create mode 100644 pkg/linters/bytescomparestring/testdata/src/bytescomparestring/paren_operand.go.golden create mode 100644 pkg/linters/writebytestring/testdata/src/writebytestring/paren_conversion.go create mode 100644 pkg/linters/writebytestring/testdata/src/writebytestring/paren_conversion.go.golden diff --git a/pkg/linters/appendbytestring/appendbytestring.go b/pkg/linters/appendbytestring/appendbytestring.go index b96c6bf53ea..09ae6c41951 100644 --- a/pkg/linters/appendbytestring/appendbytestring.go +++ b/pkg/linters/appendbytestring/appendbytestring.go @@ -69,7 +69,9 @@ func analyzeAppendByteString(pass *analysis.Pass, n ast.Node, generatedFiles fil } // The second argument must be a []byte(s) conversion where s is a string. - conv, ok := call.Args[1].(*ast.CallExpr) + // astutil.UnwrapParenExpr handles a parenthesized conversion, e.g. + // append(b, ([]byte(s))...). + conv, ok := astutil.UnwrapParenExpr(call.Args[1]).(*ast.CallExpr) if !ok { return } diff --git a/pkg/linters/appendbytestring/testdata/src/appendbytestring/paren_conversion.go b/pkg/linters/appendbytestring/testdata/src/appendbytestring/paren_conversion.go new file mode 100644 index 00000000000..138cf4ed461 --- /dev/null +++ b/pkg/linters/appendbytestring/testdata/src/appendbytestring/paren_conversion.go @@ -0,0 +1,5 @@ +package appendbytestring + +func badParenConversion(b []byte, s string) []byte { + return append(b, ([]byte(s))...) // want `append\(b, \[\]byte\(s\)\.\.\.\) can be simplified to append\(b, s\.\.\.\); the \[\]byte conversion is unnecessary` +} diff --git a/pkg/linters/appendbytestring/testdata/src/appendbytestring/paren_conversion.go.golden b/pkg/linters/appendbytestring/testdata/src/appendbytestring/paren_conversion.go.golden new file mode 100644 index 00000000000..8d9ce3afa21 --- /dev/null +++ b/pkg/linters/appendbytestring/testdata/src/appendbytestring/paren_conversion.go.golden @@ -0,0 +1,5 @@ +package appendbytestring + +func badParenConversion(b []byte, s string) []byte { + return append(b, (s)...) // want `append\(b, \[\]byte\(s\)\.\.\.\) can be simplified to append\(b, s\.\.\.\); the \[\]byte conversion is unnecessary` +} diff --git a/pkg/linters/bytescomparestring/bytescomparestring.go b/pkg/linters/bytescomparestring/bytescomparestring.go index fb430543ac0..b5316a8051d 100644 --- a/pkg/linters/bytescomparestring/bytescomparestring.go +++ b/pkg/linters/bytescomparestring/bytescomparestring.go @@ -220,7 +220,9 @@ func buildBytesImportTextEdit(pass *analysis.Pass, file *ast.File, seenImportFil // 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) + // astutil.UnwrapParenExpr handles a parenthesized operand, e.g. + // string(a) == (string(b)). + call, ok := astutil.UnwrapParenExpr(expr).(*ast.CallExpr) if !ok || len(call.Args) != 1 { return nil, nil, false } diff --git a/pkg/linters/bytescomparestring/testdata/src/bytescomparestring/paren_operand.go b/pkg/linters/bytescomparestring/testdata/src/bytescomparestring/paren_operand.go new file mode 100644 index 00000000000..15a17d01a67 --- /dev/null +++ b/pkg/linters/bytescomparestring/testdata/src/bytescomparestring/paren_operand.go @@ -0,0 +1,5 @@ +package bytescomparestring + +func badParenOperand(a, b []byte) 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` +} diff --git a/pkg/linters/bytescomparestring/testdata/src/bytescomparestring/paren_operand.go.golden b/pkg/linters/bytescomparestring/testdata/src/bytescomparestring/paren_operand.go.golden new file mode 100644 index 00000000000..99285b34e6d --- /dev/null +++ b/pkg/linters/bytescomparestring/testdata/src/bytescomparestring/paren_operand.go.golden @@ -0,0 +1,7 @@ +package bytescomparestring + +import "bytes" + +func badParenOperand(a, b []byte) 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` +} diff --git a/pkg/linters/stringbytesroundtrip/stringbytesroundtrip.go b/pkg/linters/stringbytesroundtrip/stringbytesroundtrip.go index fc4f4a2200d..ebeb3821fec 100644 --- a/pkg/linters/stringbytesroundtrip/stringbytesroundtrip.go +++ b/pkg/linters/stringbytesroundtrip/stringbytesroundtrip.go @@ -70,7 +70,9 @@ func roundTripCalls(n ast.Node) (*ast.CallExpr, *ast.CallExpr, bool) { if !ok || len(outer.Args) != 1 || outer.Ellipsis.IsValid() { return nil, nil, false } - inner, ok := outer.Args[0].(*ast.CallExpr) + // astutil.UnwrapParenExpr handles a parenthesized inner conversion, e.g. + // string(([]byte(s))). + inner, ok := astutil.UnwrapParenExpr(outer.Args[0]).(*ast.CallExpr) if !ok || len(inner.Args) != 1 || inner.Ellipsis.IsValid() { return nil, nil, false } diff --git a/pkg/linters/stringbytesroundtrip/testdata/src/stringbytesroundtrip/stringbytesroundtrip.go b/pkg/linters/stringbytesroundtrip/testdata/src/stringbytesroundtrip/stringbytesroundtrip.go index 980b0331b1b..fdadb401f46 100644 --- a/pkg/linters/stringbytesroundtrip/testdata/src/stringbytesroundtrip/stringbytesroundtrip.go +++ b/pkg/linters/stringbytesroundtrip/testdata/src/stringbytesroundtrip/stringbytesroundtrip.go @@ -61,6 +61,14 @@ func badMixedNamedTypes() { _ = namedAlias([]byte(na)) // want `namedAlias\(\[\]byte\(na\)\) is a redundant round-trip; replace it with namedAlias\(na\)` } +func badParenInner() { + s := "hello" + b := []byte{104, 101, 108, 108, 111} + + _ = string(([]byte(s))) // want `string\(\[\]byte\(s\)\) is a redundant round-trip` + _ = []byte((string(b))) // want `\[\]byte\(string\(b\)\) makes two copies to clone b` +} + // helperString is a regular function, not a type conversion — must not be flagged. func helperString(b []byte) string { return string(b) } diff --git a/pkg/linters/writebytestring/testdata/src/writebytestring/paren_conversion.go b/pkg/linters/writebytestring/testdata/src/writebytestring/paren_conversion.go new file mode 100644 index 00000000000..69d973b29cc --- /dev/null +++ b/pkg/linters/writebytestring/testdata/src/writebytestring/paren_conversion.go @@ -0,0 +1,9 @@ +package writebytestring + +import "bytes" + +func badParenConversion() { + var buf bytes.Buffer + s := "hello" + buf.Write(([]byte(s))) // want `buf\.Write\(\[\]byte\(s\)\) can be replaced with io\.WriteString\(&buf, s\) to potentially avoid a \[\]byte allocation if the writer implements io\.StringWriter` +} diff --git a/pkg/linters/writebytestring/testdata/src/writebytestring/paren_conversion.go.golden b/pkg/linters/writebytestring/testdata/src/writebytestring/paren_conversion.go.golden new file mode 100644 index 00000000000..b3c4cc4a0f1 --- /dev/null +++ b/pkg/linters/writebytestring/testdata/src/writebytestring/paren_conversion.go.golden @@ -0,0 +1,12 @@ +package writebytestring + +import ( + "bytes" + "io" +) + +func badParenConversion() { + var buf bytes.Buffer + s := "hello" + io.WriteString(&buf, s) // want `buf\.Write\(\[\]byte\(s\)\) can be replaced with io\.WriteString\(&buf, s\) to potentially avoid a \[\]byte allocation if the writer implements io\.StringWriter` +} diff --git a/pkg/linters/writebytestring/writebytestring.go b/pkg/linters/writebytestring/writebytestring.go index 7ad0950f42d..28e0bfe5860 100644 --- a/pkg/linters/writebytestring/writebytestring.go +++ b/pkg/linters/writebytestring/writebytestring.go @@ -92,7 +92,9 @@ func analyzeWriteCall(pass *analysis.Pass, n ast.Node, generatedFiles filecheck. } // The single argument must be a []byte(s) conversion where s is a string. - conv, ok := call.Args[0].(*ast.CallExpr) + // astutil.UnwrapParenExpr handles a parenthesized conversion, e.g. + // w.Write(([]byte(s))). + conv, ok := astutil.UnwrapParenExpr(call.Args[0]).(*ast.CallExpr) if !ok { return }