Verify when safely escape and single quote strings on fmt.Sprintf.
This code is based on perfsprint analyzer.
While it seems safe write '%s'
to just delimit some string, it may have nasty consequences, like if the string already contains a quote char it add some surprises in our output.
But go supports a better alternative: %q
is defined as a single-quoted character literal safely escaped with Go syntax.
go install github.com/peczenyj/fmtquotecheck/cmd/fmtquotecheck@latest
package main
import "fmt"
func main(){
fmt.Printf("hello '%s'", "world") // we should use %q here
}
$ fmtquotecheck ./main.go
./main.go:6:16: explicit single-quoted '%s' should be replaced by `%q` in fmt.Printf
by using the option -fix
the linter will convert all '%s'
to %q
.
- run:
name: install fmtquotecheck
command: go install github.com/peczenyj/fmtquotecheck/cmd/fmtquotecheck@latest
- run:
name: run fmtquotecheck
command: fmtquotecheck ./...
- name: install fmtquotecheck
run: go install github.com/peczenyj/fmtquotecheck/cmd/fmtquotecheck@latest
- name: run fmtquotecheck
run: fmtquotecheck ./...