Skip to content

Commit

Permalink
Add ifshort linter (#1587)
Browse files Browse the repository at this point in the history
  • Loading branch information
esimonov committed Jan 20, 2021
1 parent f049bfc commit 750309d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .golangci.example.yml
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/config/config.go
Expand Up @@ -270,6 +270,7 @@ type LintersSettings struct {
Makezero MakezeroSettings
Thelper ThelperSettings
Forbidigo ForbidigoSettings
Ifshort IfshortSettings
Predeclared PredeclaredSettings

Custom map[string]CustomLinterSettings
Expand Down Expand Up @@ -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"`
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/golinters/ifshort.go
@@ -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)
}
3 changes: 3 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -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"),
Expand Down
11 changes: 11 additions & 0 deletions test/testdata/ifshort.go
@@ -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
}
}

0 comments on commit 750309d

Please sign in to comment.