diff --git a/go.sum b/go.sum index 3a1c54a..9f7fda2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/rules/style.go b/rules/style.go index fd86f25..436e307 100644 --- a/rules/style.go +++ b/rules/style.go @@ -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`) +} diff --git a/rules/testdata/style.go b/rules/testdata/style.go index 51b18d2..69c1703 100644 --- a/rules/testdata/style.go +++ b/rules/testdata/style.go @@ -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) +}