catenacyber / perfsprint

Golang linter to use strconv
MIT License
21 stars 2 forks source link

RFE: `hex.EncodeToString` on byte _arrays_, with slicing #13

Closed scop closed 10 months ago

scop commented 11 months ago

hex.EncodeToString is currently (correctly) not being suggested for byte arrays.

I'm wondering if it should, also noting that an array would need to be made a slice one way or another, e.g. with [:]?

catenacyber commented 11 months ago

Hmm... Do you have a code sample ? If I recall correctly, some cases are supported

scop commented 11 months ago

https://go.dev/play/p/p98DpofjCGz

package main

import (
    "crypto/sha256"
    "encoding/hex"
    "fmt"
)

func main() {
    sum := sha256.Sum256([]byte("hello world\n"))
    fmt.Println(fmt.Sprintf("%x", sum))
    // fmt.Println(hex.EncodeToString(sum)) // cannot use sum (variable of type [32]byte) as []byte value in argument to hex.EncodeToString
    fmt.Println(hex.EncodeToString(sum[:]))
}
catenacyber commented 11 months ago

This only works for literals, right ? Cf https://go.dev/play/p/V2PmRQt7jUn

    fmt.Println(fmt.Sprintf("%x", sha256.Sum256([]byte("hello world\n"))))
// cannot be replaced by following line
    fmt.Println(hex.EncodeToString(sha256.Sum256([]byte("hello world\n"))[:]))
// errors with invalid operation: sha256.Sum256([]byte("hello world\n")) (value of type [32]byte) (slice of unaddressable value)
Jorropo commented 11 months ago

@catenacyber this works for non literal but you need to put in a variable first:

sum := sha256.Sum256([]byte("hello world\n"))
fmt.Println(hex.EncodeToString(sum[:]))

This could be tricky because if there are multiple functions invoked in the expression moving one before in a variable might change side-effects.

catenacyber commented 11 months ago

@scop this works for me with latest commit 0.3.1

Which version are you running ?

catenacyber commented 10 months ago

Closing as working on latest version