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

Add errgroupcheck linter #4891

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ linters-settings:
# Default: false
report-no-exported: false

errgroupcheck:
# Check if any sync.errgroup.Group instance is missing a call to the Wait() func.
# Default: true
require-wait: true

errorlint:
# Check whether fmt.Errorf uses the %w verb for formatting errors.
# See the https://github.com/polyfloyd/go-errorlint for caveats.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/GaijinEntertainment/go-exhaustruct/v3 v3.3.0
github.com/OpenPeeDeeP/depguard/v2 v2.2.0
github.com/alecthomas/go-check-sumtype v0.1.4
github.com/alexbagnolini/errgroupcheck v0.1.2
github.com/alexkohler/nakedret/v2 v2.0.4
github.com/alexkohler/prealloc v1.0.0
github.com/alingse/asasalint v0.0.11
Expand Down
2 changes: 2 additions & 0 deletions go.sum

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

12 changes: 12 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@
"durationcheck",
"errcheck",
"errchkjson",
"errgroupcheck",
"errname",
"errorlint",
"execinquery",
Expand Down Expand Up @@ -865,6 +866,17 @@
}
}
},
"errgroupcheck": {
"type": "object",
"additionalProperties": false,
"properties": {
"require-wait": {
"description": "Check if any sync.errgroup.Group instance is missing a call to the Wait() func.",
"type": "boolean",
"default": true
}
}
},
"errorlint": {
"type": "object",
"additionalProperties": false,
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type LintersSettings struct {
DupWord DupWordSettings
Errcheck ErrcheckSettings
ErrChkJSON ErrChkJSONSettings
ErrGroupCheck ErrGroupCheckSettings
ErrorLint ErrorLintSettings
Exhaustive ExhaustiveSettings
Exhaustruct ExhaustructSettings
Expand Down Expand Up @@ -382,6 +383,10 @@ type ErrChkJSONSettings struct {
ReportNoExported bool `mapstructure:"report-no-exported"`
}

type ErrGroupCheckSettings struct {
RequireWait bool `mapstructure:"require-wait"`
}

type ErrorLintSettings struct {
Errorf bool `mapstructure:"errorf"`
ErrorfMulti bool `mapstructure:"errorf-multi"`
Expand Down
33 changes: 33 additions & 0 deletions pkg/golinters/errgroupcheck/errgroupcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package errgroupcheck

import (
"github.com/alexbagnolini/errgroupcheck"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goanalysis"
"golang.org/x/tools/go/analysis"
)

func New(cfg *config.ErrGroupCheckSettings) *goanalysis.Linter {
var setts = errgroupcheck.DefaultSettings()

if cfg != nil {
setts.RequireWait = cfg.RequireWait
}

cfgMap := map[string]map[string]any{}

analyzer := errgroupcheck.NewAnalyzer(setts)

if cfg != nil {
cfgMap[analyzer.Name] = map[string]any{
"require-wait": cfg.RequireWait,
}
}

return goanalysis.NewLinter(
analyzer.Name,
analyzer.Doc,
[]*analysis.Analyzer{analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
11 changes: 11 additions & 0 deletions pkg/golinters/errgroupcheck/errgroupcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package errgroupcheck

import (
"testing"

"github.com/golangci/golangci-lint/test/testshared/integration"
)

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
101 changes: 101 additions & 0 deletions pkg/golinters/errgroupcheck/testdata/errgroupcheck_wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//golangcitest:args -Eerrgroupcheck
//golangcitest:config_path testdata/errgroupcheck_wait.yml
package testdata

import (
"context"

"golang.org/x/sync/errgroup"
)

func ErrgroupWithWait() {
eg := errgroup.Group{}

eg.Go(func() error {
return nil
})

eg.Go(func() error {
return nil
})

_ = eg.Wait()
}

func ErrgroupMissingWait() {
eg := errgroup.Group{} // want "errgroup 'eg' does not have Wait called"

eg.Go(func() error {
return nil
})

eg.Go(func() error {
return nil
})
}

func ErrgroupContextWithWait() {
eg, _ := errgroup.WithContext(context.Background())

eg.Go(func() error {
return nil
})

eg.Go(func() error {
return nil
})

_ = eg.Wait()
}

func ErrgroupContextMissingWait() {
eg, _ := errgroup.WithContext(context.Background()) // want "errgroup 'eg' does not have Wait called"

eg.Go(func() error {
return nil
})

eg.Go(func() error {
return nil
})
}

func ErrgroupMultipleScopesWithWait() {
eg := errgroup.Group{}

eg.Go(func() error {
return nil
})

eg.Go(func() error {
eg2 := errgroup.Group{}

eg2.Go(func() error {
return nil
})

return eg2.Wait()
})

_ = eg.Wait()
}

func ErrgroupMultipleScopesMissingWait() {
eg := errgroup.Group{}

eg.Go(func() error {
return nil
})

eg.Go(func() error {
eg2 := errgroup.Group{} // want "errgroup 'eg2' does not have Wait called"

eg2.Go(func() error {
return nil
})

return nil
})

_ = eg.Wait()
}
3 changes: 3 additions & 0 deletions pkg/golinters/errgroupcheck/testdata/errgroupcheck_wait.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
errgroupcheck:
require-wait: true
5 changes: 5 additions & 0 deletions pkg/golinters/errgroupcheck/testdata/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module wait

go 1.19

require golang.org/x/sync v0.7.0
2 changes: 2 additions & 0 deletions pkg/golinters/errgroupcheck/testdata/go.sum

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

7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/golangci/golangci-lint/pkg/golinters/err113"
"github.com/golangci/golangci-lint/pkg/golinters/errcheck"
"github.com/golangci/golangci-lint/pkg/golinters/errchkjson"
"github.com/golangci/golangci-lint/pkg/golinters/errgroupcheck"
"github.com/golangci/golangci-lint/pkg/golinters/errname"
"github.com/golangci/golangci-lint/pkg/golinters/errorlint"
"github.com/golangci/golangci-lint/pkg/golinters/execinquery"
Expand Down Expand Up @@ -236,6 +237,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithLoadForGoAnalysis().
WithURL("https://github.com/breml/errchkjson"),

linter.NewConfig(errgroupcheck.New(&cfg.LintersSettings.ErrGroupCheck)).
WithSince("1.60.0").
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/alexbagnolini/errgroupcheck"),

linter.NewConfig(errname.New()).
WithSince("v1.42.0").
WithPresets(linter.PresetStyle).
Expand Down