Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ linters:
- funlen
- ginkgolinter
- gocheckcompilerdirectives
- gocheckerrbeforeuse
- gochecknoglobals
- gochecknoinits
- gochecksumtype
Expand Down Expand Up @@ -163,6 +164,7 @@ linters:
- funlen
- ginkgolinter
- gocheckcompilerdirectives
- gocheckerrbeforeuse
- gochecknoglobals
- gochecknoinits
- gochecksumtype
Expand Down Expand Up @@ -657,6 +659,12 @@ linters:
# Default: false
force-assertion-description: true

gocheckerrbeforeuse:
# Maximum allowable distance between receiving an error and handling it.
# Value must always be greater than or equal to 1.
# Default: 1
max-allowed-distance: 1

gochecksumtype:
# Presence of `default` case in switch statements satisfies exhaustiveness, if all members are not listed.
# Default: true
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/BurntSushi/toml v1.5.0
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
github.com/OpenPeeDeeP/depguard/v2 v2.2.1
github.com/T-Sh/go-check-err-before-use v1.0.0
github.com/alecthomas/chroma/v2 v2.20.0
github.com/alecthomas/go-check-sumtype v0.3.1
github.com/alexkohler/nakedret/v2 v2.0.6
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.

1 change: 1 addition & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@
"funlen",
"ginkgolinter",
"gocheckcompilerdirectives",
"gocheckerrbeforeuse",
"gochecknoglobals",
"gochecknoinits",
"gochecksumtype",
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 @@ -238,6 +238,7 @@ type LintersSettings struct {
Funlen FunlenSettings `mapstructure:"funlen"`
GinkgoLinter GinkgoLinterSettings `mapstructure:"ginkgolinter"`
Gocognit GocognitSettings `mapstructure:"gocognit"`
GoCheckErrBeforeUse GoCheckErrBeforeUseSettings `mapstructure:"gocheckerrbeforeuse"`
GoChecksumType GoChecksumTypeSettings `mapstructure:"gochecksumtype"`
Goconst GoConstSettings `mapstructure:"goconst"`
Gocritic GoCriticSettings `mapstructure:"gocritic"`
Expand Down Expand Up @@ -478,6 +479,10 @@ type GinkgoLinterSettings struct {
ForceAssertionDescription bool `mapstructure:"force-assertion-description"`
}

type GoCheckErrBeforeUseSettings struct {
MaxAllowedDistance int `mapstructure:"max-allowed-distance"`
}

type GoChecksumTypeSettings struct {
DefaultSignifiesExhaustive bool `mapstructure:"default-signifies-exhaustive"`
IncludeSharedInterfaces bool `mapstructure:"include-shared-interfaces"`
Expand Down
13 changes: 13 additions & 0 deletions pkg/golinters/gocheckerrbeforeuse/gocheckerrbeforeuse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gocheckerrbeforeuse

import (
"github.com/T-Sh/go-check-err-before-use/pkg/analyzer"
"github.com/golangci/golangci-lint/v2/pkg/config"
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
)

func New(settings *config.GoCheckErrBeforeUseSettings) *goanalysis.Linter {
analyzerSettings := analyzer.Settings{Distance: settings.MaxAllowedDistance}

return goanalysis.NewLinterFromAnalyzer(analyzer.NewAnalyzer(analyzerSettings))
}
11 changes: 11 additions & 0 deletions pkg/golinters/gocheckerrbeforeuse/gocheckerrbeforeuse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gocheckerrbeforeuse

import (
"testing"

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

func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}
6 changes: 6 additions & 0 deletions pkg/golinters/gocheckerrbeforeuse/testdata/custom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "2"

linters:
settings:
gocheckerrbeforeuse:
max-allowed-distance: 2
25 changes: 25 additions & 0 deletions pkg/golinters/gocheckerrbeforeuse/testdata/gocheckerrbeforeuse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//golangcitest:args -Egocheckerrbeforeuse
package testdata

func returns2Values() (int, error) {
return 0, nil
}

func Negative() {
i, err := returns2Values() // want "error must be checked right after receiving"

print(i)

if err != nil {
return
}
}

func Positive() {
i, err := returns2Values()
if err != nil {
return
}

print(i)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//golangcitest:args -Egocheckerrbeforeuse
//golangcitest:config_path testdata/custom.yml
package testdata

func returns2Values() (int, error) {
return 0, nil
}

func PositiveWithCustomDistance() {
i, err := returns2Values()

print(i)

if err != nil {
return
}
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/golangci/golangci-lint/v2/pkg/golinters/gci"
"github.com/golangci/golangci-lint/v2/pkg/golinters/ginkgolinter"
"github.com/golangci/golangci-lint/v2/pkg/golinters/gocheckcompilerdirectives"
"github.com/golangci/golangci-lint/v2/pkg/golinters/gocheckerrbeforeuse"
"github.com/golangci/golangci-lint/v2/pkg/golinters/gochecknoglobals"
"github.com/golangci/golangci-lint/v2/pkg/golinters/gochecknoinits"
"github.com/golangci/golangci-lint/v2/pkg/golinters/gochecksumtype"
Expand Down Expand Up @@ -299,6 +300,10 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
WithSince("v1.51.0").
WithURL("https://github.com/leighmcculloch/gocheckcompilerdirectives"),

linter.NewConfig(gocheckerrbeforeuse.New(&cfg.Linters.Settings.GoCheckErrBeforeUse)).
WithSince("v2.5.0").
WithURL("https://github.com/T-Sh/go-check-err-before-use"),

linter.NewConfig(gochecknoglobals.New()).
WithSince("v1.12.0").
WithLoadForGoAnalysis().
Expand Down