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
4 changes: 3 additions & 1 deletion pkg/linters/appendbytestring/appendbytestring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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`
}
Original file line number Diff line number Diff line change
@@ -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`
}
4 changes: 3 additions & 1 deletion pkg/linters/bytescomparestring/bytescomparestring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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`
}
Original file line number Diff line number Diff line change
@@ -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`
}
4 changes: 3 additions & 1 deletion pkg/linters/stringbytesroundtrip/stringbytesroundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
Original file line number Diff line number Diff line change
@@ -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`
}
Original file line number Diff line number Diff line change
@@ -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`
}
4 changes: 3 additions & 1 deletion pkg/linters/writebytestring/writebytestring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading