Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gocritic lint issues #1696

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ linters-settings:
- standard
- default
- prefix(github.com/spf13/viper)
gocritic:
# Enable multiple checks by tags. See "Tags" section in https://github.com/go-critic/go-critic#usage.
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- importShadow
golint:
min-confidence: 0
goimports:
Expand All @@ -22,6 +32,7 @@ linters:
- exhaustive
- exportloopref
- gci
- gocritic
- godot
- gofmt
- gofumpt
Expand Down Expand Up @@ -63,7 +74,6 @@ linters:
# - gochecknoinits
# - gocognit
# - goconst
# - gocritic
# - gocyclo
# - gosec
# - gosimple
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/dotenv/map_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// flattenAndMergeMap recursively flattens the given map into a new map
// Code is based on the function with the same name in the main package.
// TODO: move it to a common place.
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any {
if shadow != nil && prefix != "" && shadow[prefix] != nil {
// prefix is shadowed => nothing more to flatten
return shadow
Expand Down
6 changes: 3 additions & 3 deletions internal/encoding/ini/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Codec struct {
LoadOptions LoadOptions
}

func (c Codec) Encode(v map[string]any) ([]byte, error) {
func (c *Codec) Encode(v map[string]any) ([]byte, error) {
alexandear marked this conversation as resolved.
Show resolved Hide resolved
cfg := ini.Empty()
ini.PrettyFormat = false

Expand Down Expand Up @@ -62,7 +62,7 @@ func (c Codec) Encode(v map[string]any) ([]byte, error) {
return buf.Bytes(), nil
}

func (c Codec) Decode(b []byte, v map[string]any) error {
func (c *Codec) Decode(b []byte, v map[string]any) error {
cfg := ini.Empty(c.LoadOptions)

err := cfg.Append(b)
Expand Down Expand Up @@ -90,7 +90,7 @@ func (c Codec) Decode(b []byte, v map[string]any) error {
return nil
}

func (c Codec) keyDelimiter() string {
func (c *Codec) keyDelimiter() string {
if c.KeyDelimiter == "" {
return "."
}
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/ini/map_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func deepSearch(m map[string]any, path []string) map[string]any {
// flattenAndMergeMap recursively flattens the given map into a new map
// Code is based on the function with the same name in the main package.
// TODO: move it to a common place.
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any {
if shadow != nil && prefix != "" && shadow[prefix] != nil {
// prefix is shadowed => nothing more to flatten
return shadow
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/javaproperties/map_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func deepSearch(m map[string]any, path []string) map[string]any {
// flattenAndMergeMap recursively flattens the given map into a new map
// Code is based on the function with the same name in the main package.
// TODO: move it to a common place.
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any {
if shadow != nil && prefix != "" && shadow[prefix] != nil {
// prefix is shadowed => nothing more to flatten
return shadow
Expand Down
1 change: 1 addition & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (n *discardHandler) Enabled(_ context.Context, _ slog.Level) bool {
return false
}

//nolint:gocritic // hugeParam: _ is heavy (288 bytes); consider passing it by pointer
alexandear marked this conversation as resolved.
Show resolved Hide resolved
func (n *discardHandler) Handle(_ context.Context, _ slog.Record) error {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions overrides_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir
v := New()
firstKeys := strings.Split(firstPath, v.keyDelim)
if assert == nil ||
len(firstKeys) == 0 || len(firstKeys[0]) == 0 {
len(firstKeys) == 0 || firstKeys[0] == "" {
return v
}

Expand All @@ -115,7 +115,7 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir

// Override and check new value
secondKeys := strings.Split(secondPath, v.keyDelim)
if len(secondKeys) == 0 || len(secondKeys[0]) == 0 {
if len(secondKeys) == 0 || secondKeys[0] == "" {
return v
}
v.Set(secondPath, secondValue)
Expand All @@ -129,7 +129,7 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir
// configuration map of the given layer, and that the final value equals the one given.
func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string, value any) {
if assert == nil || v == nil ||
len(keys) == 0 || len(keys[0]) == 0 {
len(keys) == 0 || keys[0] == "" {
return
}

Expand Down
2 changes: 1 addition & 1 deletion remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (rc remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error)
return bytes.NewReader(resp), nil
}

func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (responseCh <-chan *viper.RemoteResponse, quitCh chan bool) {
alexandear marked this conversation as resolved.
Show resolved Hide resolved
cm, err := getConfigManager(rp)
if err != nil {
return nil, nil
Expand Down
17 changes: 10 additions & 7 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (v *Viper) resetEncoding() {
}

{
codec := ini.Codec{
codec := &ini.Codec{
KeyDelimiter: v.keyDelim,
LoadOptions: v.iniLoadOptions,
}
Expand Down Expand Up @@ -957,7 +957,8 @@ func (v *Viper) Sub(key string) *Viper {
}

if reflect.TypeOf(data).Kind() == reflect.Map {
subv.parents = append(v.parents, strings.ToLower(key))
subv.parents = append([]string(nil), v.parents...)
subv.parents = append(subv.parents, strings.ToLower(key))
subv.automaticEnvApplied = v.automaticEnvApplied
subv.envPrefix = v.envPrefix
subv.envKeyReplacer = v.envKeyReplacer
Expand Down Expand Up @@ -1409,7 +1410,7 @@ func readAsCSV(val string) ([]string, error) {
func stringToStringConv(val string) any {
val = strings.Trim(val, "[]")
// An empty string would cause an empty map
if len(val) == 0 {
if val == "" {
return map[string]any{}
}
r := csv.NewReader(strings.NewReader(val))
Expand All @@ -1433,7 +1434,7 @@ func stringToStringConv(val string) any {
func stringToIntConv(val string) any {
val = strings.Trim(val, "[]")
// An empty string would cause an empty map
if len(val) == 0 {
if val == "" {
return map[string]any{}
}
ss := strings.Split(val, ",")
Expand Down Expand Up @@ -1481,13 +1482,13 @@ func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {

// RegisterAlias creates an alias that provides another accessor for the same key.
// This enables one to change a name without breaking the application.
func RegisterAlias(alias string, key string) { v.RegisterAlias(alias, key) }
func RegisterAlias(alias, key string) { v.RegisterAlias(alias, key) }

func (v *Viper) RegisterAlias(alias string, key string) {
func (v *Viper) RegisterAlias(alias, key string) {
v.registerAlias(alias, strings.ToLower(key))
}

func (v *Viper) registerAlias(alias string, key string) {
func (v *Viper) registerAlias(alias, key string) {
alias = strings.ToLower(alias)
if alias != key && alias != v.realKey(key) {
_, exists := v.aliases[alias]
Expand Down Expand Up @@ -2152,6 +2153,8 @@ func (v *Viper) SetConfigPermissions(perm os.FileMode) {
}

// IniLoadOptions sets the load options for ini parsing.
//
//nolint:gocritic // hugeParam: in is heavy (114 bytes); consider passing it by pointer
func IniLoadOptions(in ini.LoadOptions) Option {
return optionFunc(func(v *Viper) {
v.iniLoadOptions = in
Expand Down
33 changes: 17 additions & 16 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,15 @@ func initIni() {
}

// initDirs makes directories for testing.
func initDirs(t *testing.T) (string, string) {
var (
testDirs = []string{`a a`, `b`, `C_`}
config = `improbable`
)
func initDirs(t *testing.T) (root, config string) {
testDirs := []string{`a a`, `b`, `C_`}
config = `improbable`

if runtime.GOOS != "windows" {
testDirs = append(testDirs, `d\d`)
}

root := t.TempDir()
root = t.TempDir()

for _, dir := range testDirs {
innerDir := filepath.Join(root, dir)
Expand Down Expand Up @@ -428,7 +426,7 @@ func TestReadInConfig(t *testing.T) {
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
require.NoError(t, err)

_, err = file.Write([]byte(`key: value`))
_, err = file.WriteString(`key: value`)
require.NoError(t, err)

file.Close()
Expand All @@ -453,7 +451,7 @@ func TestReadInConfig(t *testing.T) {
file, err := fs.Create(testutil.AbsFilePath(t, "/etc/viper/config.yaml"))
require.NoError(t, err)

_, err = file.Write([]byte(`key: value`))
_, err = file.WriteString(`key: value`)
require.NoError(t, err)

file.Close()
Expand Down Expand Up @@ -930,7 +928,8 @@ func TestUnmarshalWithDecoderOptions(t *testing.T) {
if raw == "" {
return m, nil
}
return m, json.Unmarshal([]byte(raw), &m)
err := json.Unmarshal([]byte(raw), &m)
return m, err
},
))

Expand Down Expand Up @@ -2237,21 +2236,21 @@ func doTestCaseInsensitive(t *testing.T, typ, config string) {
assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q")))
}

func newViperWithConfigFile(t *testing.T) (*Viper, string) {
func newViperWithConfigFile(t *testing.T) (v *Viper, configFile string) {
alexandear marked this conversation as resolved.
Show resolved Hide resolved
watchDir := t.TempDir()
configFile := path.Join(watchDir, "config.yaml")
configFile = path.Join(watchDir, "config.yaml")
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
require.NoError(t, err)
v := New()
v = New()
v.SetConfigFile(configFile)
err = v.ReadInConfig()
require.NoError(t, err)
require.Equal(t, "bar", v.Get("foo"))
return v, configFile
}

func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
watchDir := t.TempDir()
func newViperWithSymlinkedConfigFile(t *testing.T) (v *Viper, watchDir, configFile string) {
watchDir = t.TempDir()
dataDir1 := path.Join(watchDir, "data1")
err := os.Mkdir(dataDir1, 0o777)
require.NoError(t, err)
Expand All @@ -2262,11 +2261,11 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
// now, symlink the tm `data1` dir to `data` in the baseDir
os.Symlink(dataDir1, path.Join(watchDir, "data"))
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
configFile := path.Join(watchDir, "config.yaml")
configFile = path.Join(watchDir, "config.yaml")
os.Symlink(path.Join(watchDir, "data", "config.yaml"), configFile)
t.Logf("Config file location: %s\n", path.Join(watchDir, "config.yaml"))
// init Viper
v := New()
v = New()
v.SetConfigFile(configFile)
err = v.ReadInConfig()
require.NoError(t, err)
Expand Down Expand Up @@ -2506,6 +2505,8 @@ func BenchmarkGetBoolFromMap(b *testing.B) {
}

// Skip some tests on Windows that kept failing when Windows was added to the CI as a target.
//
//nolint:gocritic // sloppyTestFuncName
func skipWindows(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skip test on Windows")
Expand Down
Loading