-
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/gosimple: move documentation to staticcheck.io
- Loading branch information
Showing
29 changed files
with
403 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Code simplifications |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Use plain channel send or receive | ||
|
||
`select` with a single case can be replaced with a simple send or | ||
receive. | ||
|
||
**Before:** | ||
|
||
``` | ||
select { | ||
case x := <-ch: | ||
fmt.Println(x) | ||
} | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
x := <-ch | ||
fmt.Println(x) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Replace with `copy()` | ||
|
||
Use `copy()` for copying elements from one slice to another. | ||
|
||
**Before:** | ||
|
||
``` | ||
for i, x := range src { | ||
dst[i] = x | ||
} | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
copy(dst, src) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Omit comparison with boolean constant | ||
|
||
**Before:** | ||
|
||
``` | ||
if x == true {} | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
if x {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Replace with `strings.Contains` | ||
|
||
**Before:** | ||
|
||
``` | ||
if strings.Index(x, y) != -1 {} | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
if strings.Contains(x, y) {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Replace with `bytes.Equal` | ||
|
||
**Before:** | ||
|
||
``` | ||
if bytes.Compare(x, y) == 0 {} | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
if bytes.Equal(x, y) {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Drop unnecessary use of the blank identifier | ||
|
||
In many cases, assigning to the blank identifier is unnecessary. | ||
|
||
**Before:** | ||
|
||
``` | ||
for _ = range s {} | ||
x, _ = someMap[key] | ||
_ = <-ch | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
for range s{} | ||
x = someMap[key] | ||
<-ch | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Replace with `for { ... }` | ||
|
||
For infinite loops, using `for { ... }` is the most idiomatic choice. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Simplify regular expression by using raw string literal | ||
|
||
Raw string literals use `` ` `` instead of `"` and do not support any escape | ||
sequences. This means that the backslash (`\`) can be used freely, | ||
without the need of escaping. | ||
|
||
Since regular expressions have their own escape sequences, raw strings | ||
can improve their readability. | ||
|
||
**Before:** | ||
|
||
``` | ||
regexp.Compile("\\A(\\w+) profile: total \\d+\\n\\z") | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
regexp.Compile(`\A(\w+) profile: total \d+\n\z`) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Simplify returning boolean expression | ||
|
||
**Before:** | ||
|
||
``` | ||
if <expr> { | ||
return true | ||
} | ||
return false | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
return <expr> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Omit redundant nil check on slices | ||
|
||
The `len` function is defined for all slices, even nil ones, which | ||
have a length of zero. It is not necessary to check if a slice is not | ||
nil before checking that its length is not zero. | ||
|
||
**Before:** | ||
|
||
``` | ||
if x != nil && len(x) != 0 {} | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
if len(x) != 0 {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Omit default slice index | ||
|
||
When slicing, the second index defaults to the length of the value, | ||
making `s[n:len(s)]` and `s[n:]` equivalent. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Use a single append to concatenate two slices | ||
|
||
**Before:** | ||
|
||
``` | ||
for _, e := range y { | ||
x = append(x, e) | ||
} | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
x = append(x, y...) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Replace with `time.Since(x)` | ||
|
||
The `time.Since` helper has the same effect as using | ||
`time.Now().Sub(x)` but is easier to read. | ||
|
||
**Before:** | ||
|
||
``` | ||
time.Now().Sub(x) | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
time.Since(x) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Simplify returning final error | ||
|
||
**Before:** | ||
|
||
``` | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
return err | ||
``` | ||
|
||
**Note:** | ||
|
||
This simplification is only valid if `err` is an interface value and | ||
not of a concrete type. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Use a type conversion | ||
|
||
Two struct types with identical fields can be converted between each | ||
other. In older versions of Go, the fields had to have identical | ||
struct tags. Since Go 1.8, however, struct tags are ignored during | ||
conversions. It is thus not necessary to manually copy every field | ||
individually. | ||
|
||
**Before:** | ||
|
||
``` | ||
var x T1 | ||
y := T2{ | ||
Field1: x.Field1, | ||
Field2: x.Field2, | ||
} | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
var x T1 | ||
y := T2(x) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Replace with `strings.TrimPrefix` | ||
|
||
Instead of using `strings.HasPrefix` and manual slicing, use the | ||
`strings.TrimPrefix` function. If the string doesn't start with the | ||
prefix, the original string will be returned. Using | ||
`strings.TrimPrefix` reduces complexity, and avoids common bugs, such | ||
as off-by-one mistakes. | ||
|
||
**Before:** | ||
|
||
``` | ||
if strings.HasPrefix(str, prefix) { | ||
str = str[len(prefix):] | ||
} | ||
``` | ||
|
||
**After:** | ||
|
||
``` | ||
str = strings.TrimPrefix(str, prefix) | ||
``` |
Oops, something went wrong.