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

add varnamelen linter #2240

Merged
merged 8 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,21 @@ linters-settings:
# Select the Go version to target. The default is '1.13'.
go: "1.15"

varnamelen:
# The longest distance, in source lines, that is being considered a "small scope." (defaults to 5)
# Variables used in at most this many lines will be ignored.
max-distance: 5
# The minimum length of a variable's name that is considered "long." (defaults to 3)
# Variable names that are at least this long will be ignored.
min-name-length: 3
# Check method receiver names. (defaults to false)
check-receiver: false
# Check named return values. (defaults to false)
check-return: false
# Optional list of variable names that should be ignored completely. (defaults to empty list)
ignore-names:
- err

whitespace:
multi-if: false # Enforces newlines (or comments) after every multi-line if statement
multi-func: false # Enforces newlines (or comments) after every multi-line function signature
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/ashanbrown/forbidigo v1.2.0
github.com/ashanbrown/makezero v0.0.0-20210520155254-b6261585ddde
github.com/bkielbasa/cyclop v1.2.0
github.com/blizzy78/varnamelen v0.3.0
github.com/bombsimon/wsl/v3 v3.3.0
github.com/butuzov/ireturn v0.1.1
github.com/charithe/durationcheck v0.0.9
Expand Down
6 changes: 6 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type LintersSettings struct {
Unparam UnparamSettings
Unused StaticCheckSettings
Varcheck VarCheckSettings
Varnamelen VarnamelenSettings
Whitespace WhitespaceSettings
Wrapcheck WrapcheckSettings
WSL WSLSettings
Expand Down Expand Up @@ -478,6 +479,14 @@ type VarCheckSettings struct {
CheckExportedFields bool `mapstructure:"exported-fields"`
}

type VarnamelenSettings struct {
MaxDistance int `mapstructure:"max-distance"`
MinNameLength int `mapstructure:"min-name-length"`
CheckReceiver bool `mapstructure:"check-receiver"`
CheckReturn bool `mapstructure:"check-return"`
IgnoreNames []string `mapstructure:"ignore-names"`
}

type WhitespaceSettings struct {
MultiIf bool `mapstructure:"multi-if"`
MultiFunc bool `mapstructure:"multi-func"`
Expand Down
41 changes: 41 additions & 0 deletions pkg/golinters/varnamelen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package golinters

import (
"strconv"
"strings"

"github.com/blizzy78/varnamelen"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewVarnamelen(settings *config.VarnamelenSettings) *goanalysis.Linter {
a := varnamelen.NewAnalyzer()

cfg := map[string]map[string]interface{}{}
if settings != nil {
vnlCfg := map[string]interface{}{
"checkReceiver": strconv.FormatBool(settings.CheckReceiver),
"checkReturn": strconv.FormatBool(settings.CheckReturn),
"ignoreNames": strings.Join(settings.IgnoreNames, ","),
}

if settings.MaxDistance > 0 {
vnlCfg["maxDistance"] = strconv.Itoa(settings.MaxDistance)
}
if settings.MinNameLength > 0 {
vnlCfg["minNameLength"] = strconv.Itoa(settings.MinNameLength)
}

cfg[a.Name] = vnlCfg
}

return goanalysis.NewLinter(
a.Name,
"checks that the length of a variable's name matches its scope",
[]*analysis.Analyzer{a},
cfg,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
19 changes: 13 additions & 6 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var testpackageCfg *config.TestpackageSettings
var thelperCfg *config.ThelperSettings
var unusedCfg *config.StaticCheckSettings
var varnamelenCfg *config.VarnamelenSettings
var wrapcheckCfg *config.WrapcheckSettings
var nlreturnCfg *config.NlreturnSettings

Expand All @@ -146,6 +147,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
testpackageCfg = &m.cfg.LintersSettings.Testpackage
thelperCfg = &m.cfg.LintersSettings.Thelper
unusedCfg = &m.cfg.LintersSettings.Unused
varnamelenCfg = &m.cfg.LintersSettings.Varnamelen
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
nlreturnCfg = &m.cfg.LintersSettings.Nlreturn
}
Expand Down Expand Up @@ -511,30 +513,35 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithURL("https://github.com/ldez/tagliatelle"),
linter.NewConfig(golinters.NewErrName()).
WithSince("v1.42.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/Antonboom/errname").
WithSince("v1.42.0"),
WithURL("https://github.com/Antonboom/errname"),
linter.NewConfig(golinters.NewIreturn(ireturnCfg)).
WithSince("v1.43.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/butuzov/ireturn"),
linter.NewConfig(golinters.NewNilNil(nilNilCfg)).
WithSince("v1.43.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/Antonboom/nilnil").
WithSince("v1.43.0"),
WithURL("https://github.com/Antonboom/nilnil"),
linter.NewConfig(golinters.NewTenv(tenvCfg)).
WithSince("v1.43.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/sivchari/tenv"),
linter.NewConfig(golinters.NewContextCheck()).
WithSince("v1.43.0").
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/sylvia7788/contextcheck").
WithSince("v1.43.0"),
WithURL("https://github.com/sylvia7788/contextcheck"),
linter.NewConfig(golinters.NewVarnamelen(varnamelenCfg)).
WithSince("v1.43.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
blizzy78 marked this conversation as resolved.
Show resolved Hide resolved
WithURL("https://github.com/blizzy78/varnamelen"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
17 changes: 17 additions & 0 deletions test/testdata/varnamelen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//args: -Evarnamelen
package testdata

import "math"

func varnamelen() {
x := math.MinInt8 // ERROR "variable name 'x' is too short for the scope of its usage"
x++
x++
x++
x++
x++
x++
x++
x++
x++
}