Skip to content

Commit

Permalink
runes: update preferWriteByte
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 4, 2021
1 parent c06765d commit edc4845
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions checkers/rules/rules.go
Expand Up @@ -329,14 +329,15 @@ 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 opionated
//doc:before w.WriteRune('\n')
//doc:after w.WriteByte('\n')
func preferWriteByte(m dsl.Matcher) {
const runeSelf = 0x80 // utf8.RuneSelf
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() < 0x80),
).Report(`consider writing single byte rune $$ with $w.WriteByte($c)`)
}

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

0 comments on commit edc4845

Please sign in to comment.