From 6353a3baf8f3e5f8f7791816a2ea9434bdc66e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=AE=9D=E5=BC=BA?= Date: Tue, 3 May 2022 10:02:56 +0800 Subject: [PATCH 1/2] 1.adjust reg compile code location 2.pre alloc regs slice cap 3.fix typo --- wrapcheck/wrapcheck.go | 51 ++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/wrapcheck/wrapcheck.go b/wrapcheck/wrapcheck.go index 3d492ee..09647fe 100644 --- a/wrapcheck/wrapcheck.go +++ b/wrapcheck/wrapcheck.go @@ -74,7 +74,7 @@ type WrapcheckConfig struct { // to a underlying interface name, will ignore unwrapped errors returned from a // function whose call is defined on the given interface. // - // For example, an ignoreInterfaceRegexps of `[]string{"Transac(tor|tion)"}`` will ignore errors + // For example, an ignoreInterfaceRegexps of `[]string{"Transac(tor|tion)"}` will ignore errors // returned from any function whose call is defined on a interface named 'Transactor' // or 'Transaction' due to the name matching the regular expression `Transac(tor|tion)`. IgnoreInterfaceRegexps []string `mapstructure:"ignoreInterfaceRegexps" yaml:"ignoreInterfaceRegexps"` @@ -98,24 +98,27 @@ func NewAnalyzer(cfg WrapcheckConfig) *analysis.Analyzer { } func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) { - // Precompile the regexps, report the error - var ( - ignoreSigRegexp []*regexp.Regexp - ignoreInterfaceRegexps []*regexp.Regexp - ignorePackageGlobs []glob.Glob - err error - ) - - ignoreSigRegexp, err = compileRegexps(cfg.IgnoreSigRegexps) - if err == nil { - ignoreInterfaceRegexps, err = compileRegexps(cfg.IgnoreInterfaceRegexps) - } - if err == nil { - ignorePackageGlobs, err = compileGlobs(cfg.IgnorePackageGlobs) - - } return func(pass *analysis.Pass) (interface{}, error) { + // Precompile the regexps, report the error + var ( + ignoreSigRegexp []*regexp.Regexp + ignoreInterfaceRegexps []*regexp.Regexp + ignorePackageGlobs []glob.Glob + err error + ) + + ignoreSigRegexp, err = compileRegexps(cfg.IgnoreSigRegexps) + if err != nil { + return nil, err + } + + ignoreInterfaceRegexps, err = compileRegexps(cfg.IgnoreInterfaceRegexps) + if err != nil { + return nil, err + } + + ignorePackageGlobs, err = compileGlobs(cfg.IgnorePackageGlobs) if err != nil { return nil, err } @@ -305,7 +308,7 @@ func isFromOtherPkg(pass *analysis.Pass, sel *ast.SelectorExpr, pkgGlobs []glob. // `=`. This does not include `var` statements. This function will return nil if // the only declaration is a `var` (aka ValueSpec) declaration. func prevErrAssign(pass *analysis.Pass, file *ast.File, returnIdent *ast.Ident) *ast.AssignStmt { - // A slice containing all the assignments which contain an identifer + // A slice containing all the assignments which contain an identifier // referring to the source declaration of the error. This is to catch // cases where err is defined once, and then reassigned multiple times // within the same block. In these cases, we should check the method of @@ -399,14 +402,14 @@ func isUnresolved(file *ast.File, ident *ast.Ident) bool { // compileRegexps compiles a set of regular expressions returning them for use, // or the first encountered error due to an invalid expression. func compileRegexps(regexps []string) ([]*regexp.Regexp, error) { - var compiledRegexps []*regexp.Regexp - for _, reg := range regexps { + compiledRegexps := make([]*regexp.Regexp, len(regexps)) + for idx, reg := range regexps { re, err := regexp.Compile(reg) if err != nil { return nil, fmt.Errorf("unable to compile regexp %s: %v\n", reg, err) } - compiledRegexps = append(compiledRegexps, re) + compiledRegexps[idx] = re } return compiledRegexps, nil @@ -415,14 +418,14 @@ func compileRegexps(regexps []string) ([]*regexp.Regexp, error) { // compileGlobs compiles a set of globs, returning them for use, // or the first encountered error due to an invalid expression. func compileGlobs(globs []string) ([]glob.Glob, error) { - var compiledGlobs []glob.Glob - for _, globString := range globs { + compiledGlobs := make([]glob.Glob, len(globs)) + for idx, globString := range globs { glob, err := glob.Compile(globString) if err != nil { return nil, fmt.Errorf("unable to compile globs %s: %v\n", glob, err) } - compiledGlobs = append(compiledGlobs, glob) + compiledGlobs[idx] = glob } return compiledGlobs, nil } From 603ca29f0468428d467dd7a1d886270255ed6959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=AE=9D=E5=BC=BA?= Date: Fri, 13 May 2022 09:57:44 +0800 Subject: [PATCH 2/2] revert the reg location --- wrapcheck/wrapcheck.go | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/wrapcheck/wrapcheck.go b/wrapcheck/wrapcheck.go index 09647fe..b703d57 100644 --- a/wrapcheck/wrapcheck.go +++ b/wrapcheck/wrapcheck.go @@ -98,27 +98,23 @@ func NewAnalyzer(cfg WrapcheckConfig) *analysis.Analyzer { } func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) { - - return func(pass *analysis.Pass) (interface{}, error) { - // Precompile the regexps, report the error - var ( - ignoreSigRegexp []*regexp.Regexp - ignoreInterfaceRegexps []*regexp.Regexp - ignorePackageGlobs []glob.Glob - err error - ) - - ignoreSigRegexp, err = compileRegexps(cfg.IgnoreSigRegexps) - if err != nil { - return nil, err - } - + // Precompile the regexps, report the error + var ( + ignoreSigRegexp []*regexp.Regexp + ignoreInterfaceRegexps []*regexp.Regexp + ignorePackageGlobs []glob.Glob + err error + ) + + ignoreSigRegexp, err = compileRegexps(cfg.IgnoreSigRegexps) + if err == nil { ignoreInterfaceRegexps, err = compileRegexps(cfg.IgnoreInterfaceRegexps) - if err != nil { - return nil, err - } - + } + if err == nil { ignorePackageGlobs, err = compileGlobs(cfg.IgnorePackageGlobs) + } + + return func(pass *analysis.Pass) (interface{}, error) { if err != nil { return nil, err }