Skip to content

go-ruleguard/demo-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Demo project that uses ruleguard

Exploring the rules

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 the go.mod
  • Since we have tests for the rules, we also need golang.org/x/tools package for analysistest

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.

Running the rules

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{}

Auto-fixing the code

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(

Running the mandelbrot

go run ./mandelbrot

# or `go run ./mandelbrot/main.go

Enjoy the mandelbrot.png.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages