Skip to content

Commit

Permalink
Handle numbers with digit separators, fixes tommy-muehle#8
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-muehle committed Sep 28, 2020
1 parent e9379ae commit 9611820
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func TestCanExcludeNumbers(t *testing.T) {
testdata := analysistest.TestData()

options := flag.NewFlagSet("", flag.ExitOnError)
options.String("checks", "assign", "")
options.String("checks", "assign,argument", "")
options.String("excludes", "", "")
options.String("ignored-numbers", "100", "")
options.String("ignored-numbers", "1000,1234_567_890,3.14159264", "")

analyzer := Analyzer
analyzer.Flags = *options
Expand Down
8 changes: 6 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func WithIgnoredNumbers(numbers string) Option {
}

for _, number := range strings.Split(numbers, ",") {
config.IgnoredNumbers[number] = struct{}{}
config.IgnoredNumbers[config.removeDigitSeparator(number)] = struct{}{}
}
}
}
Expand All @@ -81,6 +81,10 @@ func (c *Config) IsCheckEnabled(name string) bool {
}

func (c *Config) IsIgnoredNumber(number string) bool {
_, ok := c.IgnoredNumbers[number]
_, ok := c.IgnoredNumbers[c.removeDigitSeparator(number)]
return ok
}

func (c *Config) removeDigitSeparator(number string) string {
return strings.Replace(number, "_", "", -1)
}
18 changes: 17 additions & 1 deletion testdata/src/ignored/ignored.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package ignored

import (
"log"
"net/http"
"time"
)

// ignored via configuration
var numberWithDigitSeparator int = 1_000

func example() interface{} {
s := struct {
Amount int
}{
Amount: 100,
Amount: numberWithDigitSeparator,
}

return s
Expand All @@ -20,3 +24,15 @@ func example2() *http.Client {
Timeout: 5 * time.Second, // want "Magic number: 5"
}
}

func example3() {
// ignored via configuration
if int32(1234_567_890)-int32(1_234_567_890) == 0 {
log.Println(3.1415_9265) // want "Magic number: 3.1415_9265"
}
}

func example4() {
// ignored via configuration
_ = float32(3.1415_9264)
}

0 comments on commit 9611820

Please sign in to comment.