Skip to content

Commit ee30b44

Browse files
authored
go-critic: fix invalid type conversions. (#2186)
1 parent d3705d0 commit ee30b44

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

pkg/golinters/gocritic.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"go/ast"
66
"go/types"
77
"path/filepath"
8+
"reflect"
89
"runtime"
910
"sort"
1011
"strings"
@@ -87,7 +88,7 @@ func configureCheckerInfo(info *gocriticlinter.CheckerInfo, allParams map[string
8788
for k, p := range params {
8889
v, ok := infoParams[k]
8990
if ok {
90-
v.Value = p
91+
v.Value = normalizeCheckerParamsValue(p)
9192
continue
9293
}
9394

@@ -110,6 +111,26 @@ func configureCheckerInfo(info *gocriticlinter.CheckerInfo, allParams map[string
110111
return nil
111112
}
112113

114+
// normalizeCheckerParamsValue normalizes value types.
115+
// go-critic asserts that CheckerParam.Value has some specific types,
116+
// but the file parsers (TOML, YAML, JSON) don't create the same representation for raw type.
117+
// then we have to convert value types into the expected value types.
118+
// Maybe in the future, this kind of conversion will be done in go-critic itself.
119+
//nolint:exhaustive // only 3 types (int, bool, and string) are supported by CheckerParam.Value
120+
func normalizeCheckerParamsValue(p interface{}) interface{} {
121+
rv := reflect.ValueOf(p)
122+
switch rv.Type().Kind() {
123+
case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
124+
return int(rv.Int())
125+
case reflect.Bool:
126+
return rv.Bool()
127+
case reflect.String:
128+
return rv.String()
129+
default:
130+
return p
131+
}
132+
}
133+
113134
func buildEnabledCheckers(lintCtx *linter.Context, linterCtx *gocriticlinter.Context) ([]*gocriticlinter.Checker, error) {
114135
s := lintCtx.Settings().Gocritic
115136
allParams := s.GetLowercasedParams()

0 commit comments

Comments
 (0)