.golangci.yml: make goconst checker more strict

This commit is contained in:
Dmitry Vyukov 2020-06-06 11:34:20 +02:00
parent cdf1aa4dc3
commit 3f04838a10
6 changed files with 33 additions and 21 deletions

View File

@ -76,8 +76,8 @@ linters-settings:
dupl:
threshold: 60
goconst:
min-len: 7
min-occurrences: 4
min-len: 3
min-occurrences: 3
nestif:
# TODO: consider reducing this value.
min-complexity: 12
@ -109,3 +109,6 @@ issues:
- path: (dashboard/app/.*_test\.go)
linters:
- dupl
- path: (prog/.*_test\.go)
linters:
- goconst

View File

@ -216,7 +216,7 @@ type Type struct {
}
func (n *Type) Info() (Pos, string, string) {
return n.Pos, "type", n.Ident
return n.Pos, "type-opt", n.Ident
}
type Field struct {

View File

@ -184,6 +184,8 @@ func (comp *compiler) checkFieldGroup(fields []*ast.Field, what, ctx string) {
}
}
const argBase = "BASE"
func (comp *compiler) checkTypedefs() {
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
@ -200,7 +202,7 @@ func (comp *compiler) checkTypedefs() {
// For templates we only do basic checks of arguments.
names := make(map[string]bool)
for i, arg := range n.Args {
if arg.Name == "BASE" && i != len(n.Args)-1 {
if arg.Name == argBase && i != len(n.Args)-1 {
comp.error(arg.Pos, "type argument BASE must be the last argument")
}
if names[arg.Name] {
@ -857,7 +859,7 @@ func (comp *compiler) replaceTypedef(ctx *checkCtx, t *ast.Type, flags checkFlag
}
typedef := comp.typedefs[typedefName]
// Handling optional BASE argument.
if len(typedef.Args) > 0 && typedef.Args[len(typedef.Args)-1].Name == "BASE" {
if len(typedef.Args) > 0 && typedef.Args[len(typedef.Args)-1].Name == argBase {
if flags&checkIsArg != 0 && len(t.Args) == len(typedef.Args)-1 {
t.Args = append(t.Args, &ast.Type{
Pos: t.Pos,
@ -889,7 +891,7 @@ func (comp *compiler) replaceTypedef(ctx *checkCtx, t *ast.Type, flags checkFlag
if nargs == 0 {
comp.error(t.Pos, "type %v is not a template", typedefName)
} else {
if flags&checkIsArg != 0 && typedef.Args[len(typedef.Args)-1].Name == "BASE" {
if flags&checkIsArg != 0 && typedef.Args[len(typedef.Args)-1].Name == argBase {
nargs--
}
comp.error(t.Pos, "template %v needs %v arguments instead of %v",

View File

@ -260,21 +260,25 @@ func defaultFeatures(value bool) Features {
}
func ParseFeaturesFlags(enable string, disable string, defaultValue bool) (Features, error) {
if enable == "none" && disable == "none" {
const (
none = "none"
all = "all"
)
if enable == none && disable == none {
return defaultFeatures(defaultValue), nil
}
if enable != "none" && disable != "none" {
if enable != none && disable != none {
return nil, fmt.Errorf("can't use -enable and -disable flags at the same time")
}
if enable == "all" || disable == "" {
if enable == all || disable == "" {
return defaultFeatures(true), nil
}
if disable == "all" || enable == "" {
if disable == all || enable == "" {
return defaultFeatures(false), nil
}
var items []string
var features Features
if enable != "none" {
if enable != none {
items = strings.Split(enable, ",")
features = defaultFeatures(false)
} else {
@ -286,7 +290,7 @@ func ParseFeaturesFlags(enable string, disable string, defaultValue bool) (Featu
return nil, fmt.Errorf("unknown feature specified: %s", item)
}
feature := features[item]
feature.Enabled = (enable != "none")
feature.Enabled = enable != none
features[item] = feature
}
return features, nil

View File

@ -435,8 +435,7 @@ func (r *randGen) createResource(s *state, res *ResourceType, dir Dir) (arg Arg,
func (r *randGen) generateText(kind TextKind) []byte {
switch kind {
case TextTarget:
if r.target.Arch == "amd64" || r.target.Arch == "386" {
cfg := createTargetIfuzzConfig(r.target)
if cfg := createTargetIfuzzConfig(r.target); cfg != nil {
return ifuzz.Generate(cfg, r.Rand)
}
fallthrough
@ -456,8 +455,7 @@ func (r *randGen) generateText(kind TextKind) []byte {
func (r *randGen) mutateText(kind TextKind, text []byte) []byte {
switch kind {
case TextTarget:
if r.target.Arch == "amd64" || r.target.Arch == "386" {
cfg := createTargetIfuzzConfig(r.target)
if cfg := createTargetIfuzzConfig(r.target); cfg != nil {
return ifuzz.Mutate(cfg, r.Rand, text)
}
fallthrough
@ -489,7 +487,7 @@ func createTargetIfuzzConfig(target *Target) *ifuzz.Config {
case "386":
cfg.Mode = ifuzz.ModeProt32
default:
panic("unknown text kind")
return nil
}
return cfg
}

View File

@ -541,9 +541,14 @@ func isNetlinkPolicy(fields []prog.Field) bool {
return haveAttr
}
const (
nlattrT = "nlattr_t"
nlattrTT = "nlattr_tt"
)
func isNlattr(typ prog.Type) bool {
name := typ.TemplateName()
return name == "nlattr_t" || name == "nlattr_tt"
return name == nlattrT || name == nlattrTT
}
func checkNetlinkPolicy(structMap map[string]prog.Type, typ prog.Type, fields []prog.Field,
@ -586,7 +591,7 @@ func checkNetlinkPolicy(structMap map[string]prog.Type, typ prog.Type, fields []
func checkNetlinkAttr(typ *prog.StructType, policy nlaPolicy) string {
payload := typ.Fields[2].Type
if typ.TemplateName() == "nlattr_tt" {
if typ.TemplateName() == nlattrTT {
payload = typ.Fields[4].Type
}
if warn := checkAttrType(typ, payload, policy); warn != "" {
@ -657,11 +662,11 @@ func checkAttrType(typ *prog.StructType, payload prog.Type, policy nlaPolicy) st
return "expect string"
}
case NLA_NESTED:
if typ.TemplateName() != "nlattr_tt" || typ.Fields[3].Type.(*prog.ConstType).Val != 1 {
if typ.TemplateName() != nlattrTT || typ.Fields[3].Type.(*prog.ConstType).Val != 1 {
return "should be nlnest"
}
case NLA_BITFIELD32:
if typ.TemplateName() != "nlattr_t" || payload.TemplateName() != "nla_bitfield32" {
if typ.TemplateName() != nlattrT || payload.TemplateName() != "nla_bitfield32" {
return "should be nlattr[nla_bitfield32]"
}
case NLA_NESTED_ARRAY, NLA_REJECT: