Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit f5a7f3d

Browse files
committed
Improve README
1 parent 1dcf01a commit f5a7f3d

File tree

1 file changed

+75
-8
lines changed

1 file changed

+75
-8
lines changed

README.md

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,89 @@ func someFunc() {
4040
if err != nil {
4141
otherFunc2(err)
4242
}
43-
otherFunc3(err) // Variable referenced in a call outside the if-statement.
43+
otherFunc3(err) // Variable referenced in a call outside if-statement.
4444
}
4545
```
4646

4747
```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+
```
5056

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
5362
}
63+
return b // Variables a and b are used in different statements.
64+
}
65+
```
5466

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
5773
}
5874
}
5975
```
76+
etc.
6077

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

Comments
 (0)