mirror

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2024 License: MIT Imports: 7 Imported by: 6

README

mirror Stand with Ukraine Code Coverage build status

mirror suggests use of alternative functions/methods in order to gain performance boosts by avoiding unnecessary []byte/string conversion calls. See MIRROR_FUNCS.md list of mirror functions you can use in go's stdlib.


United 24 Help Oleg Butuzov


Linter Use Cases

github.com/argoproj/argo-cd
// Before
func IsValidHostname(hostname string, fqdn bool) bool {
  if !fqdn {
    return validHostNameRegexp.Match([]byte(hostname)) || validIPv6Regexp.Match([]byte(hostname))
  } else {
    return validFQDNRegexp.Match([]byte(hostname))
  }
}

// After: With alternative method (and lost `else` case)
func IsValidHostname(hostname string, fqdn bool) bool {
  if !fqdn {
    return validHostNameRegexp.MatchString(hostname) || validIPv6Regexp.MatchString(hostname)
  }

  return validFQDNRegexp.MatchString(hostname)
}

Install

go install github.com/butuzov/mirror/cmd/mirror@latest
golangci-lint

golangci-lint supports mirror since v1.53.0

How to use

You run mirror with go vet:

go vet -vettool=$(which mirror) ./...
# github.com/jcmoraisjr/haproxy-ingress/pkg/common/net/ssl
pkg/common/net/ssl/ssl.go:64:11: avoid allocations with (*os.File).WriteString
pkg/common/net/ssl/ssl.go:161:12: avoid allocations with (*os.File).WriteString
pkg/common/net/ssl/ssl.go:166:3: avoid allocations with (*os.File).WriteString

Can be called directly:

mirror ./...
# https://github.com/cosmtrek/air
/air/runner/util.go:149:6: avoid allocations with (*regexp.Regexp).MatchString
/air/runner/util.go:173:14: avoid allocations with (*os.File).WriteString

With golangci-lint

golangci-lint run --no-config --disable-all -Emirror
# github.com/argoproj/argo-cd
test/e2e/fixture/app/actions.go:83:11: avoid allocations with (*os.File).WriteString (mirror)
	_, err = tmpFile.Write([]byte(data))
	         ^
server/server.go:1166:9: avoid allocations with (*regexp.Regexp).MatchString (mirror)
	return mainJsBundleRegex.Match([]byte(filename))
	       ^
server/account/account.go:91:6: avoid allocations with (*regexp.Regexp).MatchString (mirror)
	if !validPasswordRegexp.Match([]byte(q.NewPassword)) {
	    ^
server/badge/badge.go:52:20: avoid allocations with (*regexp.Regexp).FindAllStringSubmatchIndex (mirror)
	for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
	                  ^
util/cert/cert.go:82:10: avoid allocations with (*regexp.Regexp).MatchString (mirror)
		return validHostNameRegexp.Match([]byte(hostname)) || validIPv6Regexp.Match([]byte(hostname))

Command line

  • You can add checks for _test.go files with cli option --with-tests
golangci-lint

With golangci-lint tests are checked by default and can be can be turned off by using the regular golangci-lint ways to do it:

  • flag --tests (e.g. --tests=false)
  • flag --skip-files (e.g. --skip-files="_test.go")
  • yaml configuration run.skip-files:
    run:
      skip-files:
        - '(.+)_test\.go'
    
  • yaml configuration issues.exclude-rules:
      issues:
        exclude-rules:
          - path: '(.+)_test\.go'
            linters:
              - mirror
    

Contributing

# Update Assets (testdata/(strings|bytes|os|utf8|maphash|regexp|bufio).go)
(task|make) generate
# Run Tests
(task|make) tests
# Lint Code
(task|make) lints

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BytesFunctions = []checker.Violation{
		{
			Targets:   checker.Bytes,
			Type:      checker.Function,
			Package:   "bytes",
			Caller:    "NewBuffer",
			Args:      []int{0},
			AltCaller: "NewBufferString",

			Generate: &checker.Generate{
				Pattern: `NewBuffer($0)`,
				Returns: []string{"*bytes.Buffer"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Function,
			Package:   "bytes",
			Caller:    "NewBufferString",
			Args:      []int{0},
			AltCaller: "NewBuffer",

			Generate: &checker.Generate{
				Pattern: `NewBufferString($0)`,
				Returns: []string{"*bytes.Buffer"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "Compare",
			Args:       []int{0, 1},
			AltPackage: "strings",
			AltCaller:  "Compare",

			Generate: &checker.Generate{
				Pattern: `Compare($0, $1)`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "Contains",
			Args:       []int{0, 1},
			AltPackage: "strings",
			AltCaller:  "Contains",

			Generate: &checker.Generate{
				Pattern: `Contains($0, $1)`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "ContainsAny",
			Args:       []int{0},
			AltPackage: "strings",
			AltCaller:  "ContainsAny",

			Generate: &checker.Generate{
				Pattern: `ContainsAny($0, "f")`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "ContainsRune",
			Args:       []int{0},
			AltPackage: "strings",
			AltCaller:  "ContainsRune",

			Generate: &checker.Generate{
				Pattern: `ContainsRune($0, 'ф')`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "Count",
			Args:       []int{0, 1},
			AltPackage: "strings",
			AltCaller:  "Count",

			Generate: &checker.Generate{
				Pattern: `Count($0, $1)`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "EqualFold",
			Args:       []int{0, 1},
			AltPackage: "strings",
			AltCaller:  "EqualFold",

			Generate: &checker.Generate{
				Pattern: `EqualFold($0, $1)`,
				Returns: []string{"bool"},
			},
		},

		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "HasPrefix",
			Args:       []int{0, 1},
			AltPackage: "strings",
			AltCaller:  "HasPrefix",

			Generate: &checker.Generate{
				Pattern: `HasPrefix($0, $1)`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "HasSuffix",
			Args:       []int{0, 1},
			AltPackage: "strings",
			AltCaller:  "HasSuffix",

			Generate: &checker.Generate{
				Pattern: `HasSuffix($0, $1)`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "Index",
			Args:       []int{0, 1},
			AltPackage: "strings",
			AltCaller:  "Index",

			Generate: &checker.Generate{
				Pattern: `Index($0, $1)`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "IndexAny",
			Args:       []int{0},
			AltPackage: "strings",
			AltCaller:  "IndexAny",

			Generate: &checker.Generate{
				Pattern: `IndexAny($0, "f")`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "IndexByte",
			Args:       []int{0},
			AltPackage: "strings",
			AltCaller:  "IndexByte",

			Generate: &checker.Generate{
				Pattern: `IndexByte($0, 'f')`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "IndexFunc",
			Args:       []int{0},
			AltPackage: "strings",
			AltCaller:  "IndexFunc",

			Generate: &checker.Generate{
				Pattern: `IndexFunc($0, func(rune) bool {return true })`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "IndexRune",
			Args:       []int{0},
			AltPackage: "strings",
			AltCaller:  "IndexRune",

			Generate: &checker.Generate{
				Pattern: `IndexRune($0, rune('ф'))`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "LastIndex",
			Args:       []int{0, 1},
			AltPackage: "strings",
			AltCaller:  "LastIndex",

			Generate: &checker.Generate{
				Pattern: `LastIndex($0, $1)`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "LastIndexAny",
			Args:       []int{0},
			AltPackage: "strings",
			AltCaller:  "LastIndexAny",

			Generate: &checker.Generate{
				Pattern: `LastIndexAny($0, "ф")`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "LastIndexByte",
			Args:       []int{0},
			AltPackage: "strings",
			AltCaller:  "LastIndexByte",

			Generate: &checker.Generate{
				Pattern: `LastIndexByte($0, 'f')`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Bytes,
			Type:       checker.Function,
			Package:    "bytes",
			Caller:     "LastIndexFunc",
			Args:       []int{0},
			AltPackage: "strings",
			AltCaller:  "LastIndexFunc",

			Generate: &checker.Generate{
				Pattern: `LastIndexFunc($0, func(rune) bool {return true })`,
				Returns: []string{"int"},
			},
		},
	}

	BytesBufferMethods = []checker.Violation{
		{
			Targets:   checker.Bytes,
			Type:      checker.Method,
			Package:   "bytes",
			Struct:    "Buffer",
			Caller:    "Write",
			Args:      []int{0},
			AltCaller: "WriteString",

			Generate: &checker.Generate{
				PreCondition: `bb := bytes.Buffer{}`,
				Pattern:      `Write($0)`,
				Returns:      []string{"int", "error"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "bytes",
			Struct:    "Buffer",
			Caller:    "WriteString",
			Args:      []int{0},
			AltCaller: "Write",

			Generate: &checker.Generate{
				PreCondition: `bb := bytes.Buffer{}`,
				Pattern:      `WriteString($0)`,
				Returns:      []string{"int", "error"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "bytes",
			Struct:    "Buffer",
			Caller:    "WriteString",
			Args:      []int{0},
			ArgsType:  checker.Rune,
			AltCaller: "WriteRune",
			Generate: &checker.Generate{
				SkipGenerate: true,
				Pattern:      `WriteString($0)`,
				Returns:      []string{"int", "error"},
			},
		},
	}
)
View Source
var (
	MaphashFunctions = []checker.Violation{
		{
			Targets:   checker.Bytes,
			Type:      checker.Function,
			Package:   "hash/maphash",
			Caller:    "Bytes",
			Args:      []int{1},
			AltCaller: "String",

			Generate: &checker.Generate{
				Pattern: `Bytes(maphash.MakeSeed(), $0)`,
				Returns: []string{"uint64"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Function,
			Package:   "hash/maphash",
			Caller:    "String",
			Args:      []int{1},
			AltCaller: "Bytes",

			Generate: &checker.Generate{
				Pattern: `String(maphash.MakeSeed(), $0)`,
				Returns: []string{"uint64"},
			},
		},
	}

	MaphashMethods = []checker.Violation{
		{
			Targets:   checker.Bytes,
			Type:      checker.Method,
			Package:   "hash/maphash",
			Struct:    "Hash",
			Caller:    "Write",
			Args:      []int{0},
			AltCaller: "WriteString",

			Generate: &checker.Generate{
				PreCondition: `h := maphash.Hash{}`,
				Pattern:      `Write($0)`,
				Returns:      []string{"int", "error"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "hash/maphash",
			Struct:    "Hash",
			Caller:    "WriteString",
			Args:      []int{0},
			AltCaller: "Write",

			Generate: &checker.Generate{
				PreCondition: `h := maphash.Hash{}`,
				Pattern:      `WriteString($0)`,
				Returns:      []string{"int", "error"},
			},
		},
	}
)
View Source
var (
	RegexpFunctions = []checker.Violation{
		{
			Targets:   checker.Bytes,
			Type:      checker.Function,
			Package:   "regexp",
			Caller:    "Match",
			Args:      []int{1},
			AltCaller: "MatchString",

			Generate: &checker.Generate{
				Pattern: `Match("foo", $0)`,
				Returns: []string{"bool", "error"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Function,
			Package:   "regexp",
			Caller:    "MatchString",
			Args:      []int{1},
			AltCaller: "Match",

			Generate: &checker.Generate{
				Pattern: `MatchString("foo", $0)`,
				Returns: []string{"bool", "error"},
			},
		},
	}

	RegexpRegexpMethods = []checker.Violation{
		{
			Targets:   checker.Bytes,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "Match",
			Args:      []int{0},
			AltCaller: "MatchString",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `Match($0)`,
				Returns:      []string{"bool"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "MatchString",
			Args:      []int{0},
			AltCaller: "Match",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `MatchString($0)`,
				Returns:      []string{"bool"},
			},
		},
		{
			Targets:   checker.Bytes,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "FindAllIndex",
			Args:      []int{0},
			AltCaller: "FindAllStringIndex",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindAllIndex($0, 1)`,
				Returns:      []string{"[][]int"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "FindAllStringIndex",
			Args:      []int{0},
			AltCaller: "FindAllIndex",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindAllStringIndex($0, 1)`,
				Returns:      []string{"[][]int"},
			},
		},
		{
			Targets:   checker.Bytes,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "FindAllSubmatchIndex",
			Args:      []int{0},
			AltCaller: "FindAllStringSubmatchIndex",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindAllSubmatchIndex($0, 1)`,
				Returns:      []string{"[][]int"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "FindAllStringSubmatchIndex",
			Args:      []int{0},
			AltCaller: "FindAllSubmatchIndex",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindAllStringSubmatchIndex($0, 1)`,
				Returns:      []string{"[][]int"},
			},
		},
		{
			Targets:   checker.Bytes,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "FindIndex",
			Args:      []int{0},
			AltCaller: "FindStringIndex",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindIndex($0)`,
				Returns:      []string{"[]int"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "FindStringIndex",
			Args:      []int{0},
			AltCaller: "FindIndex",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindStringIndex($0)`,
				Returns:      []string{"[]int"},
			},
		},
		{
			Targets:   checker.Bytes,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "FindSubmatchIndex",
			Args:      []int{0},
			AltCaller: "FindStringSubmatchIndex",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindSubmatchIndex($0)`,
				Returns:      []string{"[]int"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "regexp",
			Struct:    "Regexp",
			Caller:    "FindStringSubmatchIndex",
			Args:      []int{0},
			AltCaller: "FindSubmatchIndex",

			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindStringSubmatchIndex($0)`,
				Returns:      []string{"[]int"},
			},
		},
	}
)
View Source
var (
	StringFunctions = []checker.Violation{
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "Compare",
			Args:       []int{0, 1},
			AltPackage: "bytes",
			AltCaller:  "Compare",

			Generate: &checker.Generate{
				Pattern: `Compare($0,$1)`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "Contains",
			Args:       []int{0, 1},
			AltPackage: "bytes",
			AltCaller:  "Contains",

			Generate: &checker.Generate{
				Pattern: `Contains($0,$1)`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "ContainsAny",
			Args:       []int{0},
			AltPackage: "bytes",
			AltCaller:  "ContainsAny",

			Generate: &checker.Generate{
				Pattern: `ContainsAny($0,"foobar")`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "ContainsRune",
			Args:       []int{0},
			AltPackage: "bytes",
			AltCaller:  "ContainsRune",

			Generate: &checker.Generate{
				Pattern: `ContainsRune($0,'ф')`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "Count",
			Args:       []int{0, 1},
			AltPackage: "bytes",
			AltCaller:  "Count",

			Generate: &checker.Generate{
				Pattern: `Count($0, $1)`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "EqualFold",
			Args:       []int{0, 1},
			AltPackage: "bytes",
			AltCaller:  "EqualFold",

			Generate: &checker.Generate{
				Pattern: `EqualFold($0,$1)`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "HasPrefix",
			Args:       []int{0, 1},
			AltPackage: "bytes",
			AltCaller:  "HasPrefix",

			Generate: &checker.Generate{
				Pattern: `HasPrefix($0,$1)`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "HasSuffix",
			Args:       []int{0, 1},
			AltPackage: "bytes",
			AltCaller:  "HasSuffix",

			Generate: &checker.Generate{
				Pattern: `HasSuffix($0,$1)`,
				Returns: []string{"bool"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "Index",
			Args:       []int{0, 1},
			AltPackage: "bytes",
			AltCaller:  "Index",

			Generate: &checker.Generate{
				Pattern: `Index($0,$1)`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "IndexAny",
			Args:       []int{0},
			AltPackage: "bytes",
			AltCaller:  "IndexAny",

			Generate: &checker.Generate{
				Pattern: `IndexAny($0, "f")`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "IndexByte",
			Args:       []int{0},
			AltPackage: "bytes",
			AltCaller:  "IndexByte",

			Generate: &checker.Generate{
				Pattern: `IndexByte($0, 'f')`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "IndexFunc",
			Args:       []int{0},
			AltPackage: "bytes",
			AltCaller:  "IndexFunc",

			Generate: &checker.Generate{
				Pattern: `IndexFunc($0, func(r rune) bool { return true })`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "IndexRune",
			Args:       []int{0},
			AltPackage: "bytes",
			AltCaller:  "IndexRune",

			Generate: &checker.Generate{
				Pattern: `IndexRune($0, rune('ф'))`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "LastIndex",
			Args:       []int{0, 1},
			AltPackage: "bytes",
			AltCaller:  "LastIndex",

			Generate: &checker.Generate{
				Pattern: `LastIndex($0,$1)`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "LastIndexAny",
			Args:       []int{0},
			AltPackage: "bytes",
			AltCaller:  "LastIndexAny",

			Generate: &checker.Generate{
				Pattern: `LastIndexAny($0,"f")`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "LastIndexByte",
			Args:       []int{0},
			AltPackage: "bytes",
			AltCaller:  "LastIndexByte",

			Generate: &checker.Generate{
				Pattern: `LastIndexByte($0, 'f')`,
				Returns: []string{"int"},
			},
		},
		{
			Targets:    checker.Strings,
			Type:       checker.Function,
			Package:    "strings",
			Caller:     "LastIndexFunc",
			Args:       []int{0},
			AltPackage: "bytes",
			AltCaller:  "LastIndexFunc",

			Generate: &checker.Generate{
				Pattern: `LastIndexFunc($0, func(r rune) bool { return true })`,
				Returns: []string{"int"},
			},
		},
	}

	StringsBuilderMethods = []checker.Violation{
		{
			Targets:   checker.Bytes,
			Type:      checker.Method,
			Package:   "strings",
			Struct:    "Builder",
			Caller:    "Write",
			Args:      []int{0},
			AltCaller: "WriteString",

			Generate: &checker.Generate{
				PreCondition: `builder := strings.Builder{}`,
				Pattern:      `Write($0)`,
				Returns:      []string{"int", "error"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "strings",
			Struct:    "Builder",
			Caller:    "WriteString",
			Args:      []int{0},
			AltCaller: "Write",

			Generate: &checker.Generate{
				PreCondition: `builder := strings.Builder{}`,
				Pattern:      `WriteString($0)`,
				Returns:      []string{"int", "error"},
			},
		},
		{
			Targets:   checker.Strings,
			Type:      checker.Method,
			Package:   "strings",
			Struct:    "Builder",
			Caller:    "WriteString",
			Args:      []int{0},
			ArgsType:  checker.Rune,
			AltCaller: "WriteRune",
			Generate: &checker.Generate{
				SkipGenerate: true,
				Pattern:      `WriteString($0)`,
				Returns:      []string{"int", "error"},
			},
		},
	}
)
View Source
var BufioMethods = []checker.Violation{
	{
		Targets:   checker.Bytes,
		Type:      checker.Method,
		Package:   "bufio",
		Struct:    "Writer",
		Caller:    "Write",
		Args:      []int{0},
		AltCaller: "WriteString",

		Generate: &checker.Generate{
			PreCondition: `b := bufio.Writer{}`,
			Pattern:      `Write($0)`,
			Returns:      []string{"int", "error"},
		},
	},
	{
		Type:      checker.Method,
		Targets:   checker.Strings,
		Package:   "bufio",
		Struct:    "Writer",
		Caller:    "WriteString",
		Args:      []int{0},
		AltCaller: "Write",

		Generate: &checker.Generate{
			PreCondition: `b := bufio.Writer{}`,
			Pattern:      `WriteString($0)`,
			Returns:      []string{"int", "error"},
		},
	},
	{
		Targets:   checker.Strings,
		Type:      checker.Method,
		Package:   "bufio",
		Struct:    "Writer",
		Caller:    "WriteString",
		Args:      []int{0},
		ArgsType:  checker.Rune,
		AltCaller: "WriteRune",

		Generate: &checker.Generate{
			SkipGenerate: true,
			Pattern:      `WriteString($0)`,
			Returns:      []string{"int", "error"},
		},
	},
}
View Source
var HTTPTestMethods = []checker.Violation{
	{
		Targets:   checker.Bytes,
		Type:      checker.Method,
		Package:   "net/http/httptest",
		Struct:    "ResponseRecorder",
		Caller:    "Write",
		Args:      []int{0},
		AltCaller: "WriteString",

		Generate: &checker.Generate{
			PreCondition: `h := httptest.ResponseRecorder{}`,
			Pattern:      `Write($0)`,
			Returns:      []string{"int", "error"},
		},
	},
	{
		Targets:   checker.Strings,
		Type:      checker.Method,
		Package:   "net/http/httptest",
		Struct:    "ResponseRecorder",
		Caller:    "WriteString",
		Args:      []int{0},
		AltCaller: "Write",

		Generate: &checker.Generate{
			PreCondition: `h := httptest.ResponseRecorder{}`,
			Pattern:      `WriteString($0)`,
			Returns:      []string{"int", "error"},
		},
	},
}
View Source
var OsFileMethods = []checker.Violation{
	{
		Targets:   checker.Bytes,
		Type:      checker.Method,
		Package:   "os",
		Struct:    "File",
		Caller:    "Write",
		Args:      []int{0},
		AltCaller: "WriteString",

		Generate: &checker.Generate{
			PreCondition: `f := &os.File{}`,
			Pattern:      `Write($0)`,
			Returns:      []string{"int", "error"},
		},
	},
	{
		Targets:   checker.Strings,
		Type:      checker.Method,
		Package:   "os",
		Struct:    "File",
		Caller:    "WriteString",
		Args:      []int{0},
		AltCaller: "Write",

		Generate: &checker.Generate{
			PreCondition: `f := &os.File{}`,
			Pattern:      `WriteString($0)`,
			Returns:      []string{"int", "error"},
		},
	},
}
View Source
var UTF8Functions = []checker.Violation{
	{
		Type:      checker.Function,
		Targets:   checker.Bytes,
		Package:   "unicode/utf8",
		Caller:    "Valid",
		Args:      []int{0},
		AltCaller: "ValidString",

		Generate: &checker.Generate{
			Pattern: `Valid($0)`,
			Returns: []string{"bool"},
		},
	},
	{
		Targets:   checker.Strings,
		Type:      checker.Function,
		Package:   "unicode/utf8",
		Caller:    "ValidString",
		Args:      []int{0},
		AltCaller: "Valid",

		Generate: &checker.Generate{
			Pattern: `ValidString($0)`,
			Returns: []string{"bool"},
		},
	},
	{
		Targets:   checker.Bytes,
		Type:      checker.Function,
		Package:   "unicode/utf8",
		Caller:    "FullRune",
		Args:      []int{0},
		AltCaller: "FullRuneInString",

		Generate: &checker.Generate{
			Pattern: `FullRune($0)`,
			Returns: []string{"bool"},
		},
	},
	{
		Targets:   checker.Strings,
		Type:      checker.Function,
		Package:   "unicode/utf8",
		Caller:    "FullRuneInString",
		Args:      []int{0},
		AltCaller: "FullRune",

		Generate: &checker.Generate{
			Pattern: `FullRuneInString($0)`,
			Returns: []string{"bool"},
		},
	},

	{
		Targets:   checker.Bytes,
		Type:      checker.Function,
		Package:   "unicode/utf8",
		Caller:    "RuneCount",
		Args:      []int{0},
		AltCaller: "RuneCountInString",

		Generate: &checker.Generate{
			Pattern: `RuneCount($0)`,
			Returns: []string{"int"},
		},
	},
	{
		Targets:   checker.Strings,
		Type:      checker.Function,
		Package:   "unicode/utf8",
		Caller:    "RuneCountInString",
		Args:      []int{0},
		AltCaller: "RuneCount",

		Generate: &checker.Generate{
			Pattern: `RuneCountInString($0)`,
			Returns: []string{"int"},
		},
	},

	{
		Targets:   checker.Bytes,
		Type:      checker.Function,
		Package:   "unicode/utf8",
		Caller:    "DecodeLastRune",
		Args:      []int{0},
		AltCaller: "DecodeLastRuneInString",

		Generate: &checker.Generate{
			Pattern: `DecodeLastRune($0)`,
			Returns: []string{"rune", "int"},
		},
	},
	{
		Targets:   checker.Strings,
		Type:      checker.Function,
		Package:   "unicode/utf8",
		Caller:    "DecodeLastRuneInString",
		Args:      []int{0},
		AltCaller: "DecodeLastRune",

		Generate: &checker.Generate{
			Pattern: `DecodeLastRuneInString($0)`,
			Returns: []string{"rune", "int"},
		},
	},
	{
		Targets:   checker.Bytes,
		Type:      checker.Function,
		Package:   "unicode/utf8",
		Caller:    "DecodeRune",
		Args:      []int{0},
		AltCaller: "DecodeRuneInString",

		Generate: &checker.Generate{
			Pattern: `DecodeRune($0)`,
			Returns: []string{"rune", "int"},
		},
	},
	{
		Targets:   checker.Strings,
		Type:      checker.Function,
		Package:   "unicode/utf8",
		Args:      []int{0},
		Caller:    "DecodeRuneInString",
		AltCaller: "DecodeRune",

		Generate: &checker.Generate{
			Pattern: `DecodeRuneInString($0)`,
			Returns: []string{"rune", "int"},
		},
	},
}

Functions

func NewAnalyzer

func NewAnalyzer() *analysis.Analyzer

func Run added in v0.2.0

func Run(pass *analysis.Pass, withTests bool) []*checker.Violation

Types

This section is empty.

Directories

Path Synopsis
cmd
internal/mirror-table
This generates MIRROR_FUNCS.md
This generates MIRROR_FUNCS.md
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL