Skip to content

Commit a9d3b53

Browse files
authored
chore: prealloc slices, fix typos (#27)
* 1.adjust reg compile code location 2.pre alloc regs slice cap 3.fix typo * revert the reg location
1 parent 3713191 commit a9d3b53

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

wrapcheck/wrapcheck.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type WrapcheckConfig struct {
7474
// to a underlying interface name, will ignore unwrapped errors returned from a
7575
// function whose call is defined on the given interface.
7676
//
77-
// For example, an ignoreInterfaceRegexps of `[]string{"Transac(tor|tion)"}`` will ignore errors
77+
// For example, an ignoreInterfaceRegexps of `[]string{"Transac(tor|tion)"}` will ignore errors
7878
// returned from any function whose call is defined on a interface named 'Transactor'
7979
// or 'Transaction' due to the name matching the regular expression `Transac(tor|tion)`.
8080
IgnoreInterfaceRegexps []string `mapstructure:"ignoreInterfaceRegexps" yaml:"ignoreInterfaceRegexps"`
@@ -112,7 +112,6 @@ func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) {
112112
}
113113
if err == nil {
114114
ignorePackageGlobs, err = compileGlobs(cfg.IgnorePackageGlobs)
115-
116115
}
117116

118117
return func(pass *analysis.Pass) (interface{}, error) {
@@ -305,7 +304,7 @@ func isFromOtherPkg(pass *analysis.Pass, sel *ast.SelectorExpr, pkgGlobs []glob.
305304
// `=`. This does not include `var` statements. This function will return nil if
306305
// the only declaration is a `var` (aka ValueSpec) declaration.
307306
func prevErrAssign(pass *analysis.Pass, file *ast.File, returnIdent *ast.Ident) *ast.AssignStmt {
308-
// A slice containing all the assignments which contain an identifer
307+
// A slice containing all the assignments which contain an identifier
309308
// referring to the source declaration of the error. This is to catch
310309
// cases where err is defined once, and then reassigned multiple times
311310
// within the same block. In these cases, we should check the method of
@@ -399,14 +398,14 @@ func isUnresolved(file *ast.File, ident *ast.Ident) bool {
399398
// compileRegexps compiles a set of regular expressions returning them for use,
400399
// or the first encountered error due to an invalid expression.
401400
func compileRegexps(regexps []string) ([]*regexp.Regexp, error) {
402-
var compiledRegexps []*regexp.Regexp
403-
for _, reg := range regexps {
401+
compiledRegexps := make([]*regexp.Regexp, len(regexps))
402+
for idx, reg := range regexps {
404403
re, err := regexp.Compile(reg)
405404
if err != nil {
406405
return nil, fmt.Errorf("unable to compile regexp %s: %v\n", reg, err)
407406
}
408407

409-
compiledRegexps = append(compiledRegexps, re)
408+
compiledRegexps[idx] = re
410409
}
411410

412411
return compiledRegexps, nil
@@ -415,14 +414,14 @@ func compileRegexps(regexps []string) ([]*regexp.Regexp, error) {
415414
// compileGlobs compiles a set of globs, returning them for use,
416415
// or the first encountered error due to an invalid expression.
417416
func compileGlobs(globs []string) ([]glob.Glob, error) {
418-
var compiledGlobs []glob.Glob
419-
for _, globString := range globs {
417+
compiledGlobs := make([]glob.Glob, len(globs))
418+
for idx, globString := range globs {
420419
glob, err := glob.Compile(globString)
421420
if err != nil {
422421
return nil, fmt.Errorf("unable to compile globs %s: %v\n", glob, err)
423422
}
424423

425-
compiledGlobs = append(compiledGlobs, glob)
424+
compiledGlobs[idx] = glob
426425
}
427426
return compiledGlobs, nil
428427
}

0 commit comments

Comments
 (0)