Skip to content
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

New linter errifinline #4385

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Expand Up @@ -2416,6 +2416,7 @@ linters:
- durationcheck
- errcheck
- errchkjson
- errifinline
- errname
- errorlint
- execinquery
Expand Down Expand Up @@ -2537,6 +2538,7 @@ linters:
- durationcheck
- errcheck
- errchkjson
- errifinline
- errname
- errorlint
- execinquery
Expand Down
7 changes: 5 additions & 2 deletions go.mod
@@ -1,6 +1,8 @@
module github.com/golangci/golangci-lint

go 1.21
go 1.21.0

toolchain go1.21.7

require (
4d63.com/gocheckcompilerdirectives v1.2.1
Expand All @@ -20,6 +22,7 @@ require (
github.com/alingse/asasalint v0.0.11
github.com/ashanbrown/forbidigo v1.6.0
github.com/ashanbrown/makezero v1.1.1
github.com/bastianccm/errifinline v0.0.1
github.com/bkielbasa/cyclop v1.2.1
github.com/blizzy78/varnamelen v0.8.0
github.com/bombsimon/wsl/v4 v4.2.1
Expand Down Expand Up @@ -123,7 +126,7 @@ require (
go-simpler.org/musttag v0.8.0
go-simpler.org/sloglint v0.4.0
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
golang.org/x/tools v0.17.0
golang.org/x/tools v0.18.0
gopkg.in/yaml.v3 v3.0.1
honnef.co/go/tools v0.4.6
mvdan.cc/gofumpt v0.6.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum

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

22 changes: 22 additions & 0 deletions pkg/golinters/errifinline.go
@@ -0,0 +1,22 @@
package golinters

import (
"github.com/bastianccm/errifinline"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewErrIfInline() *goanalysis.Linter {
a, err := errifinline.NewAnalyzer()
if err != nil {
linterLogger.Fatalf("errifinline: create analyzer: %v", err)
}

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -356,6 +356,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithLoadForGoAnalysis().
WithURL("https://github.com/breml/errchkjson"),

linter.NewConfig(golinters.NewErrIfInline()).
WithSince("1.57.0").
WithLoadForGoAnalysis().
WithURL("https://github.com/bastianccm/errifinline"),

linter.NewConfig(golinters.NewErrName()).
WithSince("v1.42.0").
WithPresets(linter.PresetStyle).
Expand Down
36 changes: 36 additions & 0 deletions test/testdata/errifinline.go
@@ -0,0 +1,36 @@
//golangcitest:args -Eerrifinline
package testdata

func error1() error {
return nil
}

func error2() (any, error) {
return nil, nil
}

func errIfInline() {
err := error1()
if err != nil { // want `inline err assignment in if initializer`
_ = err
}

if err := error1(); err != nil {
_ = err
}

_, err = error2()
if err != nil { // want `inline err assignment in if initializer`
_ = err
}

if _, err := error2(); err != nil {
_ = err
}

something, err := error2()
if err != nil {
_ = err
}
_ = something
}