Skip to content

Commit f68d1dc

Browse files
authored
refactor: enable gocritic linter and fix lint issues (#342)
1 parent a9fcfa3 commit f68d1dc

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

.golangci.yml

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
linters-settings:
2+
gocritic:
3+
enabled-checks:
4+
- emptyStringTest
5+
- evalOrder
6+
- paramTypeCombine
7+
- preferStringWriter
8+
- sprintfQuotedString
9+
- stringConcatSimplify
10+
- yodaStyleExpr
211
revive:
312
rules:
413
- name: line-length-limit
@@ -15,6 +24,7 @@ linters:
1524
enable:
1625
- thelper
1726
- gofumpt
27+
- gocritic
1828
- tparallel
1929
- unconvert
2030
- unparam

env.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,16 @@ func ParseWithOptions(v interface{}, opts Options) error {
263263
// values from environment variables.
264264
func ParseAs[T any]() (T, error) {
265265
var t T
266-
return t, Parse(&t)
266+
err := Parse(&t)
267+
return t, err
267268
}
268269

269270
// ParseWithOptions parses the given struct type containing `env` tags and
270271
// loads its values from environment variables.
271272
func ParseAsWithOptions[T any](opts Options) (T, error) {
272273
var t T
273-
return t, ParseWithOptions(&t, opts)
274+
err := ParseWithOptions(&t, opts)
275+
return t, err
274276
}
275277

276278
// Must panic is if err is not nil, and returns t otherwise.
@@ -355,10 +357,10 @@ func doParseField(
355357
if !refField.CanSet() {
356358
return nil
357359
}
358-
if reflect.Ptr == refField.Kind() && refField.Elem().Kind() == reflect.Struct && !refField.IsNil() {
360+
if refField.Kind() == reflect.Ptr && refField.Elem().Kind() == reflect.Struct && !refField.IsNil() {
359361
return parseInternal(refField.Interface(), processField, optionsWithEnvPrefix(refTypeField, opts))
360362
}
361-
if reflect.Struct == refField.Kind() && refField.CanAddr() && refField.Type().Name() == "" {
363+
if refField.Kind() == reflect.Struct && refField.CanAddr() && refField.Type().Name() == "" {
362364
return parseInternal(refField.Addr().Interface(), processField, optionsWithEnvPrefix(refTypeField, opts))
363365
}
364366

@@ -384,7 +386,7 @@ func doParseField(
384386
}
385387
}
386388

387-
if reflect.Struct == refField.Kind() {
389+
if refField.Kind() == reflect.Struct {
388390
return doParse(refField, processField, optionsWithEnvPrefix(refTypeField, opts))
389391
}
390392

@@ -397,17 +399,17 @@ func doParseField(
397399

398400
func isSliceOfStructs(refTypeField reflect.StructField, opts Options) bool {
399401
field := refTypeField.Type
400-
if reflect.Ptr == field.Kind() {
402+
if field.Kind() == reflect.Ptr {
401403
field = field.Elem()
402404
}
403405

404-
if reflect.Slice != field.Kind() {
406+
if field.Kind() != reflect.Slice {
405407
return false
406408
}
407409

408410
field = field.Elem()
409411

410-
if reflect.Ptr == field.Kind() {
412+
if field.Kind() == reflect.Ptr {
411413
field = field.Elem()
412414
}
413415

@@ -422,7 +424,7 @@ func isSliceOfStructs(refTypeField reflect.StructField, opts Options) bool {
422424
}
423425

424426
if !ignore {
425-
ignore = reflect.Struct != field.Kind()
427+
ignore = field.Kind() != reflect.Struct
426428
}
427429
return !ignore
428430
}
@@ -603,7 +605,7 @@ func get(fieldParams FieldParams, opts Options) (val string, err error) {
603605
defer os.Unsetenv(fieldParams.Key)
604606
}
605607

606-
if fieldParams.Required && !exists && len(fieldParams.OwnKey) > 0 {
608+
if fieldParams.Required && !exists && fieldParams.OwnKey != "" {
607609
return "", newVarIsNotSetError(fieldParams.Key)
608610
}
609611

@@ -638,7 +640,7 @@ func getFromFile(filename string) (value string, err error) {
638640
return string(b), err
639641
}
640642

641-
func getOr(key, defaultValue string, defExists bool, envs map[string]string) (val string, exists bool, isDefault bool) {
643+
func getOr(key, defaultValue string, defExists bool, envs map[string]string) (val string, exists, isDefault bool) {
642644
value, exists := envs[key]
643645
switch {
644646
case (!exists || key == "") && defExists:
@@ -794,7 +796,7 @@ func handleMap(field reflect.Value, value string, sf reflect.StructField, funcMa
794796
}
795797

796798
func asTextUnmarshaler(field reflect.Value) encoding.TextUnmarshaler {
797-
if reflect.Ptr == field.Kind() {
799+
if field.Kind() == reflect.Ptr {
798800
if field.IsNil() {
799801
field.Set(reflect.New(field.Type().Elem()))
800802
}

env_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func TestParsesEnv(t *testing.T) {
276276
t.Setenv("URL", tos(url1))
277277
t.Setenv("URLS", toss(url1, url2))
278278

279-
t.Setenv("SEPSTRINGS", strings.Join([]string{str1, str2}, ":"))
279+
t.Setenv("SEPSTRINGS", str1+":"+str2)
280280

281281
nonDefinedStr := "nonDefinedStr"
282282
t.Setenv("NONDEFINED_STR", nonDefinedStr)
@@ -1485,7 +1485,7 @@ func TestFileBadFile(t *testing.T) {
14851485
}
14861486

14871487
err := Parse(&config{})
1488-
isErrorWithMessage(t, err, fmt.Sprintf(`env: could not load content of file "%s" from variable SECRET_KEY: open %s: %s`, filename, filename, oserr))
1488+
isErrorWithMessage(t, err, fmt.Sprintf("env: could not load content of file %q from variable SECRET_KEY: open %s: %s", filename, filename, oserr))
14891489
isTrue(t, errors.Is(err, LoadFileContentError{}))
14901490
}
14911491

error.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func newParseError(sf reflect.StructField, err error) error {
6868
}
6969

7070
func (e ParseError) Error() string {
71-
return fmt.Sprintf(`parse error on field "%s" of type "%s": %v`, e.Name, e.Type, e.Err)
71+
return fmt.Sprintf("parse error on field %q of type %q: %v", e.Name, e.Type, e.Err)
7272
}
7373

7474
// The error occurs when pass something that is not a pointer to a struct to Parse
@@ -89,7 +89,7 @@ func newNoParserError(sf reflect.StructField) error {
8989
}
9090

9191
func (e NoParserError) Error() string {
92-
return fmt.Sprintf(`no parser found for field "%s" of type "%s"`, e.Name, e.Type)
92+
return fmt.Sprintf("no parser found for field %q of type %q", e.Name, e.Type)
9393
}
9494

9595
// This error occurs when the given tag is not supported.
@@ -109,7 +109,7 @@ func (e NoSupportedTagOptionError) Error() string {
109109

110110
// This error occurs when the required variable is not set.
111111
//
112-
// deprecated: use VarIsNotSetError
112+
// Deprecated: use VarIsNotSetError.
113113
type EnvVarIsNotSetError = VarIsNotSetError
114114

115115
// This error occurs when the required variable is not set.
@@ -127,7 +127,7 @@ func (e VarIsNotSetError) Error() string {
127127

128128
// This error occurs when the variable which must be not empty is existing but has an empty value
129129
//
130-
// deprecated: use EmptyVarError
130+
// Deprecated: use EmptyVarError.
131131
type EmptyEnvVarError = EmptyVarError
132132

133133
// This error occurs when the variable which must be not empty is existing but has an empty value
@@ -155,7 +155,7 @@ func newLoadFileContentError(filename, key string, err error) error {
155155
}
156156

157157
func (e LoadFileContentError) Error() string {
158-
return fmt.Sprintf(`could not load content of file "%s" from variable %s: %v`, e.Filename, e.Key, e.Err)
158+
return fmt.Sprintf("could not load content of file %q from variable %s: %v", e.Filename, e.Key, e.Err)
159159
}
160160

161161
// This error occurs when it's impossible to convert value using given parser.

example_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package env
33
import (
44
"errors"
55
"fmt"
6-
"io"
76
"os"
87
"reflect"
98
)
@@ -400,7 +399,7 @@ func ExampleParseWithOptions_useFieldName() {
400399
// it.
401400
func ExampleParse_fromFile() {
402401
f, _ := os.CreateTemp("", "")
403-
_, _ = io.WriteString(f, "super secret")
402+
_, _ = f.WriteString("super secret")
404403
_ = f.Close()
405404

406405
type Config struct {

0 commit comments

Comments
 (0)