Skip to content

Instantly share code, notes, and snippets.

@quasilyte
Last active July 10, 2020 09:03
Show Gist options
  • Save quasilyte/3d12aea1ee9ab7eb74445ff0e447c9c9 to your computer and use it in GitHub Desktop.
Save quasilyte/3d12aea1ee9ab7eb74445ff0e447c9c9 to your computer and use it in GitHub Desktop.
package gorules
import "github.com/quasilyte/go-ruleguard/dsl/fluent"
func useMathBits(m fluent.Matcher) {
m.Match(`$X >> $N | $X << (8 - $N)`,
`$X << $N | $X >> (8 - $N)`,
`$X >> (8 - $N) | $X << $N`,
`$X << (8 - $N) | $X >> $N`,
`$X >> $N | $X << (16 - $N)`,
`$X << $N | $X >> (16 - $N)`,
`$X >> (16 - $N) | $X << $N`,
`$X << (16 - $N) | $X >> $N`,
`$X >> $N | $X << (32 - $N)`,
`$X << $N | $X >> (32 - $N)`,
`$X >> (32 - $N) | $X << $N`,
`$X << (32 - $N) | $X >> $N`,
`$X >> $N | $X << (64 - $N)`,
`$X << $N | $X >> (64 - $N)`,
`$X >> (64 - $N) | $X << $N`,
`$X << (64 - $N) | $X >> $N`).
Report(`Try using math/bits instead`)
}
package gorules
import "github.com/quasilyte/go-ruleguard/dsl/fluent"
func useMathBits(m fluent.Matcher) {
// Suggest() is a rewrite template for a match.
// If no Report is defined, it's also used to infer the warning text
// when autofixing is disabled.
m.Match(`$x << $n | $x >> (8 - $n) `,).
Suggest(`bits.RotateLeft8($x, $n)`)
m.Match(`$x << $n | $x >> (16 - $n) `,).
Suggest(`bits.RotateLeft16($x, $n)`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment