Skip to content

Commit 106aa1a

Browse files
blizzy78SeigeC
authored andcommitted
add varnamelen linter (golangci#2240)
1 parent 2fff7c4 commit 106aa1a

File tree

7 files changed

+102
-6
lines changed

7 files changed

+102
-6
lines changed

.golangci.example.yml

+15
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,21 @@ linters-settings:
650650
# Select the Go version to target. The default is '1.13'.
651651
go: "1.15"
652652

653+
varnamelen:
654+
# The longest distance, in source lines, that is being considered a "small scope." (defaults to 5)
655+
# Variables used in at most this many lines will be ignored.
656+
max-distance: 5
657+
# The minimum length of a variable's name that is considered "long." (defaults to 3)
658+
# Variable names that are at least this long will be ignored.
659+
min-name-length: 3
660+
# Check method receiver names. (defaults to false)
661+
check-receiver: false
662+
# Check named return values. (defaults to false)
663+
check-return: false
664+
# Optional list of variable names that should be ignored completely. (defaults to empty list)
665+
ignore-names:
666+
- err
667+
653668
whitespace:
654669
multi-if: false # Enforces newlines (or comments) after every multi-line if statement
655670
multi-func: false # Enforces newlines (or comments) after every multi-line function signature

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/ashanbrown/forbidigo v1.2.0
1414
github.com/ashanbrown/makezero v0.0.0-20210520155254-b6261585ddde
1515
github.com/bkielbasa/cyclop v1.2.0
16+
github.com/blizzy78/varnamelen v0.3.0
1617
github.com/bombsimon/wsl/v3 v3.3.0
1718
github.com/butuzov/ireturn v0.1.1
1819
github.com/charithe/durationcheck v0.0.9

go.sum

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ type LintersSettings struct {
136136
Unparam UnparamSettings
137137
Unused StaticCheckSettings
138138
Varcheck VarCheckSettings
139+
Varnamelen VarnamelenSettings
139140
Whitespace WhitespaceSettings
140141
Wrapcheck WrapcheckSettings
141142
WSL WSLSettings
@@ -478,6 +479,14 @@ type VarCheckSettings struct {
478479
CheckExportedFields bool `mapstructure:"exported-fields"`
479480
}
480481

482+
type VarnamelenSettings struct {
483+
MaxDistance int `mapstructure:"max-distance"`
484+
MinNameLength int `mapstructure:"min-name-length"`
485+
CheckReceiver bool `mapstructure:"check-receiver"`
486+
CheckReturn bool `mapstructure:"check-return"`
487+
IgnoreNames []string `mapstructure:"ignore-names"`
488+
}
489+
481490
type WhitespaceSettings struct {
482491
MultiIf bool `mapstructure:"multi-if"`
483492
MultiFunc bool `mapstructure:"multi-func"`

pkg/golinters/varnamelen.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package golinters
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
7+
"github.com/blizzy78/varnamelen"
8+
"golang.org/x/tools/go/analysis"
9+
10+
"github.com/golangci/golangci-lint/pkg/config"
11+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
12+
)
13+
14+
func NewVarnamelen(settings *config.VarnamelenSettings) *goanalysis.Linter {
15+
a := varnamelen.NewAnalyzer()
16+
17+
cfg := map[string]map[string]interface{}{}
18+
if settings != nil {
19+
vnlCfg := map[string]interface{}{
20+
"checkReceiver": strconv.FormatBool(settings.CheckReceiver),
21+
"checkReturn": strconv.FormatBool(settings.CheckReturn),
22+
"ignoreNames": strings.Join(settings.IgnoreNames, ","),
23+
}
24+
25+
if settings.MaxDistance > 0 {
26+
vnlCfg["maxDistance"] = strconv.Itoa(settings.MaxDistance)
27+
}
28+
if settings.MinNameLength > 0 {
29+
vnlCfg["minNameLength"] = strconv.Itoa(settings.MinNameLength)
30+
}
31+
32+
cfg[a.Name] = vnlCfg
33+
}
34+
35+
return goanalysis.NewLinter(
36+
a.Name,
37+
"checks that the length of a variable's name matches its scope",
38+
[]*analysis.Analyzer{a},
39+
cfg,
40+
).WithLoadMode(goanalysis.LoadModeSyntax)
41+
}

pkg/lint/lintersdb/manager.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
121121
var testpackageCfg *config.TestpackageSettings
122122
var thelperCfg *config.ThelperSettings
123123
var unusedCfg *config.StaticCheckSettings
124+
var varnamelenCfg *config.VarnamelenSettings
124125
var wrapcheckCfg *config.WrapcheckSettings
125126
var nlreturnCfg *config.NlreturnSettings
126127

@@ -146,6 +147,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
146147
testpackageCfg = &m.cfg.LintersSettings.Testpackage
147148
thelperCfg = &m.cfg.LintersSettings.Thelper
148149
unusedCfg = &m.cfg.LintersSettings.Unused
150+
varnamelenCfg = &m.cfg.LintersSettings.Varnamelen
149151
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
150152
nlreturnCfg = &m.cfg.LintersSettings.Nlreturn
151153
}
@@ -511,30 +513,35 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
511513
WithPresets(linter.PresetStyle).
512514
WithURL("https://github.com/ldez/tagliatelle"),
513515
linter.NewConfig(golinters.NewErrName()).
516+
WithSince("v1.42.0").
514517
WithPresets(linter.PresetStyle).
515518
WithLoadForGoAnalysis().
516-
WithURL("https://github.com/Antonboom/errname").
517-
WithSince("v1.42.0"),
519+
WithURL("https://github.com/Antonboom/errname"),
518520
linter.NewConfig(golinters.NewIreturn(ireturnCfg)).
519521
WithSince("v1.43.0").
520522
WithPresets(linter.PresetStyle).
521523
WithLoadForGoAnalysis().
522524
WithURL("https://github.com/butuzov/ireturn"),
523525
linter.NewConfig(golinters.NewNilNil(nilNilCfg)).
526+
WithSince("v1.43.0").
524527
WithPresets(linter.PresetStyle).
525528
WithLoadForGoAnalysis().
526-
WithURL("https://github.com/Antonboom/nilnil").
527-
WithSince("v1.43.0"),
529+
WithURL("https://github.com/Antonboom/nilnil"),
528530
linter.NewConfig(golinters.NewTenv(tenvCfg)).
529531
WithSince("v1.43.0").
530532
WithPresets(linter.PresetStyle).
531533
WithLoadForGoAnalysis().
532534
WithURL("https://github.com/sivchari/tenv"),
533535
linter.NewConfig(golinters.NewContextCheck()).
536+
WithSince("v1.43.0").
534537
WithPresets(linter.PresetBugs).
535538
WithLoadForGoAnalysis().
536-
WithURL("https://github.com/sylvia7788/contextcheck").
537-
WithSince("v1.43.0"),
539+
WithURL("https://github.com/sylvia7788/contextcheck"),
540+
linter.NewConfig(golinters.NewVarnamelen(varnamelenCfg)).
541+
WithSince("v1.43.0").
542+
WithPresets(linter.PresetStyle).
543+
WithLoadForGoAnalysis().
544+
WithURL("https://github.com/blizzy78/varnamelen"),
538545

539546
linter.NewConfig(golinters.NewCheckBannedFunc()).
540547
WithPresets(linter.PresetStyle),

test/testdata/varnamelen.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//args: -Evarnamelen
2+
package testdata
3+
4+
import "math"
5+
6+
func varnamelen() {
7+
x := math.MinInt8 // ERROR "variable name 'x' is too short for the scope of its usage"
8+
x++
9+
x++
10+
x++
11+
x++
12+
x++
13+
x++
14+
x++
15+
x++
16+
x++
17+
}

0 commit comments

Comments
 (0)