You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -487,7 +487,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
487
487
|[`cognitive-complexity`](./RULES_DESCRIPTIONS.md#cognitive-complexity)| int | Sets restriction for maximum Cognitive complexity. | no | no |
488
488
|[`string-of-int`](./RULES_DESCRIPTIONS.md#string-of-int)| n/a | Warns on suspicious casts from int to string | no | yes |
489
489
|[`string-format`](./RULES_DESCRIPTIONS.md#string-format)| map | Warns on specific string literals that fail one or more user-configured regular expressions | no | no |
490
-
|[`early-return`](./RULES_DESCRIPTIONS.md#early-return)| n/a | Spots if-then-else statements that can be refactored to simplify code reading | no | no |
490
+
|[`early-return`](./RULES_DESCRIPTIONS.md#early-return)| n/a | Spots if-then-else statements where the predicate may be inverted to reduce nesting| no | no |
491
491
|[`unconditional-recursion`](./RULES_DESCRIPTIONS.md#unconditional-recursion)| n/a | Warns on function calls that will lead to (direct) infinite recursion | no | no |
492
492
|[`identical-branches`](./RULES_DESCRIPTIONS.md#identical-branches)| n/a | Spots if-then-else statements with identical `then` and `else` branches | no | no |
493
493
|[`defer`](./RULES_DESCRIPTIONS.md#defer)| map | Warns on some [defer gotchas](https://blog.learngoprogramming.com/5-gotchas-of-defer-in-go-golang-part-iii-36a1ab3d6ef1)| no | no |
Copy file name to clipboardExpand all lines: RULES_DESCRIPTIONS.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -288,7 +288,7 @@ _Configuration_: N/A
288
288
289
289
## early-return
290
290
291
-
_Description_: In GO it is idiomatic to minimize nesting statements, a typical example is to avoid if-then-else constructions. This rule spots constructions like
291
+
_Description_: In Go it is idiomatic to minimize nesting statements, a typical example is to avoid if-then-else constructions. This rule spots constructions like
292
292
```go
293
293
if cond {
294
294
// do something
@@ -297,7 +297,7 @@ if cond {
297
297
return ...
298
298
}
299
299
```
300
-
that can be rewritten into more idiomatic:
300
+
where the `if` condition may be inverted in order to reduce nesting:
Copy file name to clipboardExpand all lines: testdata/early-return.go
+71-6
Original file line number
Diff line number
Diff line change
@@ -3,35 +3,36 @@
3
3
package fixtures
4
4
5
5
funcearlyRet() bool {
6
-
ifcond { // MATCH /if c {...} else {... return } can be simplified to if !c { ... return } .../
6
+
ifcond { // MATCH /if c {...} else {... return } can be simplified to if !c { ... return } .../
7
7
println()
8
8
println()
9
9
println()
10
10
} else {
11
11
returnfalse
12
12
}
13
13
14
-
ifcond { //MATCH /if c {...} else {... return } can be simplified to if !c { ... return } .../
14
+
ifcond { //MATCH /if c {...} else {... return } can be simplified to if !c { ... return } .../
15
15
println()
16
16
} else {
17
17
returnfalse
18
18
}
19
19
20
-
ifcond { //MATCH /if c { } else {... return} can be simplified to if !c { ... return }/
20
+
ifcond { //MATCH /if c { } else {... return} can be simplified to if !c { ... return }/
21
21
} else {
22
22
returnfalse
23
23
}
24
24
25
25
ifcond {
26
26
println()
27
-
} elseifcond { //MATCH /if c { } else {... return} can be simplified to if !c { ... return }/
27
+
} elseifcond { //MATCH /if c { } else {... return} can be simplified to if !c { ... return }/
28
28
} else {
29
29
returnfalse
30
30
}
31
31
32
+
// the first branch does not return, so we can't reduce nesting here
32
33
ifcond {
33
34
println()
34
-
} elseifcond {//MATCH /if c {...} else {... return } can be simplified to if !c { ... return } .../
35
+
} elseifcond {
35
36
println()
36
37
} else {
37
38
returnfalse
@@ -44,7 +45,7 @@ func earlyRet() bool {
44
45
returnfalse
45
46
}
46
47
47
-
ifcond { //MATCH /if c {...} else {... return } can be simplified to if !c { ... return } .../
48
+
ifcond { //MATCH /if c {...} else {... return } can be simplified to if !c { ... return } .../
48
49
println()
49
50
println()
50
51
println()
@@ -59,4 +60,68 @@ func earlyRet() bool {
59
60
} else {
60
61
println()
61
62
}
63
+
64
+
ifcond {
65
+
ifcond { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } .../
66
+
println()
67
+
} else {
68
+
returnfalse
69
+
}
70
+
}
71
+
72
+
ifcond {
73
+
println()
74
+
} else {
75
+
ifcond { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } .../
76
+
println()
77
+
} else {
78
+
returnfalse
79
+
}
80
+
}
81
+
82
+
ifcond {
83
+
println()
84
+
} elseifcond {
85
+
println()
86
+
} else {
87
+
ifcond { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } .../
88
+
println()
89
+
} else {
90
+
returnfalse
91
+
}
92
+
}
93
+
94
+
for {
95
+
ifcond { //MATCH /if c { ... } else { ... continue } can be simplified to if !c { ... continue } .../
96
+
println()
97
+
} else {
98
+
continue
99
+
}
100
+
}
101
+
102
+
for {
103
+
ifcond { //MATCH /if c { ... } else { ... break } can be simplified to if !c { ... break } .../
104
+
println()
105
+
} else {
106
+
break
107
+
}
108
+
}
109
+
110
+
ifcond { //MATCH /if c { ... } else { ... panic() } can be simplified to if !c { ... panic() } .../
111
+
println()
112
+
} else {
113
+
panic("!")
114
+
}
115
+
116
+
ifcond { //MATCH /if c { ... } else { ... goto } can be simplified to if !c { ... goto } .../
117
+
println()
118
+
} else {
119
+
goto X
120
+
}
121
+
122
+
ifx, ok:=foo(); ok { //MATCH /if c { ... } else { ... return } can be simplified to if !c { ... return } ... (move short variable declaration to its own line if necessary)/
0 commit comments