diff --git a/pkg/golinters/gocritic.go b/pkg/golinters/gocritic.go index 75eb7d30..e98f2344 100644 --- a/pkg/golinters/gocritic.go +++ b/pkg/golinters/gocritic.go @@ -5,6 +5,7 @@ import ( "go/ast" "go/types" "path/filepath" + "reflect" "runtime" "sort" "strings" @@ -87,7 +88,7 @@ func configureCheckerInfo(info *gocriticlinter.CheckerInfo, allParams map[string for k, p := range params { v, ok := infoParams[k] if ok { - v.Value = p + v.Value = normalizeCheckerParamsValue(p) continue } @@ -110,6 +111,26 @@ func configureCheckerInfo(info *gocriticlinter.CheckerInfo, allParams map[string return nil } +// normalizeCheckerParamsValue normalizes value types. +// go-critic asserts that CheckerParam.Value has some specific types, +// but the file parsers (TOML, YAML, JSON) don't create the same representation for raw type. +// then we have to convert value types into the expected value types. +// Maybe in the future, this kind of conversion will be done in go-critic itself. +//nolint:exhaustive // only 3 types (int, bool, and string) are supported by CheckerParam.Value +func normalizeCheckerParamsValue(p interface{}) interface{} { + rv := reflect.ValueOf(p) + switch rv.Type().Kind() { + case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int: + return int(rv.Int()) + case reflect.Bool: + return rv.Bool() + case reflect.String: + return rv.String() + default: + return p + } +} + func buildEnabledCheckers(lintCtx *linter.Context, linterCtx *gocriticlinter.Context) ([]*gocriticlinter.Checker, error) { s := lintCtx.Settings().Gocritic allParams := s.GetLowercasedParams()