File tree Expand file tree Collapse file tree 7 files changed +47
-0
lines changed Expand file tree Collapse file tree 7 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -293,6 +293,12 @@ linters-settings:
293293 packages-with-error-message :
294294 # specify an error message to output when a blacklisted package is used
295295 - github.com/sirupsen/logrus : " logging is allowed only by logutils.Log"
296+ ifshort :
297+ # Maximum length of variable declaration measured in number of lines, after which linter won't suggest using short syntax.
298+ # Has higher priority than max-decl-chars.
299+ max-decl-lines : 1
300+ # Maximum length of variable declaration measured in number of characters, after which linter won't suggest using short syntax.
301+ max-decl-chars : 30
296302 lll :
297303 # max line length, lines longer will be reported. Default is 120.
298304 # '\t' is counted as 1 character by default, and can be changed with the tab-width option
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ require (
1111 github.com/bombsimon/wsl/v3 v3.1.0
1212 github.com/daixiang0/gci v0.2.7
1313 github.com/denis-tingajkin/go-header v0.4.2
14+ github.com/esimonov/ifshort v0.0.0-20201228162320-ffc50097b7f7
1415 github.com/fatih/color v1.10.0
1516 github.com/go-critic/go-critic v0.5.2
1617 github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b
Original file line number Diff line number Diff line change @@ -270,6 +270,7 @@ type LintersSettings struct {
270270 Makezero MakezeroSettings
271271 Thelper ThelperSettings
272272 Forbidigo ForbidigoSettings
273+ Ifshort IfshortSettings
273274
274275 Custom map [string ]CustomLinterSettings
275276}
@@ -407,6 +408,11 @@ type ThelperSettings struct {
407408 } `mapstructure:"benchmark"`
408409}
409410
411+ type IfshortSettings struct {
412+ MaxDeclLines int `mapstructure:"max-decl-lines"`
413+ MaxDeclChars int `mapstructure:"max-decl-chars"`
414+ }
415+
410416type ForbidigoSettings struct {
411417 Forbid []string `mapstructure:"forbid"`
412418}
Original file line number Diff line number Diff line change 1+ package golinters
2+
3+ import (
4+ "github.com/esimonov/ifshort/pkg/analyzer"
5+ "golang.org/x/tools/go/analysis"
6+
7+ "github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+ )
9+
10+ func NewIfshort () * goanalysis.Linter {
11+ return goanalysis .NewLinter (
12+ "ifshort" ,
13+ "Checks that your code uses short syntax for if-statements whenever possible" ,
14+ []* analysis.Analyzer {analyzer .Analyzer },
15+ nil ,
16+ ).WithLoadMode (goanalysis .LoadModeSyntax )
17+ }
Original file line number Diff line number Diff line change @@ -342,6 +342,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
342342 linter .NewConfig (golinters .NewForbidigo ()).
343343 WithPresets (linter .PresetStyle ).
344344 WithURL ("https://github.com/ashanbrown/forbidigo" ),
345+ linter .NewConfig (golinters .NewIfshort ()).
346+ WithPresets (linter .PresetStyle ).
347+ WithURL ("https://github.com/esimonov/ifshort" ),
345348
346349 // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
347350 linter .NewConfig (golinters .NewNoLintLint ()).
Original file line number Diff line number Diff line change 1+ //args: -Eifshort
2+ package testdata
3+
4+ func DontUseShortSyntaxWhenPossible () {
5+ getValue := func () interface {} { return nil }
6+
7+ v := getValue () // ERROR "variable 'v' is only used in the if-statement .*"
8+ if v != nil {
9+ return
10+ }
11+ }
You can’t perform that action at this time.
0 commit comments