mirror

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: MIT Imports: 12 Imported by: 6

README ¶

mirror Code Coverage build status

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

🇺🇦 PLEASE HELP ME 🇺🇦

Fundrise for scout drone DJI Matrice 30T for my squad (Ukrainian Forces). See more details at butuzov/README.md

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/mirrror/cmd/mirror@latest

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

Command line

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

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var (
	BytesFunctions = map[string]checker.Violation{
		"NewBuffer": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.NewBufferString",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "bytes",
				Function: "NewBufferString",
			},
			Generate: &checker.Generate{
				Pattern: `NewBuffer($0)`,
				Returns: 1,
			},
		},
		"NewBufferString": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.NewBuffer",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  "bytes",
				Function: "NewBuffer",
			},
			Generate: &checker.Generate{
				Pattern: `NewBufferString($0)`,
				Returns: 1,
			},
		},
		"Compare": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.Compare",
			Args:           []int{0, 1},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "Compare",
			},
			Generate: &checker.Generate{
				Pattern: `Compare($0, $1)`,
				Returns: 1,
			},
		},
		"Contains": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.Contains",
			Args:           []int{0, 1},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "Contains",
			},
			Generate: &checker.Generate{
				Pattern: `Contains($0, $1)`,
				Returns: 1,
			},
		},
		"ContainsAny": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.ContainsAny",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "ContainsAny",
			},
			Generate: &checker.Generate{
				Pattern: `ContainsAny($0, "f")`,
				Returns: 1,
			},
		},
		"ContainsRune": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.ContainsRune",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "ContainsRune",
			},
			Generate: &checker.Generate{
				Pattern: `ContainsRune($0, 'Ñ„')`,
				Returns: 1,
			},
		},
		"Count": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.Count",
			Args:           []int{0, 1},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "Count",
			},
			Generate: &checker.Generate{
				Pattern: `Count($0, $1)`,
				Returns: 1,
			},
		},
		"EqualFold": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.EqualFold",
			Args:           []int{0, 1},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "EqualFold",
			},
			Generate: &checker.Generate{
				Pattern: `EqualFold($0, $1)`,
				Returns: 1,
			},
		},

		"HasPrefix": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.HasPrefix",
			Args:           []int{0, 1},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "HasPrefix",
			},
			Generate: &checker.Generate{
				Pattern: `HasPrefix($0, $1)`,
				Returns: 1,
			},
		},
		"HasSuffix": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.HasSuffix",
			Args:           []int{0, 1},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "HasSuffix",
			},
			Generate: &checker.Generate{
				Pattern: `HasSuffix($0, $1)`,
				Returns: 1,
			},
		},
		"Index": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.Index",
			Args:           []int{0, 1},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "Index",
			},
			Generate: &checker.Generate{
				Pattern: `Index($0, $1)`,
				Returns: 1,
			},
		},
		"IndexAny": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.IndexAny",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "IndexAny",
			},
			Generate: &checker.Generate{
				Pattern: `IndexAny($0, "f")`,
				Returns: 1,
			},
		},
		"IndexByte": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.IndexByte",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "IndexByte",
			},
			Generate: &checker.Generate{
				Pattern: `IndexByte($0, 'f')`,
				Returns: 1,
			},
		},
		"IndexFunc": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.IndexFunc",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "IndexFunc",
			},
			Generate: &checker.Generate{
				Pattern: `IndexFunc($0, func(rune) bool {return true })`,
				Returns: 1,
			},
		},
		"IndexRune": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.IndexRune",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "IndexRune",
			},
			Generate: &checker.Generate{
				Pattern: `IndexRune($0, rune('Ñ„'))`,
				Returns: 1,
			},
		},
		"LastIndex": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.LastIndex",
			Args:           []int{0, 1},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "LastIndex",
			},
			Generate: &checker.Generate{
				Pattern: `LastIndex($0, $1)`,
				Returns: 1,
			},
		},
		"LastIndexAny": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.LastIndexAny",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "LastIndexAny",
			},
			Generate: &checker.Generate{
				Pattern: `LastIndexAny($0, "Ñ„")`,
				Returns: 1,
			},
		},

		"LastIndexByte": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.LastIndexByte",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "LastIndexByte",
			},
			Generate: &checker.Generate{
				Pattern: `LastIndexByte($0, 'f')`,
				Returns: 1,
			},
		},
		"LastIndexFunc": {
			Type:           checker.Function,
			Message:        "avoid allocations with strings.LastIndexAny",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "strings",
				Function: "LastIndexAny",
			},
			Generate: &checker.Generate{
				Pattern: `LastIndexFunc($0, func(rune) bool {return true })`,
				Returns: 1,
			},
		},
	}

	BytesBufferMethods = map[string]checker.Violation{
		"Write": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*bytes.Buffer).WriteString",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Method: "WriteString",
			},
			Generate: &checker.Generate{
				PreCondition: `bb := bytes.Buffer{}`,
				Pattern:      `Write($0)`,
				Returns:      2,
			},
		},
		"WriteString": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*bytes.Buffer).Write",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Method: "Write",
			},
			Generate: &checker.Generate{
				PreCondition: `bb := bytes.Buffer{}`,
				Pattern:      `WriteString($0)`,
				Returns:      2,
			},
		},
	}
)
View Source
var (
	RegexpFunctions = map[string]checker.Violation{
		"Match": {
			Type:           checker.Function,
			Message:        "avoid allocations with regexp.MatchString",
			Args:           []int{1},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Package:  "regexp",
				Function: "MatchString",
			},
			Generate: &checker.Generate{
				Pattern: `Match("foo", $0)`,
				Returns: 2,
			},
		},
		"MatchString": {
			Type:           checker.Function,
			Message:        "avoid allocations with regexp.Match",
			Args:           []int{1},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  "regexp",
				Function: "Match",
			},
			Generate: &checker.Generate{
				Pattern: `MatchString("foo", $0)`,
				Returns: 2,
			},
		},
	}

	// As you see we are not using all of the regexp method because
	// nes we missing return concrete types (bytes or strings)
	// which most probably was intentional.
	RegexpRegexpMethods = map[string]checker.Violation{
		"Match": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).MatchString",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Method: "MatchString",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `Match($0)`,
				Returns:      1,
			},
		},
		"MatchString": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).Match",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Method: "Match",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `MatchString($0)`,
				Returns:      1,
			},
		},
		"FindAllIndex": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).FindAllStringIndex",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Method: "FindAllStringIndex",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindAllIndex($0, 1)`,
				Returns:      1,
			},
		},
		"FindAllStringIndex": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).FindAllIndex",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Method: "FindAllIndex",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindAllStringIndex($0, 1)`,
				Returns:      1,
			},
		},
		"FindAllSubmatchIndex": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).FindAllStringSubmatchIndex",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Method: "FindAllStringSubmatchIndex",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindAllSubmatchIndex($0, 1)`,
				Returns:      1,
			},
		},
		"FindAllStringSubmatchIndex": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).FindAllSubmatchIndex",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Method: "FindAllSubmatchIndex",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindAllStringSubmatchIndex($0, 1)`,
				Returns:      1,
			},
		},
		"FindIndex": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).FindStringIndex",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Method: "FindStringIndex",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindIndex($0)`,
				Returns:      1,
			},
		},
		"FindStringIndex": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).FindStringIndex",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Method: "FindIndex",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindStringIndex($0)`,
				Returns:      1,
			},
		},
		"FindSubmatchIndex": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).FindStringSubmatchIndex",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Method: "FindStringSubmatchIndex",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindSubmatchIndex($0)`,
				Returns:      1,
			},
		},
		"FindStringSubmatchIndex": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*regexp.Regexp).FindSubmatchIndex",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Method: "FindSubmatchIndex",
			},
			Generate: &checker.Generate{
				PreCondition: `re := regexp.MustCompile(".*")`,
				Pattern:      `FindStringSubmatchIndex($0)`,
				Returns:      1,
			},
		},
	}
)
View Source
var (
	StringFunctions = map[string]checker.Violation{
		"Compare": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.Compare",
			Args:           []int{0, 1},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `Compare`,
			},
			Generate: &checker.Generate{
				Pattern: `Compare($0,$1)`,
				Returns: 1,
			},
		},
		"Contains": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.Contains",
			Args:           []int{0, 1},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `Contains`,
			},
			Generate: &checker.Generate{
				Pattern: `Contains($0,$1)`,
				Returns: 1,
			},
		},
		"ContainsAny": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.ContainsAny",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `ContainsAny`,
			},
			Generate: &checker.Generate{
				Pattern: `ContainsAny($0,"foobar")`,
				Returns: 1,
			},
		},
		"ContainsRune": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.ContainsRune",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `ContainsRune`,
			},
			Generate: &checker.Generate{
				Pattern: `ContainsRune($0,'Ñ„')`,
				Returns: 1,
			},
		},
		"Count": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.Count",
			Args:           []int{0, 1},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `Count`,
			},
			Generate: &checker.Generate{
				Pattern: `Count($0, $1)`,
				Returns: 1,
			},
		},
		"EqualFold": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.EqualFold",
			Args:           []int{0, 1},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `EqualFold`,
			},
			Generate: &checker.Generate{
				Pattern: `EqualFold($0,$1)`,
				Returns: 1,
			},
		},
		"HasPrefix": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.HasPrefix",
			Args:           []int{0, 1},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `HasPrefix`,
			},
			Generate: &checker.Generate{
				Pattern: `HasPrefix($0,$1)`,
				Returns: 1,
			},
		},
		"HasSuffix": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.HasSuffix",
			Args:           []int{0, 1},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `HasSuffix`,
			},
			Generate: &checker.Generate{
				Pattern: `HasSuffix($0,$1)`,
				Returns: 1,
			},
		},
		"Index": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.Index",
			Args:           []int{0, 1},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `Index`,
			},
			Generate: &checker.Generate{
				Pattern: `Index($0,$1)`,
				Returns: 1,
			},
		},
		"IndexAny": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.IndexAny",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `IndexAny`,
			},
			Generate: &checker.Generate{
				Pattern: `IndexAny($0, "f")`,
				Returns: 1,
			},
		},
		"IndexByte": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.IndexByte",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `IndexByte`,
			},
			Generate: &checker.Generate{
				Pattern: `IndexByte($0, byte('f'))`,
				Returns: 1,
			},
		},
		"IndexFunc": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.IndexFunc",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `IndexFunc`,
			},
			Generate: &checker.Generate{
				Pattern: `IndexFunc($0,func(r rune) bool { return true })`,
				Returns: 1,
			},
		},
		"IndexRune": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.IndexRune",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `IndexRune`,
			},
			Generate: &checker.Generate{
				Pattern: `IndexRune($0, rune('Ñ„'))`,
				Returns: 1,
			},
		},
		"LastIndex": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.LastIndex",
			Args:           []int{0, 1},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `LastIndex`,
			},
			Generate: &checker.Generate{
				Pattern: `LastIndex($0,$1)`,
				Returns: 1,
			},
		},
		"LastIndexAny": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.LastIndexAny",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `LastIndexAny`,
			},
			Generate: &checker.Generate{
				Pattern: `LastIndexAny($0,"f")`,
				Returns: 1,
			},
		},
		"LastIndexByte": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.LastIndexByte",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `LastIndexByte`,
			},
			Generate: &checker.Generate{
				Pattern: `LastIndexByte($0, byte('f'))`,
				Returns: 1,
			},
		},
		"LastIndexFunc": {
			Type:           checker.Function,
			Message:        "avoid allocations with bytes.LastIndexAny",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Package:  `bytes`,
				Function: `LastIndexAny`,
			},
			Generate: &checker.Generate{
				Pattern: `LastIndexFunc($0, func(r rune) bool { return true })`,
				Returns: 1,
			},
		},
	}

	StringsBuilderMethods = map[string]checker.Violation{
		"Write": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*strings.Builder).WriteString",
			Args:           []int{0},
			StringTargeted: false,
			Alternative: checker.Alternative{
				Method: `WriteString`,
			},
			Generate: &checker.Generate{
				PreCondition: `builder := strings.Builder{}`,
				Pattern:      `Write($0)`,
				Returns:      2,
			},
		},
		"WriteString": {
			Type:           checker.Method,
			Message:        "avoid allocations with (*strings.Builder).Write",
			Args:           []int{0},
			StringTargeted: true,
			Alternative: checker.Alternative{
				Method: `Write`,
			},
			Generate: &checker.Generate{
				PreCondition: `builder := strings.Builder{}`,
				Pattern:      `WriteString($0)`,
				Returns:      2,
			},
		},
	}
)
View Source
var BufioMethods = map[string]checker.Violation{
	"Write": {
		Type:           checker.Method,
		Message:        "avoid allocations with (*bufio.Writer).WriteString",
		Args:           []int{0},
		StringTargeted: false,
		Alternative: checker.Alternative{
			Method: "WriteString",
		},
		Generate: &checker.Generate{
			PreCondition: `b := bufio.Writer{}`,
			Pattern:      `Write($0)`,
			Returns:      2,
		},
	},
	"WriteString": {
		Type:           checker.Method,
		Message:        "avoid allocations with (*bufio.Writer).Write",
		Args:           []int{0},
		StringTargeted: true,
		Alternative: checker.Alternative{
			Method: "Write",
		},
		Generate: &checker.Generate{
			PreCondition: `b := bufio.Writer{}`,
			Pattern:      `WriteString($0)`,
			Returns:      2,
		},
	},
}
View Source
var HTTPTestMethods = map[string]checker.Violation{
	"Write": {
		Type:           checker.Method,
		Message:        "avoid allocations with (*httptest.ResponseRecorder).WriteString",
		Args:           []int{0},
		StringTargeted: false,
		Alternative: checker.Alternative{
			Method: "WriteString",
		},
		Generate: &checker.Generate{
			PreCondition: `h := httptest.ResponseRecorder{}`,
			Pattern:      `Write($0)`,
			Returns:      2,
		},
	},
	"WriteString": {
		Type:           checker.Method,
		Message:        "avoid allocations with (*httptest.ResponseRecorder).Write",
		Args:           []int{0},
		StringTargeted: true,
		Alternative: checker.Alternative{
			Method: "Write",
		},
		Generate: &checker.Generate{
			PreCondition: `h := httptest.ResponseRecorder{}`,
			Pattern:      `WriteString($0)`,
			Returns:      2,
		},
	},
}
View Source
var MaphashMethods = map[string]checker.Violation{
	"Write": {
		Type:           checker.Method,
		Message:        "avoid allocations with (*maphash.Hash).WriteString",
		Args:           []int{0},
		StringTargeted: false,
		Alternative: checker.Alternative{
			Method: "WriteString",
		},
		Generate: &checker.Generate{
			PreCondition: `h := maphash.Hash{}`,
			Pattern:      `Write($0)`,
			Returns:      2,
		},
	},
	"WriteString": {
		Type:           checker.Method,
		Message:        "avoid allocations with (*maphash.Hash).Write",
		Args:           []int{0},
		StringTargeted: true,
		Alternative: checker.Alternative{
			Method: "Write",
		},
		Generate: &checker.Generate{
			PreCondition: `h := maphash.Hash{}`,
			Pattern:      `WriteString($0)`,
			Returns:      2,
		},
	},
}
View Source
var OsFileMethods = map[string]checker.Violation{
	"Write": {
		Type:           checker.Method,
		Message:        "avoid allocations with (*os.File).WriteString",
		Args:           []int{0},
		StringTargeted: false,
		Alternative: checker.Alternative{
			Method: "WriteString",
		},
		Generate: &checker.Generate{
			PreCondition: `f := &os.File{}`,
			Pattern:      `Write($0)`,
			Returns:      2,
		},
	},
	"WriteString": {
		Type:           checker.Method,
		Message:        "avoid allocations with (*os.File).Write",
		Args:           []int{0},
		StringTargeted: true,
		Alternative: checker.Alternative{
			Method: "Write",
		},
		Generate: &checker.Generate{
			PreCondition: `f := &os.File{}`,
			Pattern:      `WriteString($0)`,
			Returns:      2,
		},
	},
}
View Source
var UTF8Functions = map[string]checker.Violation{
	"Valid": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.ValidString",
		Args:           []int{0},
		StringTargeted: false,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "ValidString",
		},
		Generate: &checker.Generate{
			Pattern: `Valid($0)`,
			Returns: 1,
		},
	},
	"ValidString": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.Valid",
		Args:           []int{0},
		StringTargeted: true,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "Valid",
		},
		Generate: &checker.Generate{
			Pattern: `ValidString($0)`,
			Returns: 1,
		},
	},
	"FullRune": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.FullRuneInString",
		Args:           []int{0},
		StringTargeted: false,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "FullRuneInString",
		},
		Generate: &checker.Generate{
			Pattern: `FullRune($0)`,
			Returns: 1,
		},
	},
	"FullRuneInString": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.FullRune",
		Args:           []int{0},
		StringTargeted: true,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "FullRune",
		},
		Generate: &checker.Generate{
			Pattern: `FullRuneInString($0)`,
			Returns: 1,
		},
	},

	"RuneCount": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.RuneCountInString",
		Args:           []int{0},
		StringTargeted: false,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "RuneCountInString",
		},
		Generate: &checker.Generate{
			Pattern: `RuneCount($0)`,
			Returns: 1,
		},
	},
	"RuneCountInString": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.RuneCount",
		Args:           []int{0},
		StringTargeted: true,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "RuneCount",
		},
		Generate: &checker.Generate{
			Pattern: `RuneCountInString($0)`,
			Returns: 1,
		},
	},

	"DecodeLastRune": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.DecodeLastRuneInString",
		Args:           []int{0},
		StringTargeted: false,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "DecodeLastRuneInString",
		},
		Generate: &checker.Generate{
			Pattern: `DecodeLastRune($0)`,
			Returns: 2,
		},
	},
	"DecodeLastRuneInString": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.DecodeLastRune",
		Args:           []int{0},
		StringTargeted: true,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "DecodeLastRune",
		},
		Generate: &checker.Generate{
			Pattern: `DecodeLastRuneInString($0)`,
			Returns: 2,
		},
	},
	"DecodeRune": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.DecodeRuneInString",
		Args:           []int{0},
		StringTargeted: false,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "DecodeRuneInString",
		},
		Generate: &checker.Generate{
			Pattern: `DecodeRune($0)`,
			Returns: 2,
		},
	},
	"DecodeRuneInString": {
		Type:           checker.Function,
		Message:        "avoid allocations with utf8.DecodeRune",
		Args:           []int{0},
		StringTargeted: true,
		Alternative: checker.Alternative{
			Package:  "unicode/utf8",
			Function: "DecodeRune",
		},
		Generate: &checker.Generate{
			Pattern: `DecodeRuneInString($0)`,
			Returns: 2,
		},
	},
}

Functions ¶

func NewAnalyzer ¶

func NewAnalyzer() *analysis.Analyzer

Types ¶

This section is empty.

Directories ¶

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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