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 2 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
16 changes: 16 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,22 @@ 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
- ctx

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
3 changes: 2 additions & 1 deletion 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.2.0
github.com/bombsimon/wsl/v3 v3.3.0
github.com/butuzov/ireturn v0.1.0
github.com/charithe/durationcheck v0.0.8
Expand Down Expand Up @@ -86,7 +87,7 @@ require (
github.com/uudashr/gocognit v1.0.5
github.com/valyala/quicktemplate v1.6.3
github.com/yeya24/promlinter v0.1.0
golang.org/x/tools v0.1.5
golang.org/x/tools v0.1.6
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
honnef.co/go/tools v0.2.1
mvdan.cc/gofumpt v0.1.1
Expand Down
22 changes: 16 additions & 6 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 @@ -134,6 +134,7 @@ type LintersSettings struct {
Unparam UnparamSettings
Unused StaticCheckSettings
Varcheck VarCheckSettings
Varnamelen VarnamelenSettings
Whitespace WhitespaceSettings
Wrapcheck WrapcheckSettings
WSL WSLSettings
Expand Down Expand Up @@ -467,6 +468,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
48 changes: 48 additions & 0 deletions pkg/golinters/varnamelen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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": boolString(settings.CheckReceiver),
"checkReturn": boolString(settings.CheckReturn),
blizzy78 marked this conversation as resolved.
Show resolved Hide resolved
"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,
)
blizzy78 marked this conversation as resolved.
Show resolved Hide resolved
}

func boolString(b bool) string {
if b {
return "true"
}
return "false"
}
blizzy78 marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,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

if m.cfg != nil {
Expand All @@ -142,6 +143,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
}

Expand Down Expand Up @@ -520,6 +522,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithLoadForGoAnalysis().
WithURL("https://github.com/Antonboom/nilnil").
WithSince("v1.43.0"),
linter.NewConfig(golinters.NewVarnamelen(varnamelenCfg)).
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
blizzy78 marked this conversation as resolved.
Show resolved Hide resolved
WithURL("https://github.com/blizzy78/varnamelen").
WithSince("v1.43.0"),

// 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
15 changes: 15 additions & 0 deletions test/testdata/varnamelen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//args: -Evarnamelen
package testdata

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