Skip to content

Commit

Permalink
feat: add config option to extend ignoreSigs (#56)
Browse files Browse the repository at this point in the history
The use case for this is I would like to add another signature to
ignore, but still being able to rely on the default list is convenient,
since I don't have to manually sync my list with any future changes to
the defaults.

closes #54
  • Loading branch information
matthewhughes934 authored Nov 16, 2024
1 parent 4a8f079 commit e8cc4d2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ ignoreSigs:
- .WithMessagef(
- .WithStack(


# An array of strings specifying additional substrings of signatures to ignore.
# Unlike ignoreSigs, this option extends the default set (or the set specified
# in ignoreSigs) without replacing it entirely. This allows you to add specific
# signatures to the ignore list while retaining the defaults or any items in
# ignoreSigs.
extraIgnoreSigs:
- .CustomError(
- .SpecificWrap(

# An array of strings which specify regular expressions of signatures to ignore.
# This is similar to the ignoreSigs configuration above, but gives slightly more
# flexibility.
Expand Down
5 changes: 5 additions & 0 deletions wrapcheck/testdata/config_extraIgnoreSigs/.wrapcheck.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extraIgnoreSigs:
- json.Marshal(

ignoreSigs:
- errors.New(
27 changes: 27 additions & 0 deletions wrapcheck/testdata/config_extraIgnoreSigs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"encoding/json"
"errors"
)

func main() {
do()
}

func do() error {
// no issue with function in 'extraIgnoreSigs'
_, err := json.Marshal(struct{}{})
if err != nil {
return err
}

// expect issue for function that is not ignored
res := struct{}{}
if err := json.Unmarshal([]byte("{}"), &res); err != nil {
return err // want `error returned from external package is unwrapped`
}

// no issue with function in 'ignoreSigs'
return errors.New("Some error")
}
6 changes: 5 additions & 1 deletion wrapcheck/wrapcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type WrapcheckConfig struct {
// list to your config.
IgnoreSigs []string `mapstructure:"ignoreSigs" yaml:"ignoreSigs"`

// ExtraIgnoreSigs defines an additional list of signatures to ignore, on
// top of IgnoreSigs.
ExtraIgnoreSigs []string `mapstructure:"extraIgnoreSigs" yaml:"extraIgnoreSigs"`

// IgnoreSigRegexps defines a list of regular expressions which if matched
// to the signature of the function call returning the error, will be ignored. This
// allows you to specify functions that wrapcheck will not report as
Expand Down Expand Up @@ -276,7 +280,7 @@ func reportUnwrapped(

// Check for ignored signatures
fnSig := pass.TypesInfo.ObjectOf(sel.Sel).String()
if contains(cfg.IgnoreSigs, fnSig) {
if contains(cfg.IgnoreSigs, fnSig) || contains(cfg.ExtraIgnoreSigs, fnSig) {
return
} else if containsMatch(regexpsSig, fnSig) {
return
Expand Down

0 comments on commit e8cc4d2

Please sign in to comment.