Skip to content

Commit

Permalink
Merge pull request #7 from maratori/underscore
Browse files Browse the repository at this point in the history
Ignore underscore names
  • Loading branch information
firefart committed Jun 11, 2022
2 parents 853036d + f79863a commit e96965e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 4 additions & 0 deletions analyzer/analyzer.go
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
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
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 }

0 comments on commit e96965e

Please sign in to comment.