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

// confirm type is error
// confirm type is error and it is used in condition
obj := pass.TypesInfo.ObjectOf(ident)
if !isError(obj) || ident.Name == "_" {
if !isError(obj) || ident.Name == "_" || !errorUsedInCondition(ifStmt.Cond, ident.Name) {
continue
}

Expand Down Expand Up @@ -132,3 +132,23 @@ func shadowVarsExists(name string, scope *types.Scope) bool {

return parentScope.Lookup(name) != nil
}

func errorUsedInCondition(cond ast.Expr, errIdentName string) bool {
used := false

ast.Inspect(cond, func(n ast.Node) bool {
ident, ok := n.(*ast.Ident)
if !ok {
return true
}

if ident.Name == errIdentName {
used = true
return false
}

return true
})

return used
}
5 changes: 5 additions & 0 deletions testdata/src/a/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func valid() error {
if ok, _ := strconv.ParseBool("1"); ok {
fmt.Println("ok")
}

var myErr MyAliasErr
if err, ok := myErr.(error); ok {
fmt.Println("ok", err)
}

return nil
}
Expand Down
5 changes: 5 additions & 0 deletions testdata/src/a/main.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func valid() error {
fmt.Println("ok")
}

var myErr MyAliasErr
if err, ok := myErr.(error); ok {
fmt.Println("ok", err)
}

return nil
}

Expand Down