@@ -40,22 +40,89 @@ func someFunc() {
40
40
if err != nil {
41
41
otherFunc2 (err)
42
42
}
43
- otherFunc3 (err) // Variable referenced in a call outside the if-statement.
43
+ otherFunc3 (err) // Variable referenced in a call outside if-statement.
44
44
}
45
45
```
46
46
47
47
``` go
48
- func someFunc () {
49
- sliceOfStrings := getSliceOfStrings ()
48
+ func someFunc () interface {} {
49
+ v := getValue ()
50
+ if v != nil {
51
+ otherFunc (v)
52
+ }
53
+ return v // Variable referenced in return statement.
54
+ }
55
+ ```
50
56
51
- if sliceOfStrings == 1 && sliceOfStrings[0 ] == " " {
52
- return
57
+ ``` go
58
+ func someFunc () interface {} {
59
+ a , b := getTwoValues ()
60
+ if a != nil {
61
+ return a
53
62
}
63
+ return b // Variables a and b are used in different statements.
64
+ }
65
+ ```
54
66
55
- for _ , s := range sliceOfStrings { // Variable referenced in a loop.
56
- // do stuff
67
+ ``` go
68
+ func someFunc () {
69
+ v := &someStruct{}
70
+
71
+ if v != nil { // cannot be `if v := &someStruct{}; v != nil
72
+ return
57
73
}
58
74
}
59
75
```
76
+ etc.
60
77
61
- etc.
78
+ ## Usage
79
+
80
+ ``` shell
81
+ usage: ifshort [--max-decl-chars {integer}] [--max-decl-lines {integer}] [INPUT]
82
+
83
+ positional arguments:
84
+ INPUT
85
+
86
+ options:
87
+ --max-decl-chars
88
+ maximum length of variable declaration measured in number of characters, after which the linter won' t suggest using short syntax. (default 30)
89
+ --max-decl-lines
90
+ maximum length of variable declaration measured in number of lines, after which the linter won' t suggest using short syntax. (default 1)
91
+ Has precedence over max-decl-chars.
92
+ ```
93
+
94
+ Example usage to check only the variables whose declaration takes no more than 50 characters:
95
+
96
+ ` ifshort --max-decl-chars 50 path/to/myproject ` .
97
+
98
+ With this configuration, ` ifshort ` won't suggest using short syntax on line 3:
99
+
100
+ ``` go
101
+ 1 func someFunc () {
102
+ 2 v := getValue (" Long long long declaration, linter shouldn't force short syntax for it." ) // More than 50 characters long.
103
+ 3 if v != nil {
104
+ 4 otherFunc1 (v)
105
+ 5 }
106
+ 6 }
107
+ ```
108
+
109
+ Example usage to check only the variables whose declaration takes no more than 2 lines:
110
+
111
+ ` ifshort --max-decl-lines 2 path/to/myproject ` .
112
+
113
+ ``` go
114
+ func someFunc () {
115
+ v1 := getValue (" firstLine" ,
116
+ " secondLine" ) // This declaration will be checked, and short syntax suggested.
117
+ if v1 != nil {
118
+ someFunc (v1)
119
+ }
120
+
121
+ v2 := getValue (" firstLine" ,
122
+ " secondLine" ,
123
+ " thirdLine" ) // This declaration won't be checked.
124
+ if v2 != nil {
125
+ someFunc (v2)
126
+ }
127
+ }
128
+ ```
0 commit comments