Skip to content

Commit 9dc3d97

Browse files
committed
Fix some false positives, add configuration, fix linter errors
* Functions on one line should not be considered * Fix better token.Pos() for whitespaces * Fix variable names * Add stringer * Support configuration (not yet exposed properly) * Support to whitelist any RHS * Support to whitelist any calls one line above * Support to cuddle any assignment and call if any of the RHS/LHS is used
1 parent 32d6ac1 commit 9dc3d97

File tree

6 files changed

+363
-142
lines changed

6 files changed

+363
-142
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ good, making it harder for other people to read and understand. The linter will
1414
warn about newlines in and around blocks, in the beginning of files and other
1515
places in the code.
1616

17+
**I know this linter is aggressive** and a lot of projects I've tested it on
18+
have failed miserably. For this linter to be useful at all I want to be open to
19+
new ideas, configurations and discussions! Also note that some of the warnings
20+
might be bugs or unintentional false positives so I would love an
21+
[issue](https://github.com/bombsimon/wsl/issues/new) to fix, discuss, change or
22+
make something configurable!
23+
1724
## Usage
1825

1926
Install by using `go get -u github.com/bombsimon/wsl/cmd/...`.
@@ -26,6 +33,9 @@ By default, the linter will run on `./...` which means all go files in the
2633
current path and all subsequent paths, including test files. To disable linting
2734
test files, use `-n` or `--no-test`.
2835

36+
This linter can also be used as a part of
37+
[golangci-lint](https://github.com/golangci/golangci-lint)
38+
2939
## Rules
3040

3141
Note that this linter doesn't take in consideration the issues that will be
@@ -335,6 +345,11 @@ fmt.Println(a)
335345

336346
foo := true
337347
someFunc(false)
348+
349+
if someBool() {
350+
fmt.Println("doing things")
351+
}
352+
x.Calling()
338353
```
339354

340355
**Do**
@@ -350,6 +365,12 @@ someFunc(foo)
350365
bar := false
351366

352367
someFunc(true)
368+
369+
if someBool() {
370+
fmt.Println("doing things")
371+
}
372+
373+
x.Calling()
353374
```
354375

355376
#### Ranges

cmd/wsl/main.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,26 @@ import (
1212

1313
func main() {
1414
var (
15-
args []string
16-
help bool
17-
notest bool
18-
warnings bool
19-
cwd, _ = os.Getwd()
20-
files = []string{}
21-
finalFiles = []string{}
15+
args []string
16+
help bool
17+
noTest bool
18+
showWarnings bool
19+
cwd, _ = os.Getwd()
20+
files = []string{}
21+
finalFiles = []string{}
2222
)
2323

2424
flag.BoolVar(&help, "h", false, "Show this help text")
2525
flag.BoolVar(&help, "help", false, "")
26-
flag.BoolVar(&notest, "n", false, "Don't lint test files")
27-
flag.BoolVar(&notest, "no-test", false, "")
28-
flag.BoolVar(&warnings, "w", false, "Show warnings (ignored rules)")
29-
flag.BoolVar(&warnings, "warnings", false, "")
26+
flag.BoolVar(&noTest, "n", false, "Don't lint test files")
27+
flag.BoolVar(&noTest, "no-test", false, "")
28+
flag.BoolVar(&showWarnings, "w", false, "Show warnings (ignored rules)")
29+
flag.BoolVar(&showWarnings, "warnings", false, "")
3030

3131
flag.Parse()
3232

3333
if help {
3434
showHelp()
35-
3635
return
3736
}
3837

@@ -57,7 +56,7 @@ func main() {
5756

5857
// Use relative path to print shorter names, sort out test files if chosen.
5958
for _, f := range files {
60-
if notest {
59+
if noTest {
6160
if strings.HasSuffix(f, "_test.go") {
6261
continue
6362
}
@@ -72,26 +71,25 @@ func main() {
7271
finalFiles = append(finalFiles, f)
7372
}
7473

75-
r, w := wsl.ProcessFiles(finalFiles)
74+
processor := wsl.NewProcessor()
75+
result, warnings := processor.ProcessFiles(finalFiles)
7676

77-
for _, x := range r {
78-
fmt.Printf("%s:%d: %s\n", x.FileName, x.LineNumber, x.Reason)
77+
for _, r := range result {
78+
fmt.Println(r.String())
7979
}
8080

81-
if warnings && len(w) > 0 {
81+
if showWarnings && len(warnings) > 0 {
8282
fmt.Println()
8383
fmt.Println("⚠️ Warnings found")
8484

85-
for _, x := range w {
86-
fmt.Println(x)
85+
for _, w := range warnings {
86+
fmt.Println(w)
8787
}
8888
}
8989

90-
if len(r) > 0 {
90+
if len(result) > 0 {
9191
os.Exit(2)
9292
}
93-
94-
return
9593
}
9694

9795
func expandGoWildcard(root string) []string {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/bombsimon/wsl
22

33
require (
4-
github.com/davecgh/go-spew v1.1.1
4+
github.com/davecgh/go-spew v1.1.1 // indirect
55
github.com/kr/pretty v0.1.0 // indirect
66
github.com/stretchr/testify v1.4.0
77
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,5 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogR
1818
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1919
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
2020
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
21-
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
22-
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2321
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
2422
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)