Add ifshort linter (#1587)

This commit is contained in:
Eugene Simonov 2021-01-21 00:07:43 +02:00 committed by GitHub
parent f049bfc4ae
commit 750309d157
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 0 deletions

View File

@ -293,6 +293,12 @@ linters-settings:
packages-with-error-message:
# specify an error message to output when a blacklisted package is used
- github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
ifshort:
# Maximum length of variable declaration measured in number of lines, after which linter won't suggest using short syntax.
# Has higher priority than max-decl-chars.
max-decl-lines: 1
# Maximum length of variable declaration measured in number of characters, after which linter won't suggest using short syntax.
max-decl-chars: 30
lll:
# max line length, lines longer will be reported. Default is 120.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option

1
go.mod
View File

@ -11,6 +11,7 @@ require (
github.com/bombsimon/wsl/v3 v3.1.0
github.com/daixiang0/gci v0.2.8
github.com/denis-tingajkin/go-header v0.4.2
github.com/esimonov/ifshort v1.0.0
github.com/fatih/color v1.10.0
github.com/go-critic/go-critic v0.5.3
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b

3
go.sum generated
View File

@ -59,6 +59,8 @@ github.com/denis-tingajkin/go-header v0.4.2 h1:jEeSF4sdv8/3cT/WY8AgDHUoItNSoEZ7q
github.com/denis-tingajkin/go-header v0.4.2/go.mod h1:eLRHAVXzE5atsKAnNRDB90WHCFFnBUn4RN0nRcs1LJA=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/esimonov/ifshort v1.0.0 h1:mcOSoOMVtL4tJyyDTakunR+KFQUywLLAVesiWleGPHU=
github.com/esimonov/ifshort v1.0.0/go.mod h1:yZqNJUrNn20K8Q9n2CrjTKYyVEmX209Hgu+M1LBpeZE=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
@ -579,6 +581,7 @@ golang.org/x/tools v0.0.0-20201001104356-43ebab892c4c/go.mod h1:z6u4i615ZeAfBE4X
golang.org/x/tools v0.0.0-20201002184944-ecd9fd270d5d/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.0.0-20201007032633-0806396f153e/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.0.0-20201011145850-ed2f50202694/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.0.0-20201028025901-8cd080b735b3/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201030010431-2feb2bb1ff51/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201114224030-61ea331ec02b/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201118003311-bd56c0adb394 h1:O3VD5Fds21mB1WVRTbkiz/HTXESx6Jql5ucPZi69oiM=

View File

@ -270,6 +270,7 @@ type LintersSettings struct {
Makezero MakezeroSettings
Thelper ThelperSettings
Forbidigo ForbidigoSettings
Ifshort IfshortSettings
Predeclared PredeclaredSettings
Custom map[string]CustomLinterSettings
@ -408,6 +409,11 @@ type ThelperSettings struct {
} `mapstructure:"benchmark"`
}
type IfshortSettings struct {
MaxDeclLines int `mapstructure:"max-decl-lines"`
MaxDeclChars int `mapstructure:"max-decl-chars"`
}
type ForbidigoSettings struct {
Forbid []string `mapstructure:"forbid"`
}

17
pkg/golinters/ifshort.go Normal file
View File

@ -0,0 +1,17 @@
package golinters
import (
"github.com/esimonov/ifshort/pkg/analyzer"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)
func NewIfshort() *goanalysis.Linter {
return goanalysis.NewLinter(
"ifshort",
"Checks that your code uses short syntax for if-statements whenever possible",
[]*analysis.Analyzer{analyzer.Analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}

View File

@ -344,6 +344,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
linter.NewConfig(golinters.NewForbidigo()).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/ashanbrown/forbidigo"),
linter.NewConfig(golinters.NewIfshort()).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/esimonov/ifshort"),
linter.NewConfig(golinters.NewPredeclared(predeclaredCfg)).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/nishanths/predeclared"),

11
test/testdata/ifshort.go vendored Normal file
View File

@ -0,0 +1,11 @@
//args: -Eifshort
package testdata
func DontUseShortSyntaxWhenPossible() {
getValue := func() interface{} { return nil }
v := getValue() // ERROR "variable 'v' is only used in the if-statement .*"
if v != nil {
return
}
}