Skip to content
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
22 changes: 19 additions & 3 deletions noinlineerr.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ func inlineErrorInspector(pass *analysis.Pass) func(n ast.Node) {
continue
}

if len(assignStmt.Lhs) != 1 {
// if there are more than 1 assignment then we can make a shadow conflict with other variables
// so don't do anything beside simple error message
// if there are more than 1 assignment
// or there are any variables with same name
// then we can make a shadow conflict with other variables
// so don't do anything beside simple error message
if len(assignStmt.Lhs) != 1 || shadowVarsExists(ident.Name, pass.TypesInfo.Scopes[ifStmt]) {
pass.Reportf(ident.Pos(), errMessage)
return
}
Expand All @@ -75,6 +77,7 @@ func inlineErrorInspector(pass *analysis.Pass) func(n ast.Node) {
// and we can autofix that

var buf bytes.Buffer

_ = printer.Fprint(&buf, pass.Fset, assignStmt)
assignText := buf.String()

Expand Down Expand Up @@ -116,3 +119,16 @@ func isError(obj types.Object) bool {

return types.AssignableTo(obj.Type(), errorType)
}

func shadowVarsExists(name string, scope *types.Scope) bool {
if scope == nil {
return false
}

parentScope := scope.Parent()
if parentScope == nil {
return false
}

return parentScope.Lookup(name) != nil
}
21 changes: 20 additions & 1 deletion testdata/src/a/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type MyAliasErr error

type MyCustomError struct {}
type MyCustomError struct{}

func (mc *MyCustomError) Error() string {
return "error"
Expand Down Expand Up @@ -84,3 +84,22 @@ func invalid() error {

return nil
}

func errShadow() error {
var err error
if err := doSomething(); err != nil { // want "avoid inline error handling using `if err := ...; err != nil`; use plain assignment `err := ...`"
return err
}
fmt.Println(err)

return nil
}

func naming() error {
if otherName := doSomething(); otherName != nil { // want "avoid inline error handling using `if err := ...; err != nil`; use plain assignment `err := ...`"
return otherName
}

return nil
}

21 changes: 20 additions & 1 deletion testdata/src/a/main.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type MyAliasErr error

type MyCustomError struct {}
type MyCustomError struct{}

func (mc *MyCustomError) Error() string {
return "error"
Expand Down Expand Up @@ -86,6 +86,25 @@ func invalid() error {
if err != nil { // want "avoid inline error handling using `if err := ...; err != nil`; use plain assignment `err := ...`"
return err
}

return nil
}

func errShadow() error {
var err error
if err := doSomething(); err != nil { // want "avoid inline error handling using `if err := ...; err != nil`; use plain assignment `err := ...`"
return err
}
fmt.Println(err)

return nil
}

func naming() error {
otherName := doSomething()
if otherName != nil { // want "avoid inline error handling using `if err := ...; err != nil`; use plain assignment `err := ...`"
return otherName
}

return nil
}