Skip to content

Commit

Permalink
feat: add empty slice declaration check
Browse files Browse the repository at this point in the history
  • Loading branch information
demoManito committed Apr 12, 2023
1 parent 274635f commit fe144c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rules/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ func emptyError(m dsl.Matcher) {
m.Match(`fmt.Errorf("")`, `errors.New("")`).
Report(`empty errors are hard to debug`)
}

//doc:summary reports empty slice declaration
//doc:before x := []int{}
//doc:after var x []int
//doc:tags style
func emptySlice(m dsl.Matcher) {
m.Match(`var $name = make([]$type, 0)`, `$name := []$type{}`, `$name := make([]$type, 0, 0)`, `$name := make([]$type, 0)`).
Report(`zero-length slice declaring nil slice is better`).
Suggest(`var $name []$type`)
}
6 changes: 6 additions & 0 deletions rules/testdata/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ func emptyError() {
_ = errors.New("") // want `\Qempty errors are hard to debug`
_ = errors.New(``) // want `\Qempty errors are hard to debug`
}

func emptySlice() {
x := []int{} // want `\QemptySlice: zero-length slice declaring nil slice is better
a := make([]int, 0, 0) // want `\QemptySlice: zero-length slice declaring nil slice is better
fmt.Println(x, a)
}

0 comments on commit fe144c7

Please sign in to comment.