Skip to content

Commit

Permalink
Merge pull request #141 from kisielk/ignore-fmt
Browse files Browse the repository at this point in the history
Only ignore select functions from the fmt package by default.
  • Loading branch information
kisielk authored May 17, 2018
2 parents 8050dd7 + a174de0 commit e5700e7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 37 deletions.
40 changes: 27 additions & 13 deletions internal/errcheck/errcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,35 @@ func NewChecker() *Checker {
}

func (c *Checker) SetExclude(l map[string]bool) {
// Default exclude for stdlib functions
c.exclude = map[string]bool{
"math/rand.Read": true,
"(*math/rand.Rand).Read": true,

"(*bytes.Buffer).Write": true,
"(*bytes.Buffer).WriteByte": true,
"(*bytes.Buffer).WriteRune": true,
"(*bytes.Buffer).WriteString": true,
c.exclude = map[string]bool{}

"(*strings.Builder).Write": true,
"(*strings.Builder).WriteByte": true,
"(*strings.Builder).WriteRune": true,
"(*strings.Builder).WriteString": true,
// Default exclude for stdlib functions
for _, exc := range []string{
// bytes
"(*bytes.Buffer).Write",
"(*bytes.Buffer).WriteByte",
"(*bytes.Buffer).WriteRune",
"(*bytes.Buffer).WriteString",

// fmt
"fmt.Errorf",
"fmt.Print",
"fmt.Printf",
"fmt.Println",

// math/rand
"math/rand.Read",
"(*math/rand.Rand).Read",

// strings
"(*strings.Builder).Write",
"(*strings.Builder).WriteByte",
"(*strings.Builder).WriteRune",
"(*strings.Builder).WriteString",
} {
c.exclude[exc] = true
}

for k := range l {
c.exclude[k] = true
}
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ func parseFlags(checker *errcheck.Checker, args []string) ([]string, int) {
tags := tagsFlag{}
flags.Var(&tags, "tags", "space-separated list of build tags to include")
ignorePkg := flags.String("ignorepkg", "", "comma-separated list of package paths to ignore")
ignore := ignoreFlag(map[string]*regexp.Regexp{
"fmt": dotStar,
})
ignore := ignoreFlag(map[string]*regexp.Regexp{})
flags.Var(ignore, "ignore", "[deprecated] comma-separated list of pairs of the form pkg:regex\n"+
" the regex is used to ignore names within pkg.")

Expand Down
32 changes: 16 additions & 16 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
ignore: map[string]string{},
tags: []string{},
blank: false,
asserts: false,
Expand All @@ -84,7 +84,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck", "-blank", "-asserts"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
ignore: map[string]string{},
tags: []string{},
blank: true,
asserts: true,
Expand All @@ -93,7 +93,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck", "foo", "bar"},
paths: []string{"foo", "bar"},
ignore: map[string]string{"fmt": dotStar.String()},
ignore: map[string]string{},
tags: []string{},
blank: false,
asserts: false,
Expand All @@ -102,7 +102,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck", "-ignore", "fmt:.*,encoding/binary:.*"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String(), "encoding/binary": dotStar.String()},
ignore: map[string]string{"fmt": ".*", "encoding/binary": dotStar.String()},
tags: []string{},
blank: false,
asserts: false,
Expand All @@ -120,7 +120,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck", "-ignore", "[rR]ead|[wW]rite"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String(), "": "[rR]ead|[wW]rite"},
ignore: map[string]string{"": "[rR]ead|[wW]rite"},
tags: []string{},
blank: false,
asserts: false,
Expand All @@ -129,7 +129,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck", "-ignorepkg", "testing"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String(), "testing": dotStar.String()},
ignore: map[string]string{"testing": dotStar.String()},
tags: []string{},
blank: false,
asserts: false,
Expand All @@ -138,7 +138,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck", "-ignorepkg", "testing,foo"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String(), "testing": dotStar.String(), "foo": dotStar.String()},
ignore: map[string]string{"testing": dotStar.String(), "foo": dotStar.String()},
tags: []string{},
blank: false,
asserts: false,
Expand All @@ -147,7 +147,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck", "-tags", "foo"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
ignore: map[string]string{},
tags: []string{"foo"},
blank: false,
asserts: false,
Expand All @@ -156,7 +156,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck", "-tags", "foo bar !baz"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
ignore: map[string]string{},
tags: []string{"foo", "bar", "!baz"},
blank: false,
asserts: false,
Expand All @@ -165,7 +165,7 @@ func TestParseFlags(t *testing.T) {
parseTestCase{
args: []string{"errcheck", "-tags", "foo bar !baz"},
paths: []string{"."},
ignore: map[string]string{"fmt": dotStar.String()},
ignore: map[string]string{},
tags: []string{"foo", "bar", "!baz"},
blank: false,
asserts: false,
Expand Down Expand Up @@ -203,22 +203,22 @@ func TestParseFlags(t *testing.T) {

argsStr := strings.Join(c.args, " ")
if !slicesEqual(p, c.paths) {
t.Fatalf("%q: path got %q want %q", argsStr, p, c.paths)
t.Errorf("%q: path got %q want %q", argsStr, p, c.paths)
}
if ign := checker.Ignore; !ignoresEqual(ign, c.ignore) {
t.Fatalf("%q: ignore got %q want %q", argsStr, ign, c.ignore)
t.Errorf("%q: ignore got %q want %q", argsStr, ign, c.ignore)
}
if tags := checker.Tags; !slicesEqual(tags, c.tags) {
t.Fatalf("%q: tags got %v want %v", argsStr, tags, c.tags)
t.Errorf("%q: tags got %v want %v", argsStr, tags, c.tags)
}
if b := checker.Blank; b != c.blank {
t.Fatalf("%q: blank got %v want %v", argsStr, b, c.blank)
t.Errorf("%q: blank got %v want %v", argsStr, b, c.blank)
}
if a := checker.Asserts; a != c.asserts {
t.Fatalf("%q: asserts got %v want %v", argsStr, a, c.asserts)
t.Errorf("%q: asserts got %v want %v", argsStr, a, c.asserts)
}
if e != c.error {
t.Fatalf("%q: error got %q want %q", argsStr, e, c.error)
t.Errorf("%q: error got %q want %q", argsStr, e, c.error)
}
}
}
6 changes: 3 additions & 3 deletions testdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
)

func a() error {
fmt.Println("this function returns an error") // UNCHECKED
fmt.Println("this function returns an error") // EXCLUDED
return nil
}

func b() (int, error) {
fmt.Println("this function returns an int and an error") // UNCHECKED
fmt.Println("this function returns an int and an error") // EXCLUDED
return 0, nil
}

func c() int {
fmt.Println("this function returns an int") // UNCHECKED
fmt.Println("this function returns an int") // EXCLUDED
return 7
}

Expand Down
4 changes: 2 additions & 2 deletions testdata/main2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import "fmt"
type t struct{}

func (x t) a() error {
fmt.Println("this method returns an error") // UNCHECKED
fmt.Println("this method returns an error") // EXCLUDED
//line myfile.txt:100
fmt.Println("this method also returns an error") // UNCHECKED
fmt.Println("this method also returns an error") // EXCLUDED
return nil
}

Expand Down

0 comments on commit e5700e7

Please sign in to comment.