2023-09-23 09:05:52 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
)
|
|
|
|
|
|
|
|
type enforceSliceStyleType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
enforceSliceStyleTypeAny enforceSliceStyleType = "any"
|
|
|
|
enforceSliceStyleTypeMake enforceSliceStyleType = "make"
|
|
|
|
enforceSliceStyleTypeLiteral enforceSliceStyleType = "literal"
|
2024-02-26 12:58:31 +02:00
|
|
|
enforceSliceStyleTypeNil enforceSliceStyleType = "nil"
|
2023-09-23 09:05:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func sliceStyleFromString(s string) (enforceSliceStyleType, error) {
|
|
|
|
switch s {
|
|
|
|
case string(enforceSliceStyleTypeAny), "":
|
|
|
|
return enforceSliceStyleTypeAny, nil
|
|
|
|
case string(enforceSliceStyleTypeMake):
|
|
|
|
return enforceSliceStyleTypeMake, nil
|
|
|
|
case string(enforceSliceStyleTypeLiteral):
|
|
|
|
return enforceSliceStyleTypeLiteral, nil
|
2024-02-26 12:58:31 +02:00
|
|
|
case string(enforceSliceStyleTypeNil):
|
|
|
|
return enforceSliceStyleTypeNil, nil
|
2023-09-23 09:05:52 +02:00
|
|
|
default:
|
|
|
|
return enforceSliceStyleTypeAny, fmt.Errorf(
|
|
|
|
"invalid slice style: %s (expecting one of %v)",
|
|
|
|
s,
|
|
|
|
[]enforceSliceStyleType{
|
|
|
|
enforceSliceStyleTypeAny,
|
|
|
|
enforceSliceStyleTypeMake,
|
|
|
|
enforceSliceStyleTypeLiteral,
|
2024-02-26 12:58:31 +02:00
|
|
|
enforceSliceStyleTypeNil,
|
2023-09-23 09:05:52 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnforceSliceStyleRule implements a rule to enforce `make([]type)` over `[]type{}`.
|
|
|
|
type EnforceSliceStyleRule struct {
|
|
|
|
enforceSliceStyle enforceSliceStyleType
|
2024-11-15 13:03:59 +02:00
|
|
|
|
|
|
|
configureOnce sync.Once
|
2023-09-23 09:05:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *EnforceSliceStyleRule) configure(arguments lint.Arguments) {
|
|
|
|
if len(arguments) < 1 {
|
|
|
|
r.enforceSliceStyle = enforceSliceStyleTypeAny
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
enforceSliceStyle, ok := arguments[0].(string)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("Invalid argument '%v' for 'enforce-slice-style' rule. Expecting string, got %T", arguments[0], arguments[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
r.enforceSliceStyle, err = sliceStyleFromString(enforceSliceStyle)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Invalid argument to the enforce-slice-style rule: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
|
|
|
func (r *EnforceSliceStyleRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
2024-11-15 13:03:59 +02:00
|
|
|
r.configureOnce.Do(func() { r.configure(arguments) })
|
2023-09-23 09:05:52 +02:00
|
|
|
|
|
|
|
if r.enforceSliceStyle == enforceSliceStyleTypeAny {
|
|
|
|
// this linter is not configured
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var failures []lint.Failure
|
|
|
|
|
|
|
|
astFile := file.AST
|
|
|
|
ast.Inspect(astFile, func(n ast.Node) bool {
|
|
|
|
switch v := n.(type) {
|
|
|
|
case *ast.CompositeLit:
|
2024-02-26 12:58:31 +02:00
|
|
|
switch r.enforceSliceStyle {
|
|
|
|
case enforceSliceStyleTypeMake, enforceSliceStyleTypeNil:
|
|
|
|
// continue
|
|
|
|
default:
|
2023-09-23 09:05:52 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.isSliceType(v.Type) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-10-01 12:14:02 +02:00
|
|
|
isNotEmptySlice := len(v.Elts) > 0
|
|
|
|
if isNotEmptySlice {
|
2023-09-23 09:05:52 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-02-26 12:58:31 +02:00
|
|
|
var failureMessage string
|
|
|
|
if r.enforceSliceStyle == enforceSliceStyleTypeNil {
|
|
|
|
failureMessage = "use nil slice declaration (e.g. var args []type) instead of []type{}"
|
|
|
|
} else {
|
|
|
|
failureMessage = "use make([]type) instead of []type{} (or declare nil slice)"
|
|
|
|
}
|
2023-09-23 09:05:52 +02:00
|
|
|
failures = append(failures, lint.Failure{
|
|
|
|
Confidence: 1,
|
|
|
|
Node: v,
|
|
|
|
Category: "style",
|
2024-02-26 12:58:31 +02:00
|
|
|
Failure: failureMessage,
|
2023-09-23 09:05:52 +02:00
|
|
|
})
|
|
|
|
case *ast.CallExpr:
|
2024-02-26 12:58:31 +02:00
|
|
|
switch r.enforceSliceStyle {
|
|
|
|
case enforceSliceStyleTypeLiteral, enforceSliceStyleTypeNil:
|
|
|
|
default:
|
2023-09-23 09:05:52 +02:00
|
|
|
// skip any function calls, even if it's make([]type)
|
|
|
|
// we don't want to report it if literals are not enforced
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
ident, ok := v.Fun.(*ast.Ident)
|
|
|
|
if !ok || ident.Name != "make" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-10-01 12:14:02 +02:00
|
|
|
isInvalidMakeDeclaration := len(v.Args) < 2
|
|
|
|
if isInvalidMakeDeclaration {
|
2023-09-23 09:05:52 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.isSliceType(v.Args[0]) {
|
|
|
|
// not a slice type
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
arg, ok := v.Args[1].(*ast.BasicLit)
|
|
|
|
if !ok {
|
|
|
|
// skip invalid make declarations
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-10-01 12:14:02 +02:00
|
|
|
isSliceSizeNotZero := arg.Value != "0"
|
|
|
|
if isSliceSizeNotZero {
|
2023-09-23 09:05:52 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(v.Args) > 2 {
|
|
|
|
arg, ok := v.Args[2].(*ast.BasicLit)
|
|
|
|
if !ok {
|
|
|
|
// skip invalid make declarations
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-10-01 12:14:02 +02:00
|
|
|
isNonZeroCapacitySlice := arg.Value != "0"
|
|
|
|
if isNonZeroCapacitySlice {
|
2023-09-23 09:05:52 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 12:58:31 +02:00
|
|
|
var failureMessage string
|
|
|
|
if r.enforceSliceStyle == enforceSliceStyleTypeNil {
|
|
|
|
failureMessage = "use nil slice declaration (e.g. var args []type) instead of make([]type, 0)"
|
|
|
|
} else {
|
|
|
|
failureMessage = "use []type{} instead of make([]type, 0) (or declare nil slice)"
|
|
|
|
}
|
2023-09-23 09:05:52 +02:00
|
|
|
failures = append(failures, lint.Failure{
|
|
|
|
Confidence: 1,
|
|
|
|
Node: v.Args[0],
|
|
|
|
Category: "style",
|
2024-02-26 12:58:31 +02:00
|
|
|
Failure: failureMessage,
|
2023-09-23 09:05:52 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2023-09-24 08:55:14 +02:00
|
|
|
func (*EnforceSliceStyleRule) Name() string {
|
2023-09-23 09:05:52 +02:00
|
|
|
return "enforce-slice-style"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *EnforceSliceStyleRule) isSliceType(v ast.Expr) bool {
|
|
|
|
switch t := v.(type) {
|
|
|
|
case *ast.ArrayType:
|
|
|
|
if t.Len != nil {
|
|
|
|
// array
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// slice
|
|
|
|
return true
|
|
|
|
case *ast.Ident:
|
|
|
|
if t.Obj == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
typeSpec, ok := t.Obj.Decl.(*ast.TypeSpec)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return r.isSliceType(typeSpec.Type)
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|