Skip to content

Commit

Permalink
go-critic: fix invalid type conversions. (#2186)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Aug 20, 2021
1 parent d3705d0 commit ee30b44
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pkg/golinters/gocritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/ast"
"go/types"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand Down

0 comments on commit ee30b44

Please sign in to comment.