Skip to content

Commit 898bbb1

Browse files
committed
Merge branch 'nakabonne-nestif'
2 parents 48361b9 + d00692c commit 898bbb1

File tree

8 files changed

+137
-2
lines changed

8 files changed

+137
-2
lines changed

.golangci.example.yml

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ linters-settings:
9696
gocognit:
9797
# minimal code complexity to report, 30 by default (but we recommend 10-20)
9898
min-complexity: 10
99+
nestif:
100+
# minimal complexity of if statements to report, 5 by default
101+
min-complexity: 4
99102
goconst:
100103
# minimal length of string constant, 3 by default
101104
min-len: 3

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ lll: Reports long lines [fast: true, auto-fix: false]
229229
maligned: Tool to detect Go structs that would take less memory if their fields were sorted [fast: true, auto-fix: false]
230230
misspell: Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
231231
nakedret: Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
232+
nestif: Reports deeply nested if statements [fast: true, auto-fix: false]
232233
prealloc: Finds slice declarations that could potentially be preallocated [fast: true, auto-fix: false]
233234
rowserrcheck: checks whether Err of rows is checked successfully [fast: true, auto-fix: false]
234235
scopelint: Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false]
@@ -492,6 +493,7 @@ golangci-lint help linters
492493
- [gomodguard](https://github.com/ryancurrah/gomodguard) - Allow and block list linter for direct Go module dependencies.
493494
- [godot](https://github.com/tetafro/godot) - Check if comments end in a period
494495
- [testpackage](https://github.com/maratori/testpackage) - linter that makes you use a separate _test package
496+
- [nestif](https://github.com/nakabonne/nestif) - Reports deeply nested if statements
495497
496498
## Configuration
497499
@@ -710,6 +712,9 @@ linters-settings:
710712
gocognit:
711713
# minimal code complexity to report, 30 by default (but we recommend 10-20)
712714
min-complexity: 10
715+
nestif:
716+
# minimal complexity of if statements to report, 5 by default
717+
min-complexity: 4
713718
goconst:
714719
# minimal length of string constant, 3 by default
715720
min-len: 3
@@ -777,8 +782,8 @@ linters-settings:
777782
modules: # List of blocked modules
778783
# - github.com/uudashr/go-module: # Blocked module
779784
# recommendations: # Recommended modules that should be used instead (Optional)
780-
# - golang.org/x/mod
781-
# reason: "`mod` is the official go.mod parser library." # Reason why the recommended module should be used (Optional)
785+
# - golang.org/x/mod
786+
# reason: "`mod` is the official go.mod parser library." # Reason why the recommended module should be used (Optional)
782787
govet:
783788
# report about shadowed variables
784789
check-shadowing: true
@@ -1288,6 +1293,7 @@ Thanks to developers and authors of used linters:
12881293
- [ryancurrah](https://github.com/ryancurrah)
12891294
- [tetafro](https://github.com/tetafro)
12901295
- [maratori](https://github.com/maratori)
1296+
- [nakabonne](https://github.com/nakabonne)
12911297
12921298
## Changelog
12931299

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
github.com/mattn/go-colorable v0.1.4
3131
github.com/mitchellh/go-homedir v1.1.0
3232
github.com/mitchellh/go-ps v0.0.0-20190716172923-621e5597135b
33+
github.com/nakabonne/nestif v0.3.0
3334
github.com/pkg/errors v0.8.1
3435
github.com/ryancurrah/gomodguard v1.0.2
3536
github.com/securego/gosec v0.0.0-20200103095621-79fbf3af8d83

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz
186186
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
187187
github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
188188
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
189+
github.com/nakabonne/nestif v0.3.0 h1:+yOViDGhg8ygGrmII72nV9B/zGxY188TYpfolntsaPw=
190+
github.com/nakabonne/nestif v0.3.0/go.mod h1:dI314BppzXjJ4HsCnbo7XzrJHPszZsjnk5wEBSYHI2c=
189191
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d h1:AREM5mwr4u1ORQBMvzfzBgpsctsbQikCVpvC+tX285E=
190192
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
191193
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=

pkg/config/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ type LintersSettings struct {
212212
Gocognit GocognitSettings
213213
Godot GodotSettings
214214
Testpackage TestpackageSettings
215+
Nestif NestifSettings
215216

216217
Custom map[string]CustomLinterSettings
217218
}
@@ -297,6 +298,10 @@ type TestpackageSettings struct {
297298
SkipRegexp string `mapstructure:"skip-regexp"`
298299
}
299300

301+
type NestifSettings struct {
302+
MinComplexity int `mapstructure:"min-complexity"`
303+
}
304+
300305
//nolint:gomnd
301306
var defaultLintersSettings = LintersSettings{
302307
Lll: LllSettings{
@@ -339,6 +344,9 @@ var defaultLintersSettings = LintersSettings{
339344
Testpackage: TestpackageSettings{
340345
SkipRegexp: `(export|internal)_test\.go`,
341346
},
347+
Nestif: NestifSettings{
348+
MinComplexity: 5,
349+
},
342350
}
343351

344352
type CustomLinterSettings struct {

pkg/golinters/nestif.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package golinters
2+
3+
import (
4+
"sort"
5+
"sync"
6+
7+
"github.com/nakabonne/nestif"
8+
"golang.org/x/tools/go/analysis"
9+
10+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
11+
"github.com/golangci/golangci-lint/pkg/lint/linter"
12+
"github.com/golangci/golangci-lint/pkg/result"
13+
)
14+
15+
const nestifName = "nestif"
16+
17+
func NewNestif() *goanalysis.Linter {
18+
var mu sync.Mutex
19+
var resIssues []goanalysis.Issue
20+
21+
analyzer := &analysis.Analyzer{
22+
Name: goanalysis.TheOnlyAnalyzerName,
23+
Doc: goanalysis.TheOnlyanalyzerDoc,
24+
}
25+
return goanalysis.NewLinter(
26+
nestifName,
27+
"Reports deeply nested if statements",
28+
[]*analysis.Analyzer{analyzer},
29+
nil,
30+
).WithContextSetter(func(lintCtx *linter.Context) {
31+
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
32+
checker := &nestif.Checker{
33+
MinComplexity: lintCtx.Settings().Nestif.MinComplexity,
34+
}
35+
var issues []nestif.Issue
36+
for _, f := range pass.Files {
37+
issues = append(issues, checker.Check(f, pass.Fset)...)
38+
}
39+
if len(issues) == 0 {
40+
return nil, nil
41+
}
42+
43+
sort.SliceStable(issues, func(i, j int) bool {
44+
return issues[i].Complexity > issues[j].Complexity
45+
})
46+
47+
res := make([]goanalysis.Issue, 0, len(issues))
48+
for _, i := range issues {
49+
res = append(res, goanalysis.NewIssue(&result.Issue{ //nolint:scopelint
50+
Pos: i.Pos,
51+
Text: i.Message,
52+
FromLinter: nestifName,
53+
}, pass))
54+
}
55+
56+
mu.Lock()
57+
resIssues = append(resIssues, res...)
58+
mu.Unlock()
59+
60+
return nil, nil
61+
}
62+
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
63+
return resIssues
64+
}).WithLoadMode(goanalysis.LoadModeSyntax)
65+
}

pkg/lint/lintersdb/manager.go

+3
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
260260
WithPresets(linter.PresetStyle).
261261
WithLoadForGoAnalysis().
262262
WithURL("https://github.com/maratori/testpackage"),
263+
linter.NewConfig(golinters.NewNestif()).
264+
WithPresets(linter.PresetComplexity).
265+
WithURL("https://github.com/nakabonne/nestif"),
263266
}
264267

265268
isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""

test/testdata/nestif.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//args: -Enestif
2+
//config: linters-settings.nestif.min-complexity=1
3+
package testdata
4+
5+
func _() {
6+
var b1, b2, b3, b4 bool
7+
8+
if b1 { // ERROR "`if b1` is deeply nested \(complexity: 1\)"
9+
if b2 { // +1
10+
}
11+
}
12+
13+
if b1 { // ERROR "`if b1` is deeply nested \(complexity: 3\)"
14+
if b2 { // +1
15+
if b3 { // +2
16+
}
17+
}
18+
}
19+
20+
if b1 { // ERROR "`if b1` is deeply nested \(complexity: 5\)"
21+
if b2 { // +1
22+
} else if b3 { // +1
23+
if b4 { // +2
24+
}
25+
} else { // +1
26+
}
27+
}
28+
29+
if b1 { // ERROR "`if b1` is deeply nested \(complexity: 9\)"
30+
if b2 { // +1
31+
if b3 { // +2
32+
}
33+
}
34+
35+
if b2 { // +1
36+
if b3 { // +2
37+
if b4 { // +3
38+
}
39+
}
40+
}
41+
}
42+
43+
if b1 == b2 == b3 { // ERROR "`if b1 == b2 == b3` is deeply nested \(complexity: 1\)"
44+
if b4 { // +1
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)