Ruleguard by example: Autofixable rules

package gorules

import "github.com/quasilyte/go-ruleguard/dsl"

func stringsSimplify(m dsl.Matcher) {
	// Some issues have simple fixes that can be expressed as
	// a replacement pattern. Rules can use Suggest() function
	// to add a quickfix action for such issues.
	m.Match(`strings.Replace($s, $old, $new, -1)`).
		Report(`this Replace() call can be simplified`).
		Suggest(`strings.ReplaceAll($s, $old, $new)`)

	// Suggest() can be used without Report().
	// It'll print the suggested template to the user.
	m.Match(`strings.Count($s1, $s2) == 0`).
		Suggest(`!strings.Contains($s1, $s2)`)
}

If we'll run ruleguard with the -fix flag, quickfix actions will be applied inplace.

+ strings.ReplaceAll(s, "old", "new")
- strings.Replace(s, "old", "new", -1)
+ if !strings.Contains(s, "#golang") {
- if strings.Count(s, "#golang") == 0 {
package main

import "strings"

func main() {
	var s string

	_ = strings.Replace(s, "old", "new", -1)
	_ = strings.Replace(s, "old", "new", 10) // Not matched

	if strings.Count(s, "#golang") == 0 {
		println("no golang tags?")
	}
}

Notes: