Skip to content

Commit da2eea5

Browse files
T-Shvoloshina
authored andcommitted
feat: add linter gocheckerrbeforeuse
Signed-off-by: voloshina <[email protected]>
1 parent 2d4621c commit da2eea5

File tree

11 files changed

+94
-0
lines changed

11 files changed

+94
-0
lines changed

.golangci.next.reference.yml

Lines changed: 8 additions & 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
@@ -163,6 +164,7 @@ linters:
163164
- funlen
164165
- ginkgolinter
165166
- gocheckcompilerdirectives
167+
- gocheckerrbeforeuse
166168
- gochecknoglobals
167169
- gochecknoinits
168170
- gochecksumtype
@@ -657,6 +659,12 @@ linters:
657659
# Default: false
658660
force-assertion-description: true
659661

662+
gocheckerrbeforeuse:
663+
# Maximum allowable distance between receiving an error and handling it.
664+
# Value must always be greater than or equal to 1.
665+
# Default: 1
666+
max-allowed-distance: 1
667+
660668
gochecksumtype:
661669
# Presence of `default` case in switch statements satisfies exhaustiveness, if all members are not listed.
662670
# Default: true

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 v1.0.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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
gocheckerrbeforeuse:
6+
max-allowed-distance: 2
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//golangcitest:args -Egocheckerrbeforeuse
2+
//golangcitest:config_path testdata/custom.yml
3+
package testdata
4+
5+
func returns2Values() (int, error) {
6+
return 0, nil
7+
}
8+
9+
func PositiveWithCustomDistance() {
10+
i, err := returns2Values()
11+
12+
print(i)
13+
14+
if err != nil {
15+
return
16+
}
17+
}

0 commit comments

Comments
 (0)