Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add empty slice declaration check #441

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/exp/typeparams v0.0.0-20221002003631-540bb7301a08 h1:VpoGhesgULkabDHoDFGayS1wnkasmT95Jq2xZDwN45Q=
golang.org/x/exp/typeparams v0.0.0-20221002003631-540bb7301a08/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/exp/typeparams v0.0.0-20230307190834-24139beb5833 h1:jWGQJV4niP+CCmFW9ekjA9Zx8vYORzOUH2/Nl5WPuLQ=
golang.org/x/exp/typeparams v0.0.0-20230307190834-24139beb5833/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
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)
}