Skip to content

Commit

Permalink
Add 1 to be not considered as magic number
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-muehle committed Feb 24, 2020
1 parent 60feff9 commit e6f9a99
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ return 3

## Excludes

By default the number 0 and test files are excluded!
By default the numbers 0 and 1 as well as test files are excluded!

### Further known excludes

Expand Down
2 changes: 1 addition & 1 deletion checks/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (a *ArgumentAnalyzer) Check(n ast.Node) {
switch expr.Args[i-1].(type) {
case *ast.ChanType:
// When it's not a simple buffered channel, report it
if x.Value != "1" {
if a.isMagicNumber(x) {
a.pass.Reportf(x.Pos(), reportMsg, x.Value, ArgumentCheck)
}
}
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func DefaultConfig() *Config {
Checks: map[string]bool{},
IgnoredNumbers: map[string]struct{}{
"0": {},
"1": {},
},
Excludes: []*regexp.Regexp{
regexp.MustCompile(`_test.go`),
Expand Down
17 changes: 17 additions & 0 deletions testdata/src/argument/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"math"
"net/http"
"os"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -54,3 +56,18 @@ func example8() {
c := make(chan int, 2) // want "Magic number: 2"
c <- 1
}

func example9() {
os.Exit(1)
}

func example10() {
var wg sync.WaitGroup
wg.Add(1)

go func() {
duration := 500 * time.Millisecond // want "Magic number: 500"
time.Sleep(duration)
wg.Done()
}()
}

0 comments on commit e6f9a99

Please sign in to comment.