Skip to content

peczenyj/fmtquotecheck

Repository files navigation

quotecheck

tag Go Version GoDoc Go Lint codecov Report card CodeQL Dependency Review License Latest release GitHub Release Date Last commit PRs Welcome

Verify when safely escape and single quote strings on fmt.Sprintf.

This code is based on perfsprint analyzer.

Motivation

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.

Instruction

go install github.com/peczenyj/fmtquotecheck/cmd/fmtquotecheck@latest

Usage

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.

CI

CircleCI

- run:
    name: install fmtquotecheck
    command: go install github.com/peczenyj/fmtquotecheck/cmd/fmtquotecheck@latest

- run:
    name: run fmtquotecheck
    command: fmtquotecheck ./...

GitHub Actions

- name: install fmtquotecheck
  run: go install github.com/peczenyj/fmtquotecheck/cmd/fmtquotecheck@latest

- name: run fmtquotecheck
  run: fmtquotecheck ./...