Skip to content

Commit 652bd91

Browse files
authored
Add errname linter (#2129)
* Add errname linter * bump errname version
1 parent 959d8db commit 652bd91

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.15
44

55
require (
66
4d63.com/gochecknoglobals v0.0.0-20201008074935-acfc0b28355a
7+
github.com/Antonboom/errname v0.1.3
78
github.com/BurntSushi/toml v0.3.1
89
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
910
github.com/OpenPeeDeeP/depguard v1.0.1

go.sum

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/errname.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package golinters
2+
3+
import (
4+
"github.com/Antonboom/errname/pkg/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewErrName() *goanalysis.Linter {
11+
analyzers := []*analysis.Analyzer{
12+
analyzer.New(),
13+
}
14+
15+
return goanalysis.NewLinter(
16+
"errname",
17+
"Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`.",
18+
analyzers,
19+
nil,
20+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
21+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
501501
WithSince("v1.40.0").
502502
WithPresets(linter.PresetStyle).
503503
WithURL("https://github.com/ldez/tagliatelle"),
504+
linter.NewConfig(golinters.NewErrName()).
505+
WithPresets(linter.PresetStyle).
506+
WithLoadForGoAnalysis().
507+
WithURL("https://github.com/Antonboom/errname").
508+
WithSince("v1.42.0"),
504509

505510
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
506511
linter.NewConfig(golinters.NewNoLintLint()).

test/testdata/errname.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//args: -Eerrname
2+
package testdata
3+
4+
import (
5+
"errors"
6+
"fmt"
7+
)
8+
9+
var (
10+
EOF = errors.New("end of file")
11+
ErrEndOfFile = errors.New("end of file")
12+
errEndOfFile = errors.New("end of file")
13+
14+
EndOfFileError = errors.New("end of file") // ERROR "the variable name `EndOfFileError` should conform to the `ErrXxx` format"
15+
ErrorEndOfFile = errors.New("end of file") // ERROR "the variable name `ErrorEndOfFile` should conform to the `ErrXxx` format"
16+
EndOfFileErr = errors.New("end of file") // ERROR "the variable name `EndOfFileErr` should conform to the `ErrXxx` format"
17+
endOfFileError = errors.New("end of file") // ERROR "the variable name `endOfFileError` should conform to the `errXxx` format"
18+
errorEndOfFile = errors.New("end of file") // ERROR "the variable name `errorEndOfFile` should conform to the `errXxx` format"
19+
)
20+
21+
const maxSize = 256
22+
23+
var (
24+
ErrOutOfSize = fmt.Errorf("out of size (max %d)", maxSize)
25+
errOutOfSize = fmt.Errorf("out of size (max %d)", maxSize)
26+
27+
OutOfSizeError = fmt.Errorf("out of size (max %d)", maxSize) // ERROR "the variable name `OutOfSizeError` should conform to the `ErrXxx` format"
28+
outOfSizeError = fmt.Errorf("out of size (max %d)", maxSize) // ERROR "the variable name `outOfSizeError` should conform to the `errXxx` format"
29+
)
30+
31+
func errInsideFuncIsNotSentinel() error {
32+
var lastErr error
33+
return lastErr
34+
}
35+
36+
type NotErrorType struct{}
37+
38+
func (t NotErrorType) Set() {}
39+
func (t NotErrorType) Get() {}
40+
41+
type DNSConfigError struct{}
42+
43+
func (D DNSConfigError) Error() string { return "DNS config error" }
44+
45+
type someTypeWithoutPtr struct{} // ERROR "the type name `someTypeWithoutPtr` should conform to the `xxxError` format"
46+
func (s someTypeWithoutPtr) Error() string { return "someTypeWithoutPtr" }
47+
48+
type SomeTypeWithoutPtr struct{} // ERROR "the type name `SomeTypeWithoutPtr` should conform to the `XxxError` format"
49+
func (s SomeTypeWithoutPtr) Error() string { return "SomeTypeWithoutPtr" }
50+
51+
type someTypeWithPtr struct{} // ERROR "the type name `someTypeWithPtr` should conform to the `xxxError` format"
52+
func (s *someTypeWithPtr) Error() string { return "someTypeWithPtr" }
53+
54+
type SomeTypeWithPtr struct{} // ERROR "the type name `SomeTypeWithPtr` should conform to the `XxxError` format"
55+
func (s *SomeTypeWithPtr) Error() string { return "SomeTypeWithPtr" }

0 commit comments

Comments
 (0)