go-critic: fix invalid type conversions. (#2186)
This commit is contained in:
parent
d3705d0e8b
commit
ee30b44e4e
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user