Skip to content

Commit 750309d

Browse files
authored
Add ifshort linter (#1587)
1 parent f049bfc commit 750309d

File tree

7 files changed

+47
-0
lines changed

7 files changed

+47
-0
lines changed

Diff for: .golangci.example.yml

+6
Original file line numberDiff line numberDiff 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

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/bombsimon/wsl/v3 v3.1.0
1212
github.com/daixiang0/gci v0.2.8
1313
github.com/denis-tingajkin/go-header v0.4.2
14+
github.com/esimonov/ifshort v1.0.0
1415
github.com/fatih/color v1.10.0
1516
github.com/go-critic/go-critic v0.5.3
1617
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b

Diff for: go.sum

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

Diff for: pkg/config/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ type LintersSettings struct {
270270
Makezero MakezeroSettings
271271
Thelper ThelperSettings
272272
Forbidigo ForbidigoSettings
273+
Ifshort IfshortSettings
273274
Predeclared PredeclaredSettings
274275

275276
Custom map[string]CustomLinterSettings
@@ -408,6 +409,11 @@ type ThelperSettings struct {
408409
} `mapstructure:"benchmark"`
409410
}
410411

412+
type IfshortSettings struct {
413+
MaxDeclLines int `mapstructure:"max-decl-lines"`
414+
MaxDeclChars int `mapstructure:"max-decl-chars"`
415+
}
416+
411417
type ForbidigoSettings struct {
412418
Forbid []string `mapstructure:"forbid"`
413419
}

Diff for: pkg/golinters/ifshort.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

Diff for: pkg/lint/lintersdb/manager.go

+3
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
344344
linter.NewConfig(golinters.NewForbidigo()).
345345
WithPresets(linter.PresetStyle).
346346
WithURL("https://github.com/ashanbrown/forbidigo"),
347+
linter.NewConfig(golinters.NewIfshort()).
348+
WithPresets(linter.PresetStyle).
349+
WithURL("https://github.com/esimonov/ifshort"),
347350
linter.NewConfig(golinters.NewPredeclared(predeclaredCfg)).
348351
WithPresets(linter.PresetStyle).
349352
WithURL("https://github.com/nishanths/predeclared"),

Diff for: test/testdata/ifshort.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

0 commit comments

Comments
 (0)