Skip to content

Add go-errorlint #1420

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

Merged
merged 2 commits into from
Oct 9, 2020
Merged
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
3 changes: 3 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ linters-settings:
# Choose whether or not to use the extra rules that are disabled
# by default
extra-rules: false
errorlint:
# Report non-wrapping error creation using fmt.Errorf
errorf: true

# The custom section can be used to define linter plugins to be loaded at runtime. See README doc
# for more info.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/nakabonne/nestif v0.3.0
github.com/nishanths/exhaustive v0.0.0-20200811152831-6cf413ae40e0
github.com/pkg/errors v0.9.1
github.com/polyfloyd/go-errorlint v0.0.0-20201006195004-351e25ade6e3
github.com/ryancurrah/gomodguard v1.1.0
github.com/ryanrolds/sqlclosecheck v0.3.0
github.com/securego/gosec/v2 v2.4.0
Expand Down
8 changes: 6 additions & 2 deletions go.sum

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

8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ type LintersSettings struct {
NoLintLint NoLintLintSettings
Exhaustive ExhaustiveSettings
Gofumpt GofumptSettings
ErrorLint ErrorLintSettings

Custom map[string]CustomLinterSettings
}
Expand Down Expand Up @@ -360,6 +361,10 @@ type GofumptSettings struct {
ExtraRules bool `mapstructure:"extra-rules"`
}

type ErrorLintSettings struct {
Errorf bool `mapstructure:"errorf"`
}

var defaultLintersSettings = LintersSettings{
Lll: LllSettings{
LineLength: 120,
Expand Down Expand Up @@ -416,6 +421,9 @@ var defaultLintersSettings = LintersSettings{
Gofumpt: GofumptSettings{
ExtraRules: false,
},
ErrorLint: ErrorLintSettings{
Errorf: true,
},
}

type CustomLinterSettings struct {
Expand Down
27 changes: 27 additions & 0 deletions pkg/golinters/errorlint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package golinters

import (
"github.com/polyfloyd/go-errorlint/errorlint"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewErrorLint(cfg *config.ErrorLintSettings) *goanalysis.Linter {
a := errorlint.NewAnalyzer()
cfgMap := map[string]map[string]interface{}{}
if cfg != nil {
cfgMap[a.Name] = map[string]interface{}{
"errorf": cfg.Errorf,
}
}
return goanalysis.NewLinter(
"errorlint",
"go-errorlint is a source code linter for Go software "+
"that can be used to find code that will cause problems"+
"with the error wrapping scheme introduced in Go 1.13.",
[]*analysis.Analyzer{a},
cfgMap,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var govetCfg *config.GovetSettings
var testpackageCfg *config.TestpackageSettings
var exhaustiveCfg *config.ExhaustiveSettings
var errorlintCfg *config.ErrorLintSettings
if m.cfg != nil {
govetCfg = &m.cfg.LintersSettings.Govet
testpackageCfg = &m.cfg.LintersSettings.Testpackage
exhaustiveCfg = &m.cfg.LintersSettings.Exhaustive
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
}
const megacheckName = "megacheck"
lcs := []*linter.Config{
Expand Down Expand Up @@ -315,6 +317,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/moricho/tparallel"),
linter.NewConfig(golinters.NewErrorLint(errorlintCfg)).
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/polyfloyd/go-errorlint"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
90 changes: 90 additions & 0 deletions test/testdata/errorlint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//args: -Eerrorlint
package testdata

import (
"errors"
"log"
)

var errFoo = errors.New("foo")

func doThing() error {
return errFoo
}

func compare() {
err := doThing()
if errors.Is(err, errFoo) {
log.Println("ErrFoo")
}
if err == nil {
log.Println("nil")
}
if err != nil {
log.Println("nil")
}
if nil == err {
log.Println("nil")
}
if nil != err {
log.Println("nil")
}
if err == errFoo { // ERROR "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error"
log.Println("errFoo")
}
if err != errFoo { // ERROR "comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error"
log.Println("not errFoo")
}
if errFoo == err { // ERROR "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error"
log.Println("errFoo")
}
if errFoo != err { // ERROR "comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error"
log.Println("not errFoo")
}
switch err { // ERROR "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors"
case errFoo:
log.Println("errFoo")
}
switch doThing() { // ERROR "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors"
case errFoo:
log.Println("errFoo")
}
}

type myError struct{}

func (*myError) Error() string {
return "foo"
}

func doAnotherThing() error {
return &myError{}
}

func typeCheck() {
err := doAnotherThing()
var me *myError
if errors.As(err, &me) {
log.Println("myError")
}
_, ok := err.(*myError) // ERROR "type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors"
if ok {
log.Println("myError")
}
switch err.(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
case *myError:
log.Println("myError")
}
switch doAnotherThing().(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
case *myError:
log.Println("myError")
}
switch t := err.(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
case *myError:
log.Println("myError", t)
}
switch t := doAnotherThing().(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
case *myError:
log.Println("myError", t)
}
}
26 changes: 26 additions & 0 deletions test/testdata/errorlint_errorf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//args: -Eerrorlint
//config: linters-settings.errorlint.errorf=true
package testdata

import (
"errors"
"fmt"
)

type customError struct{}

func (customError) Error() string {
return "oops"
}

func wraps() {
err := errors.New("oops")
fmt.Errorf("error: %w", err)
fmt.Errorf("error: %v", err) // ERROR "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
fmt.Errorf("%v %v", err, err) // ERROR "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
fmt.Errorf("error: %s", err.Error()) // ERROR "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
customError := customError{}
fmt.Errorf("error: %s", customError.Error()) // ERROR "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
strErr := "oops"
fmt.Errorf("%v", strErr)
}