Rules are located in rules package.
Run tests:
$ go test -v ./rules
=== RUN TestRules
--- PASS: TestRules (1.00s)
PASS
Notes:
- Rules need
dsl
package, so it is present in thego.mod
- Since we have tests for the rules, we also need
golang.org/x/tools
package foranalysistest
If you don't want to have golang.org/x/tools
dependency in your project, then you should move
rules to a separate module and use it as a rules bundle in your main module.
Run rules over the demo project:
$ ruleguard -c 0 -rules rules/rules.go ./mandelbrot
mandelbrot/main.go:36:9: imagePt: zero point should be written as image.Point{} (rules.go:30)
36 min := image.Pt(0, 0)
mandelbrot/main.go:40:11: imageColors: suggestion: color.Black (rules.go:8)
40 black := color.Gray16{0}
mandelbrot/main.go:41:48: imageZP: image.ZP is deprecated, use image.Point{} instead (rules.go:24)
41 draw.Draw(b, bounds, image.NewUniform(black), image.ZP, draw.Src)
Run rules with golangci-lint:
$ golangci-lint run ./mandelbrot
mandelbrot/main.go:36:9: ruleguard: zero point should be written as image.Point{} (gocritic)
min := image.Pt(0, 0)
^
mandelbrot/main.go:40:11: ruleguard: suggestion: color.Black (gocritic)
black := color.Gray16{0}
^
mandelbrot/main.go:41:48: ruleguard: image.ZP is deprecated, use image.Point{} instead (gocritic)
draw.Draw(b, bounds, image.NewUniform(black), image.ZP, draw.Src)
^
See .golangci.yml config to see how to enable ruleguard
for your golangci-lint.
Run rules with gocritic:
$ gocritic check -enable ruleguard -@ruleguard.rules rules/rules.go ./mandelbrot
./mandelbrot/main.go:41:48: ruleguard: image.ZP is deprecated, use image.Point{} instead
./mandelbrot/main.go:40:11: ruleguard: suggestion: color.Black
./mandelbrot/main.go:36:9: ruleguard: zero point should be written as image.Point{}
Just run ruleguard with -fix
flag.
$ ruleguard -fix -rules rules/rules.go ./mandelbrot
mandelbrot/main.go:36:9: imagePt: zero point should be written as image.Point{} (rules.go:30)
mandelbrot/main.go:40:11: imageColors: suggestion: color.Black (rules.go:8)
mandelbrot/main.go:41:48: imageZP: image.ZP is deprecated, use image.Point{} instead (rules.go:24)
Diff:
func main() {
scale := width / (rMax - rMin)
height := int(scale * (iMax - iMin))
- min := image.Pt(0, 0)
+ min := image.Point{}
max := image.Pt(width, height)
bounds := image.Rectangle{min, max}
b := image.NewNRGBA(bounds)
- black := color.Gray16{0}
- draw.Draw(b, bounds, image.NewUniform(black), image.ZP, draw.Src)
+ black := color.Black
+ draw.Draw(b, bounds, image.NewUniform(black), image.Point{}, draw.Src)
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
fEsc := mandelbrot(complex(
go run ./mandelbrot
# or `go run ./mandelbrot/main.go
Enjoy the mandelbrot.png
.