perfsprint

command module
v0.0.0-...-562e652 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2025 License: MIT Imports: 2 Imported by: 0

README

perfsprint

CI Go Report Card

Golang linter for performance, aiming at usages of fmt.Sprintf which have faster alternatives.

Installation

go install github.com/untruerecre/perfsprint@latest

Usage

perfsprint --fix ./...

Options

The 5 following options cover all optimizations proposed by the linter.

Some have suboptions for specific cases.

  • integer-format (formatting integer with the package strconv)
    • int-conversion : disable when the optimization adds a int/uint cast (readability)
  • error-format (formatting errors)
    • errorf : known behavior change avoiding panic
    • err-error : known behavior change panicking for nil errors
  • string-format (formatting strings)
    • sprintf1 : known behavior change avoiding panic
    • strconcat : disable turning some fmt.Sprintf to a string concatenation (readability)
  • bool-format (formatting bool with strconv.FormatBool)
  • hex-format (formatting bytes with hex.EncodeToString)

To disable fmt.Errorf optimization, you can use the flag -errorf=false This optimization is not always equivalent. The code

msg := "format string attack %s"
fmt.Errorf(msg)

will panic when its optimized version will not (so it should be safer).

To disable fmt.Sprintf("toto") optimization, you can use the flag -sprintf1=false This optimization is not always equivalent. The code

msg := "format string attack %s"
fmt.Sprintf(msg)

will panic when its optimized version will not (so it should be safer).

To enable err.Error() optimization, you can use the flag -err-error=true This optimization only works when the error is not nil, otherwise the resulting code will panic.

Replacements
fmt.Sprintf("%s", strVal)  ->  strVal
fmt.Sprintf("%t", boolVal) ->  strconv.FormatBool(boolBal)
fmt.Sprintf("%x", hash)    ->  hex.EncodeToString(hash)
fmt.Sprintf("%d", id)      ->  strconv.Itoa(id)
fmt.Sprintf("%v", version) ->  strconv.FormatUint(uint64(version), 10)

More in tests.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL