Add ifshort linter (#1587)
This commit is contained in:
parent
f049bfc4ae
commit
750309d157
@ -293,6 +293,12 @@ linters-settings:
|
|||||||
packages-with-error-message:
|
packages-with-error-message:
|
||||||
# specify an error message to output when a blacklisted package is used
|
# specify an error message to output when a blacklisted package is used
|
||||||
- github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
|
- 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:
|
lll:
|
||||||
# max line length, lines longer will be reported. Default is 120.
|
# 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
|
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
|
||||||
|
1
go.mod
1
go.mod
@ -11,6 +11,7 @@ require (
|
|||||||
github.com/bombsimon/wsl/v3 v3.1.0
|
github.com/bombsimon/wsl/v3 v3.1.0
|
||||||
github.com/daixiang0/gci v0.2.8
|
github.com/daixiang0/gci v0.2.8
|
||||||
github.com/denis-tingajkin/go-header v0.4.2
|
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/fatih/color v1.10.0
|
||||||
github.com/go-critic/go-critic v0.5.3
|
github.com/go-critic/go-critic v0.5.3
|
||||||
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b
|
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b
|
||||||
|
3
go.sum
generated
3
go.sum
generated
@ -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/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/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/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.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.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
|
||||||
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
|
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-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-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-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-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-20201114224030-61ea331ec02b/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||||
golang.org/x/tools v0.0.0-20201118003311-bd56c0adb394 h1:O3VD5Fds21mB1WVRTbkiz/HTXESx6Jql5ucPZi69oiM=
|
golang.org/x/tools v0.0.0-20201118003311-bd56c0adb394 h1:O3VD5Fds21mB1WVRTbkiz/HTXESx6Jql5ucPZi69oiM=
|
||||||
|
@ -270,6 +270,7 @@ type LintersSettings struct {
|
|||||||
Makezero MakezeroSettings
|
Makezero MakezeroSettings
|
||||||
Thelper ThelperSettings
|
Thelper ThelperSettings
|
||||||
Forbidigo ForbidigoSettings
|
Forbidigo ForbidigoSettings
|
||||||
|
Ifshort IfshortSettings
|
||||||
Predeclared PredeclaredSettings
|
Predeclared PredeclaredSettings
|
||||||
|
|
||||||
Custom map[string]CustomLinterSettings
|
Custom map[string]CustomLinterSettings
|
||||||
@ -408,6 +409,11 @@ type ThelperSettings struct {
|
|||||||
} `mapstructure:"benchmark"`
|
} `mapstructure:"benchmark"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type IfshortSettings struct {
|
||||||
|
MaxDeclLines int `mapstructure:"max-decl-lines"`
|
||||||
|
MaxDeclChars int `mapstructure:"max-decl-chars"`
|
||||||
|
}
|
||||||
|
|
||||||
type ForbidigoSettings struct {
|
type ForbidigoSettings struct {
|
||||||
Forbid []string `mapstructure:"forbid"`
|
Forbid []string `mapstructure:"forbid"`
|
||||||
}
|
}
|
||||||
|
17
pkg/golinters/ifshort.go
Normal file
17
pkg/golinters/ifshort.go
Normal 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)
|
||||||
|
}
|
@ -344,6 +344,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
|||||||
linter.NewConfig(golinters.NewForbidigo()).
|
linter.NewConfig(golinters.NewForbidigo()).
|
||||||
WithPresets(linter.PresetStyle).
|
WithPresets(linter.PresetStyle).
|
||||||
WithURL("https://github.com/ashanbrown/forbidigo"),
|
WithURL("https://github.com/ashanbrown/forbidigo"),
|
||||||
|
linter.NewConfig(golinters.NewIfshort()).
|
||||||
|
WithPresets(linter.PresetStyle).
|
||||||
|
WithURL("https://github.com/esimonov/ifshort"),
|
||||||
linter.NewConfig(golinters.NewPredeclared(predeclaredCfg)).
|
linter.NewConfig(golinters.NewPredeclared(predeclaredCfg)).
|
||||||
WithPresets(linter.PresetStyle).
|
WithPresets(linter.PresetStyle).
|
||||||
WithURL("https://github.com/nishanths/predeclared"),
|
WithURL("https://github.com/nishanths/predeclared"),
|
||||||
|
11
test/testdata/ifshort.go
vendored
Normal file
11
test/testdata/ifshort.go
vendored
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user