Skip to content
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

Ignore underscore names #7

Merged
merged 1 commit into from
Jun 11, 2022
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
4 changes: 4 additions & 0 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
}

for _, n := range p.Names {
if n.Name == "_" {
continue
}

if allowErrorInDefer {
if ident, ok := p.Type.(*ast.Ident); ok {
if ident.Name == "error" && findDeferWithErrorAssignment(funcBody, n.Name) {
Expand Down
17 changes: 7 additions & 10 deletions testdata/src/allow-error-in-defer/allow_error_in_defer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ func twoReturnParams() (i int, err error) { // want `named return "i" with type
return
}

// TODO: enable test after https://github.com/firefart/nonamedreturns/pull/7
//func allUnderscoresExceptError() (_ int, err error) {
// defer func() {
// err = nil
// }()
// return
//}
func allUnderscoresExceptError() (_ int, err error) {
defer func() {
err = nil
}()
return
}

func customName() (myName error) {
defer func() {
Expand Down Expand Up @@ -70,10 +69,8 @@ func customType() (err myError) { // want `named return "err" with type "myError
return
}

// TODO: replace `i` with `_` after https://github.com/firefart/nonamedreturns/pull/7
func notTheLast() (err error, i int) { // want `named return "i" with type "int" found`
func notTheLast() (err error, _ int) {
defer func() {
i = 0
err = nil
}()
return
Expand Down
23 changes: 23 additions & 0 deletions testdata/src/default-config/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var e = func() (err error) { // want `named return "err" with type "error" found
return
}

var e2 = func() (_ error) {
return
}

func deferWithError() (err error) { // want `named return "err" with type "error" found`
defer func() {
err = nil // use flag to allow this
Expand All @@ -43,6 +47,10 @@ var (
err = nil
return
}

h2 = func() (_ error) {
return
}
)

// this should not match as the implementation does not need named parameters (see below)
Expand All @@ -56,11 +64,24 @@ func funcDefintionImpl2(arg1, arg2 interface{}) (num int, err error) { // want `
return 0, nil
}

func funcDefintionImpl3(arg1, arg2 interface{}) (num int, _ error) { // want `named return "num" with type "int" found`
return 0, nil
}

func funcDefintionImpl4(arg1, arg2 interface{}) (_ int, _ error) {
return 0, nil
}

var funcVar = func() (msg string) { // want `named return "msg" with type "string" found`
msg = "c"
return msg
}

var funcVar2 = func() (_ string) {
msg := "c"
return msg
}

func test() {
a := funcVar()
_ = a
Expand Down Expand Up @@ -98,3 +119,5 @@ func myLog(format string, args ...interface{}) {
type obj struct{}

func (o *obj) func1() (err error) { return nil } // want `named return "err" with type "error" found`

func (o *obj) func2() (_ error) { return nil }