Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runes: update preferWriteByte #1151

Merged
merged 2 commits into from Nov 4, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions checkers/rules/rules.go
Expand Up @@ -329,14 +329,17 @@ func assignOp(m dsl.Matcher) {
m.Match(`$x = $x &^ $y`).Where(m["x"].Pure).Report("replace `$$` with `$x &^= $y`")
}

//doc:summary Detects WriteRune calls with byte literal argument and reports to use WriteByte instead
//doc:tags performance experimental
//doc:summary Detects WriteRune calls with rune literal argument that is single byte and reports to use WriteByte instead
//doc:tags performance experimental opinionated
//doc:before w.WriteRune('\n')
//doc:after w.WriteByte('\n')
func preferWriteByte(m dsl.Matcher) {
// utf8.RuneSelf:
// characters below RuneSelf are represented as themselves in a single byte.
const runeSelf = 0x80
m.Match(`$w.WriteRune($c)`).Where(
m["w"].Type.Implements("io.ByteWriter") && (m["c"].Const && m["c"].Value.Int() < 256),
).Report(`consider replacing $$ with $w.WriteByte($c)`)
m["w"].Type.Implements("io.ByteWriter") && (m["c"].Const && m["c"].Value.Int() < runeSelf),
).Report(`consider writing single byte rune $c with $w.WriteByte($c)`)
}

//doc:summary Detects fmt.Sprint(f/ln) calls which can be replaced with fmt.Fprint(f/ln)
Expand Down