Skip to content

Commit

Permalink
add varnamelen linter (golangci#2240)
Browse files Browse the repository at this point in the history
  • Loading branch information
blizzy78 authored and SeigeC committed Apr 4, 2023
1 parent 2fff7c4 commit 106aa1a
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 6 deletions.
15 changes: 15 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,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().
WithURL("https://github.com/blizzy78/varnamelen"),

linter.NewConfig(golinters.NewCheckBannedFunc()).
WithPresets(linter.PresetStyle),
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++
}

0 comments on commit 106aa1a

Please sign in to comment.