Skip to content

Commit f0c4ccd

Browse files
alexandearsagikazarmark
authored andcommitted
fix: gocritic lint issues
1 parent 3a23b80 commit f0c4ccd

File tree

10 files changed

+49
-34
lines changed

10 files changed

+49
-34
lines changed

.golangci.yaml

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ linters-settings:
77
- standard
88
- default
99
- prefix(github.com/spf13/viper)
10+
gocritic:
11+
# Enable multiple checks by tags. See "Tags" section in https://github.com/go-critic/go-critic#usage.
12+
enabled-tags:
13+
- diagnostic
14+
- experimental
15+
- opinionated
16+
- performance
17+
- style
18+
disabled-checks:
19+
- importShadow
1020
golint:
1121
min-confidence: 0
1222
goimports:
@@ -22,6 +32,7 @@ linters:
2232
- exhaustive
2333
- exportloopref
2434
- gci
35+
- gocritic
2536
- godot
2637
- gofmt
2738
- gofumpt
@@ -63,7 +74,6 @@ linters:
6374
# - gochecknoinits
6475
# - gocognit
6576
# - goconst
66-
# - gocritic
6777
# - gocyclo
6878
# - gosec
6979
# - gosimple

internal/encoding/dotenv/map_utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
// flattenAndMergeMap recursively flattens the given map into a new map
1010
// Code is based on the function with the same name in the main package.
1111
// TODO: move it to a common place.
12-
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
12+
func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any {
1313
if shadow != nil && prefix != "" && shadow[prefix] != nil {
1414
// prefix is shadowed => nothing more to flatten
1515
return shadow

internal/encoding/ini/codec.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Codec struct {
1919
LoadOptions LoadOptions
2020
}
2121

22-
func (c Codec) Encode(v map[string]any) ([]byte, error) {
22+
func (c *Codec) Encode(v map[string]any) ([]byte, error) {
2323
cfg := ini.Empty()
2424
ini.PrettyFormat = false
2525

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

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

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

93-
func (c Codec) keyDelimiter() string {
93+
func (c *Codec) keyDelimiter() string {
9494
if c.KeyDelimiter == "" {
9595
return "."
9696
}

internal/encoding/ini/map_utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func deepSearch(m map[string]any, path []string) map[string]any {
4242
// flattenAndMergeMap recursively flattens the given map into a new map
4343
// Code is based on the function with the same name in the main package.
4444
// TODO: move it to a common place.
45-
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
45+
func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any {
4646
if shadow != nil && prefix != "" && shadow[prefix] != nil {
4747
// prefix is shadowed => nothing more to flatten
4848
return shadow

internal/encoding/javaproperties/map_utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func deepSearch(m map[string]any, path []string) map[string]any {
4242
// flattenAndMergeMap recursively flattens the given map into a new map
4343
// Code is based on the function with the same name in the main package.
4444
// TODO: move it to a common place.
45-
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
45+
func flattenAndMergeMap(shadow, m map[string]any, prefix, delimiter string) map[string]any {
4646
if shadow != nil && prefix != "" && shadow[prefix] != nil {
4747
// prefix is shadowed => nothing more to flatten
4848
return shadow

logger.go

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (n *discardHandler) Enabled(_ context.Context, _ slog.Level) bool {
5555
return false
5656
}
5757

58+
//nolint:gocritic // hugeParam: _ is heavy (288 bytes); consider passing it by pointer
5859
func (n *discardHandler) Handle(_ context.Context, _ slog.Record) error {
5960
return nil
6061
}

overrides_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir
9797
v := New()
9898
firstKeys := strings.Split(firstPath, v.keyDelim)
9999
if assert == nil ||
100-
len(firstKeys) == 0 || len(firstKeys[0]) == 0 {
100+
len(firstKeys) == 0 || firstKeys[0] == "" {
101101
return v
102102
}
103103

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

116116
// Override and check new value
117117
secondKeys := strings.Split(secondPath, v.keyDelim)
118-
if len(secondKeys) == 0 || len(secondKeys[0]) == 0 {
118+
if len(secondKeys) == 0 || secondKeys[0] == "" {
119119
return v
120120
}
121121
v.Set(secondPath, secondValue)
@@ -129,7 +129,7 @@ func overrideFromLayer(l layer, assert *assert.Assertions, firstPath string, fir
129129
// configuration map of the given layer, and that the final value equals the one given.
130130
func deepCheckValue(assert *assert.Assertions, v *Viper, l layer, keys []string, value any) {
131131
if assert == nil || v == nil ||
132-
len(keys) == 0 || len(keys[0]) == 0 {
132+
len(keys) == 0 || keys[0] == "" {
133133
return
134134
}
135135

remote/remote.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (rc remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error)
4444
return bytes.NewReader(resp), nil
4545
}
4646

47-
func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
47+
func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (responseCh <-chan *viper.RemoteResponse, quitCh chan bool) {
4848
cm, err := getConfigManager(rp)
4949
if err != nil {
5050
return nil, nil

viper.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (v *Viper) resetEncoding() {
345345
}
346346

347347
{
348-
codec := ini.Codec{
348+
codec := &ini.Codec{
349349
KeyDelimiter: v.keyDelim,
350350
LoadOptions: v.iniLoadOptions,
351351
}
@@ -957,7 +957,8 @@ func (v *Viper) Sub(key string) *Viper {
957957
}
958958

959959
if reflect.TypeOf(data).Kind() == reflect.Map {
960-
subv.parents = append(v.parents, strings.ToLower(key))
960+
subv.parents = append([]string(nil), v.parents...)
961+
subv.parents = append(subv.parents, strings.ToLower(key))
961962
subv.automaticEnvApplied = v.automaticEnvApplied
962963
subv.envPrefix = v.envPrefix
963964
subv.envKeyReplacer = v.envKeyReplacer
@@ -1434,7 +1435,7 @@ func readAsCSV(val string) ([]string, error) {
14341435
func stringToStringConv(val string) any {
14351436
val = strings.Trim(val, "[]")
14361437
// An empty string would cause an empty map
1437-
if len(val) == 0 {
1438+
if val == "" {
14381439
return map[string]any{}
14391440
}
14401441
r := csv.NewReader(strings.NewReader(val))
@@ -1458,7 +1459,7 @@ func stringToStringConv(val string) any {
14581459
func stringToIntConv(val string) any {
14591460
val = strings.Trim(val, "[]")
14601461
// An empty string would cause an empty map
1461-
if len(val) == 0 {
1462+
if val == "" {
14621463
return map[string]any{}
14631464
}
14641465
ss := strings.Split(val, ",")
@@ -1506,13 +1507,13 @@ func (v *Viper) SetEnvKeyReplacer(r *strings.Replacer) {
15061507

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

1511-
func (v *Viper) RegisterAlias(alias string, key string) {
1512+
func (v *Viper) RegisterAlias(alias, key string) {
15121513
v.registerAlias(alias, strings.ToLower(key))
15131514
}
15141515

1515-
func (v *Viper) registerAlias(alias string, key string) {
1516+
func (v *Viper) registerAlias(alias, key string) {
15161517
alias = strings.ToLower(alias)
15171518
if alias != key && alias != v.realKey(key) {
15181519
_, exists := v.aliases[alias]
@@ -2181,6 +2182,8 @@ func (v *Viper) SetConfigPermissions(perm os.FileMode) {
21812182
}
21822183

21832184
// IniLoadOptions sets the load options for ini parsing.
2185+
//
2186+
//nolint:gocritic // hugeParam: in is heavy (114 bytes); consider passing it by pointer
21842187
func IniLoadOptions(in ini.LoadOptions) Option {
21852188
return optionFunc(func(v *Viper) {
21862189
v.iniLoadOptions = in

viper_test.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,15 @@ func initIni() {
234234
}
235235

236236
// initDirs makes directories for testing.
237-
func initDirs(t *testing.T) (string, string) {
238-
var (
239-
testDirs = []string{`a a`, `b`, `C_`}
240-
config = `improbable`
241-
)
237+
func initDirs(t *testing.T) (root, config string) {
238+
testDirs := []string{`a a`, `b`, `C_`}
239+
config = `improbable`
242240

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

247-
root := t.TempDir()
245+
root = t.TempDir()
248246

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

431-
_, err = file.Write([]byte(`key: value`))
429+
_, err = file.WriteString(`key: value`)
432430
require.NoError(t, err)
433431

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

456-
_, err = file.Write([]byte(`key: value`))
454+
_, err = file.WriteString(`key: value`)
457455
require.NoError(t, err)
458456

459457
file.Close()
@@ -936,7 +934,8 @@ func TestUnmarshalWithDecoderOptions(t *testing.T) {
936934
if raw == "" {
937935
return m, nil
938936
}
939-
return m, json.Unmarshal([]byte(raw), &m)
937+
err := json.Unmarshal([]byte(raw), &m)
938+
return m, err
940939
},
941940
))
942941

@@ -2343,21 +2342,21 @@ func doTestCaseInsensitive(t *testing.T, typ, config string) {
23432342
assert.Equal(t, 5, cast.ToInt(Get("ef.lm.p.q")))
23442343
}
23452344

2346-
func newViperWithConfigFile(t *testing.T) (*Viper, string) {
2345+
func newViperWithConfigFile(t *testing.T) (v *Viper, configFile string) {
23472346
watchDir := t.TempDir()
2348-
configFile := path.Join(watchDir, "config.yaml")
2347+
configFile = path.Join(watchDir, "config.yaml")
23492348
err := os.WriteFile(configFile, []byte("foo: bar\n"), 0o640)
23502349
require.NoError(t, err)
2351-
v := New()
2350+
v = New()
23522351
v.SetConfigFile(configFile)
23532352
err = v.ReadInConfig()
23542353
require.NoError(t, err)
23552354
require.Equal(t, "bar", v.Get("foo"))
23562355
return v, configFile
23572356
}
23582357

2359-
func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
2360-
watchDir := t.TempDir()
2358+
func newViperWithSymlinkedConfigFile(t *testing.T) (v *Viper, watchDir, configFile string) {
2359+
watchDir = t.TempDir()
23612360
dataDir1 := path.Join(watchDir, "data1")
23622361
err := os.Mkdir(dataDir1, 0o777)
23632362
require.NoError(t, err)
@@ -2368,11 +2367,11 @@ func newViperWithSymlinkedConfigFile(t *testing.T) (*Viper, string, string) {
23682367
// now, symlink the tm `data1` dir to `data` in the baseDir
23692368
os.Symlink(dataDir1, path.Join(watchDir, "data"))
23702369
// and link the `<watchdir>/datadir1/config.yaml` to `<watchdir>/config.yaml`
2371-
configFile := path.Join(watchDir, "config.yaml")
2370+
configFile = path.Join(watchDir, "config.yaml")
23722371
os.Symlink(path.Join(watchDir, "data", "config.yaml"), configFile)
23732372
t.Logf("Config file location: %s\n", path.Join(watchDir, "config.yaml"))
23742373
// init Viper
2375-
v := New()
2374+
v = New()
23762375
v.SetConfigFile(configFile)
23772376
err = v.ReadInConfig()
23782377
require.NoError(t, err)
@@ -2612,6 +2611,8 @@ func BenchmarkGetBoolFromMap(b *testing.B) {
26122611
}
26132612

26142613
// Skip some tests on Windows that kept failing when Windows was added to the CI as a target.
2614+
//
2615+
//nolint:gocritic // sloppyTestFuncName
26152616
func skipWindows(t *testing.T) {
26162617
if runtime.GOOS == "windows" {
26172618
t.Skip("Skip test on Windows")

0 commit comments

Comments
 (0)