-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
274 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package golinters | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Antonboom/nilnil/pkg/analyzer" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
func NewNilNil(cfg *config.NilNilSettings) *goanalysis.Linter { | ||
a := analyzer.New() | ||
|
||
cfgMap := make(map[string]map[string]interface{}) | ||
if cfg != nil && len(cfg.CheckedTypes) != 0 { | ||
cfgMap[a.Name] = map[string]interface{}{ | ||
"checked-types": strings.Join(cfg.CheckedTypes, ","), | ||
} | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
a.Name, | ||
a.Doc, | ||
[]*analysis.Analyzer{a}, | ||
cfgMap, | ||
). | ||
WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
//args: -Enilnil | ||
package testdata | ||
|
||
import ( | ||
"io" | ||
"unsafe" | ||
) | ||
|
||
type User struct{} | ||
|
||
func primitivePtr() (*int, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func structPtr() (*User, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func emptyStructPtr() (*struct{}, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func anonymousStructPtr() (*struct{ ID string }, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func chBi() (chan int, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func chIn() (chan<- int, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func chOut() (<-chan int, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func fun() (func(), error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func funWithArgsAndResults() (func(a, b, c int) (int, int), error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func iface() (interface{}, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func m1() (map[int]int, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func m2() (map[int]*User, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
type Storage struct{} | ||
|
||
func (s *Storage) GetUser() (*User, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func ifReturn() (*User, error) { | ||
var s Storage | ||
if _, err := s.GetUser(); err != nil { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
return new(User), nil | ||
} | ||
|
||
func forReturn() (*User, error) { | ||
for { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
} | ||
|
||
func multipleReturn() (*User, error) { | ||
var s Storage | ||
|
||
if _, err := s.GetUser(); err != nil { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
if _, err := s.GetUser(); err != nil { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
if _, err := s.GetUser(); err != nil { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
return new(User), nil | ||
} | ||
|
||
func nested() { | ||
_ = func() (*User, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
_, _ = func() (*User, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
}() | ||
} | ||
|
||
func deeplyNested() { | ||
_ = func() { | ||
_ = func() int { | ||
_ = func() { | ||
_ = func() (*User, error) { | ||
_ = func() {} | ||
_ = func() int { return 0 } | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
} | ||
return 0 | ||
} | ||
} | ||
} | ||
|
||
type ( | ||
StructPtrType *User | ||
PrimitivePtrType *int | ||
ChannelType chan int | ||
FuncType func(int) int | ||
Checker interface{ Check() } | ||
) | ||
|
||
func structPtrType() (StructPtrType, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func primitivePtrType() (PrimitivePtrType, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func channelType() (ChannelType, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func funcType() (FuncType, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func ifaceType() (Checker, error) { | ||
return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" | ||
} | ||
|
||
func withoutArgs() {} | ||
func withoutError1() *User { return nil } | ||
func withoutError2() (*User, *User) { return nil, nil } | ||
func withoutError3() (*User, *User, *User) { return nil, nil, nil } | ||
func withoutError4() (*User, *User, *User, *User) { return nil, nil, nil, nil } | ||
|
||
// Unsupported. | ||
|
||
func invalidOrder() (error, *User) { return nil, nil } | ||
func withError3rd() (*User, bool, error) { return nil, false, nil } | ||
func withError4th() (*User, *User, *User, error) { return nil, nil, nil, nil } | ||
func unsafePtr() (unsafe.Pointer, error) { return nil, nil } | ||
func uintPtr() (uintptr, error) { return 0, nil } | ||
func slice() ([]int, error) { return nil, nil } | ||
func ifaceExtPkg() (io.Closer, error) { return nil, nil } | ||
|
||
func implicitNil1() (*User, error) { | ||
err := (error)(nil) | ||
return nil, err | ||
} | ||
|
||
func implicitNil2() (*User, error) { | ||
err := io.EOF | ||
err = nil | ||
return nil, err | ||
} | ||
|
||
func implicitNil3() (*User, error) { | ||
return nil, wrap(nil) | ||
} | ||
func wrap(err error) error { return err } |