Skip to content

Commit f2d3817

Browse files
committed
Initial commit
0 parents  commit f2d3817

File tree

6 files changed

+514
-0
lines changed

6 files changed

+514
-0
lines changed

checkReturnValue.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package minioExtensions
2+
3+
import "errors"
4+
5+
func thenBranchGoodError() error {
6+
7+
if errorSource() != ErrNone {
8+
// Good: returning an error
9+
return errors.New("failed")
10+
}
11+
doSomething()
12+
return nil
13+
14+
}
15+
16+
func thenBranchGoodWithElseError() error {
17+
18+
if errorSource() != ErrNone {
19+
// Good: an error means we return an error
20+
return errors.New("failed")
21+
} else {
22+
doSomething()
23+
}
24+
doSomething()
25+
return nil
26+
27+
}
28+
29+
func thenBranchBadError() error {
30+
31+
if errorSource() != ErrNone {
32+
// Bad: despite an error, we return nil
33+
return nil
34+
}
35+
doSomething()
36+
return nil
37+
38+
}
39+
40+
func thenBranchBadWithElseError() error {
41+
42+
if errorSource() != ErrNone {
43+
// Bad: despite an error, we return nil
44+
return nil
45+
} else {
46+
doSomething()
47+
}
48+
doSomething()
49+
return nil
50+
51+
}
52+
53+
func elseBranchGoodError() error {
54+
55+
if errorSource() == ErrNone {
56+
doSomething()
57+
} else {
58+
// Good: returning an error
59+
return errors.New("failed")
60+
}
61+
doSomething()
62+
return nil
63+
}
64+
65+
func elseBranchBadError() error {
66+
67+
if errorSource() == ErrNone {
68+
doSomething()
69+
} else {
70+
// Bad: despite an error, we return nil
71+
return nil
72+
}
73+
doSomething()
74+
return nil
75+
76+
}
77+
78+
func multiReturnBad() (string, error) {
79+
80+
if errorSource() != ErrNone {
81+
// Bad: despite an error, we return a nil error
82+
return "", nil
83+
}
84+
doSomething()
85+
return "Result", nil
86+
87+
}
88+
89+
func getNil() error {
90+
return nil
91+
}
92+
93+
func getError(s string) error {
94+
return errors.New(s)
95+
}
96+
97+
func thenBranchGoodInterproceduralError() error {
98+
99+
if errorSource() != ErrNone {
100+
// Good: returning an error
101+
return getError("failed")
102+
}
103+
doSomething()
104+
return getNil()
105+
106+
}
107+
108+
func thenBranchBadInterproceduralError() error {
109+
110+
if errorSource() != ErrNone {
111+
// Good: returning nil despite an error
112+
return getNil()
113+
}
114+
doSomething()
115+
return getNil()
116+
117+
}
118+

conditionalPolarities.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package minioExtensions
2+
3+
func thenBranchGood() {
4+
5+
if errorSource() != ErrNone {
6+
// Good: an error means we return early
7+
return
8+
}
9+
doSomething()
10+
11+
}
12+
13+
func thenBranchGoodWithElse() {
14+
15+
if errorSource() != ErrNone {
16+
// Good: an error means we return early
17+
return
18+
} else {
19+
doSomething()
20+
}
21+
doSomething()
22+
23+
}
24+
25+
func thenBranchBad() {
26+
27+
if errorSource() != ErrNone {
28+
// Bad: despite an error, we carry on to execute doSomething()
29+
insteadOfReturn()
30+
}
31+
doSomething()
32+
33+
}
34+
35+
func thenBranchBadWithElse() {
36+
37+
if errorSource() != ErrNone {
38+
// Bad: despite an error, we carry on to execute doSomething()
39+
insteadOfReturn()
40+
} else {
41+
doSomething()
42+
}
43+
doSomething()
44+
45+
}
46+
47+
func elseBranchGood() {
48+
49+
if errorSource() == ErrNone {
50+
doSomething()
51+
} else {
52+
// Good: an error means we return early
53+
return
54+
}
55+
doSomething()
56+
57+
}
58+
59+
func elseBranchBad() {
60+
61+
if errorSource() == ErrNone {
62+
doSomething()
63+
} else {
64+
// Bad: despite an error, we carry on to execute doSomething()
65+
insteadOfReturn()
66+
}
67+
doSomething()
68+
69+
}

logicalOperators.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package minioExtensions
2+
3+
func negatedThenBranchGood() {
4+
5+
if !(errorSource() == ErrNone) {
6+
// Good: an error means we return early
7+
return
8+
}
9+
doSomething()
10+
11+
}
12+
13+
func negatedThenBranchGoodWithElse() {
14+
15+
if !(errorSource() == ErrNone) {
16+
// Good: an error means we return early
17+
return
18+
} else {
19+
doSomething()
20+
}
21+
doSomething()
22+
23+
}
24+
25+
func negatedElseBranchGood() {
26+
27+
if !(errorSource() != ErrNone) {
28+
doSomething()
29+
} else {
30+
// Good: an error means we return early
31+
return
32+
}
33+
doSomething()
34+
35+
}
36+
37+
func negatedElseBranchBad() {
38+
39+
if !(errorSource() != ErrNone) {
40+
doSomething()
41+
} else {
42+
// Bad: despite an error, we carry on to execute doSomething()
43+
insteadOfReturn()
44+
}
45+
doSomething()
46+
47+
}
48+
49+
func someOtherCondition() bool {
50+
return true
51+
}
52+
53+
func logicalAndThenBranchGood() {
54+
55+
if errorSource() != ErrNone && someOtherCondition() {
56+
// Good: in the then-block, where we're certain there has been an error,
57+
// we return early. In the other branch an error is possible but not certain.
58+
return
59+
}
60+
doSomething()
61+
62+
}
63+
64+
func logicalAndThenBranchBad() {
65+
66+
if errorSource() != ErrNone && someOtherCondition() {
67+
// Bad: in the then-block, where we're certain there has been an error,
68+
// we do not return early.
69+
insteadOfReturn()
70+
}
71+
doSomething()
72+
73+
}
74+
75+
func logicalAndElseBranchUncertain() {
76+
77+
if errorSource() == ErrNone && someOtherCondition() {
78+
doSomething()
79+
} else {
80+
// Uncertain: an error is not a precondition for entering either branch.
81+
// We should be conservative and NOT flag this function as a problem.
82+
insteadOfReturn()
83+
}
84+
doSomething()
85+
86+
}
87+
88+
func logicalOrElseBranchGood() {
89+
90+
if errorSource() == ErrNone || someOtherCondition() {
91+
doSomething()
92+
} else {
93+
// Good: in the else-block, where we're certain there has been an error,
94+
// we return early. In the other branch an error is possible but not certain.
95+
return
96+
}
97+
doSomething()
98+
99+
}
100+
101+
func logicalOrElseBranchBad() {
102+
103+
if errorSource() == ErrNone || someOtherCondition() {
104+
doSomething()
105+
} else {
106+
// Bad: in the else-block, where we're certain there has been an error,
107+
// we do not return early.
108+
insteadOfReturn()
109+
}
110+
doSomething()
111+
112+
}
113+
114+
func logicalOrThenBranchUncertain() {
115+
116+
if errorSource() != ErrNone || someOtherCondition() {
117+
// Uncertain: an error is not a precondition for entering either branch.
118+
// We should be conservative and NOT flag this function as a problem.
119+
insteadOfReturn()
120+
} else {
121+
doSomething()
122+
}
123+
doSomething()
124+
125+
}

moreWaysToReturn.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package minioExtensions
2+
3+
const particularErrOne = 1
4+
const particularErrTwo = 2
5+
6+
func subBranchGood() int {
7+
8+
// Good: while the if-block's logic is complex, it always returns.
9+
err := errorSource()
10+
if err != ErrNone {
11+
if err == particularErrOne {
12+
return -1
13+
} else if err == particularErrTwo {
14+
return -2
15+
} else {
16+
return -3
17+
}
18+
}
19+
doSomething()
20+
return 0
21+
22+
}
23+
24+
func subBranchBad() int {
25+
26+
// Bad: one of the if-block's branches falls through to execute `doSomething()`.
27+
err := errorSource()
28+
if err != ErrNone {
29+
if err == particularErrOne {
30+
return -1
31+
} else if err == particularErrTwo {
32+
err = ErrNone
33+
} else {
34+
return -3
35+
}
36+
}
37+
doSomething()
38+
return 0
39+
40+
}
41+
42+
func switchGood() int {
43+
44+
// Good: while the if-block's logic is complex, it always returns.
45+
err := errorSource()
46+
if err != ErrNone {
47+
switch err {
48+
case particularErrOne:
49+
return -1
50+
case particularErrTwo:
51+
return -2
52+
default:
53+
return -3
54+
}
55+
}
56+
doSomething()
57+
return 0
58+
59+
}
60+
61+
func switchBad() int {
62+
63+
// Bad: one of the if-block's branches falls through to execute `doSomething()`.
64+
err := errorSource()
65+
if err != ErrNone {
66+
switch err {
67+
case particularErrOne:
68+
err = ErrNone
69+
case particularErrTwo:
70+
return -2
71+
default:
72+
return -3
73+
}
74+
}
75+
doSomething()
76+
return 0
77+
78+
}

0 commit comments

Comments
 (0)