Skip to content

Add ifshort linter #1587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
}
}