Skip to content

Commit b62e05a

Browse files
author
voloshina
committed
feat: add linter gocheckerrbeforeuse
1 parent 2d4621c commit b62e05a

File tree

9 files changed

+64
-0
lines changed

9 files changed

+64
-0
lines changed

.golangci.next.reference.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ linters:
5252
- funlen
5353
- ginkgolinter
5454
- gocheckcompilerdirectives
55+
- gocheckerrbeforeuse
5556
- gochecknoglobals
5657
- gochecknoinits
5758
- gochecksumtype

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/BurntSushi/toml v1.5.0
1616
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
1717
github.com/OpenPeeDeeP/depguard/v2 v2.2.1
18+
github.com/T-Sh/go-check-err-before-use v0.1.0
1819
github.com/alecthomas/chroma/v2 v2.20.0
1920
github.com/alecthomas/go-check-sumtype v0.3.1
2021
github.com/alexkohler/nakedret/v2 v2.0.6

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@
788788
"funlen",
789789
"ginkgolinter",
790790
"gocheckcompilerdirectives",
791+
"gocheckerrbeforeuse",
791792
"gochecknoglobals",
792793
"gochecknoinits",
793794
"gochecksumtype",

pkg/config/linters_settings.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ type LintersSettings struct {
238238
Funlen FunlenSettings `mapstructure:"funlen"`
239239
GinkgoLinter GinkgoLinterSettings `mapstructure:"ginkgolinter"`
240240
Gocognit GocognitSettings `mapstructure:"gocognit"`
241+
GoCheckErrBeforeUse GoCheckErrBeforeUseSettings `mapstructure:"gocheckerrbeforeuse"`
241242
GoChecksumType GoChecksumTypeSettings `mapstructure:"gochecksumtype"`
242243
Goconst GoConstSettings `mapstructure:"goconst"`
243244
Gocritic GoCriticSettings `mapstructure:"gocritic"`
@@ -478,6 +479,10 @@ type GinkgoLinterSettings struct {
478479
ForceAssertionDescription bool `mapstructure:"force-assertion-description"`
479480
}
480481

482+
type GoCheckErrBeforeUseSettings struct {
483+
MaxAllowedDistance int `mapstructure:"max-allowed-distance"`
484+
}
485+
481486
type GoChecksumTypeSettings struct {
482487
DefaultSignifiesExhaustive bool `mapstructure:"default-signifies-exhaustive"`
483488
IncludeSharedInterfaces bool `mapstructure:"include-shared-interfaces"`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package gocheckerrbeforeuse
2+
3+
import (
4+
"github.com/T-Sh/go-check-err-before-use/pkg/analyzer"
5+
"github.com/golangci/golangci-lint/v2/pkg/config"
6+
"github.com/golangci/golangci-lint/v2/pkg/goanalysis"
7+
)
8+
9+
func New(settings *config.GoCheckErrBeforeUseSettings) *goanalysis.Linter {
10+
analyzerSettings := analyzer.Settings{Distance: settings.MaxAllowedDistance}
11+
12+
return goanalysis.NewLinterFromAnalyzer(analyzer.NewAnalyzer(analyzerSettings))
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package gocheckerrbeforeuse
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/v2/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//golangcitest:args -Egocheckerrbeforeuse
2+
package testdata
3+
4+
func returns2Values() (int, error) {
5+
return 0, nil
6+
}
7+
8+
func Negative() {
9+
i, err := returns2Values() // want "error must be checked right after receiving"
10+
11+
print(i)
12+
13+
if err != nil {
14+
return
15+
}
16+
}
17+
18+
func Positive() {
19+
i, err := returns2Values()
20+
if err != nil {
21+
return
22+
}
23+
24+
print(i)
25+
}

pkg/lint/lintersdb/builder_linter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/golangci/golangci-lint/v2/pkg/golinters/gci"
3737
"github.com/golangci/golangci-lint/v2/pkg/golinters/ginkgolinter"
3838
"github.com/golangci/golangci-lint/v2/pkg/golinters/gocheckcompilerdirectives"
39+
"github.com/golangci/golangci-lint/v2/pkg/golinters/gocheckerrbeforeuse"
3940
"github.com/golangci/golangci-lint/v2/pkg/golinters/gochecknoglobals"
4041
"github.com/golangci/golangci-lint/v2/pkg/golinters/gochecknoinits"
4142
"github.com/golangci/golangci-lint/v2/pkg/golinters/gochecksumtype"
@@ -714,5 +715,9 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
714715
WithSince("v1.26.0").
715716
WithAutoFix().
716717
WithURL("https://github.com/golangci/golangci-lint/tree/HEAD/pkg/golinters/nolintlint/internal"),
718+
719+
linter.NewConfig(gocheckerrbeforeuse.New(&cfg.Linters.Settings.GoCheckErrBeforeUse)).
720+
WithSince("v2.5.0").
721+
WithURL("https://github.com/T-Sh/go-check-err-before-use"),
717722
}, nil
718723
}

0 commit comments

Comments
 (0)