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

refactor: enable gocritic linter and fix lint issues #342

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

alexandear
Copy link
Contributor

@alexandear alexandear commented Nov 20, 2024

The PR enables gocritic in golangci-lint's config and fixes appeared lint issues:

❯ golangci-lint run
env.go:606:40: emptyStringTest: replace `len(fieldParams.OwnKey) > 0` with `fieldParams.OwnKey != ""` (gocritic)
        if fieldParams.Required && !exists && len(fieldParams.OwnKey) > 0 {
                                              ^
env.go:266:12: evalOrder: may want to evaluate Parse(&t) before the return statement (gocritic)
        return t, Parse(&t)
                  ^
env.go:273:12: evalOrder: may want to evaluate ParseWithOptions(&t, opts) before the return statement (gocritic)
        return t, ParseWithOptions(&t, opts)
                  ^
env.go:641:1: paramTypeCombine: func(key, defaultValue string, defExists bool, envs map[string]string) (val string, exists bool, isDefault bool) could be replaced with func(key, defaultValue string, defExists bool, envs map[string]string) (val string, exists, isDefault bool) (gocritic)
func getOr(key, defaultValue string, defExists bool, envs map[string]string) (val string, exists bool, isDefault bool) {
^
error.go:112:1: deprecatedComment: use `Deprecated: ` (note the casing) instead of `deprecated: ` (gocritic)
// deprecated: use VarIsNotSetError
^
error.go:130:1: deprecatedComment: use `Deprecated: ` (note the casing) instead of `deprecated: ` (gocritic)
// deprecated: use EmptyVarError
^
error.go:71:9: sprintfQuotedString: use %q instead of "%s" for quoted strings (gocritic)
        return fmt.Sprintf(`parse error on field "%s" of type "%s": %v`, e.Name, e.Type, e.Err)
               ^
error.go:92:9: sprintfQuotedString: use %q instead of "%s" for quoted strings (gocritic)
        return fmt.Sprintf(`no parser found for field "%s" of type "%s"`, e.Name, e.Type)
               ^
error.go:158:9: sprintfQuotedString: use %q instead of "%s" for quoted strings (gocritic)
        return fmt.Sprintf(`could not load content of file "%s" from variable %s: %v`, e.Filename, e.Key, e.Err)
               ^
env_test.go:279:25: stringConcatSimplify: suggestion: str1 + ":" + str2 (gocritic)
        t.Setenv("SEPSTRINGS", strings.Join([]string{str1, str2}, ":"))
                               ^
example_test.go:403:9: preferStringWriter: f.WriteString("super secret") should be preferred to the io.WriteString(f, "super secret") (gocritic)
        _, _ = io.WriteString(f, "super secret")
               ^

Copy link

codecov bot commented Nov 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (1f955b7) to head (d116bb4).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #342   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines          626       628    +2     
=========================================
+ Hits           626       628    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +3 to +10
enabled-checks:
- emptyStringTest
- evalOrder
- paramTypeCombine
- preferStringWriter
- sprintfQuotedString
- stringConcatSimplify
- yodaStyleExpr
Copy link

@ccoVeille ccoVeille Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to copy paste the descriptions available for each linters.

You can find them in enabled-checks node of .golangci-lint.reference.yml

Comment on lines +3 to +10
enabled-checks:
- emptyStringTest
- evalOrder
- paramTypeCombine
- preferStringWriter
- sprintfQuotedString
- stringConcatSimplify
- yodaStyleExpr
Copy link

@ccoVeille ccoVeille Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to enable them all or enable them by tags

and disable the noisy ones, explaining why.

This way new rules are added automatically. But I do that on the repositories I maintain

@caarlos0 caarlos0 merged commit f68d1dc into caarlos0:main Nov 26, 2024
17 checks passed
@caarlos0
Copy link
Owner

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants