forked from go-critic/go-critic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checkers: rewrite deferUnlambda checker as a
StmtListVisitor
- fix assigned closure function false-positive issue go-critic#1401
- Loading branch information
Showing
4 changed files
with
792 additions
and
784 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package checkers | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"github.com/go-critic/go-critic/checkers/internal/astwalk" | ||
"github.com/go-critic/go-critic/linter" | ||
) | ||
|
||
func init() { | ||
var info linter.CheckerInfo | ||
info.Name = "deferUnlambda" | ||
info.Tags = []string{linter.StyleTag, linter.ExperimentalTag} | ||
info.Summary = "Detects deferred function literals that can be simplified" | ||
info.Before = "defer func() { f() }()" | ||
info.After = "defer f()" | ||
|
||
collection.AddChecker(&info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) { | ||
return astwalk.WalkerForStmtList(&deferUnlambdaChecker{ctx: ctx}), nil | ||
}) | ||
} | ||
|
||
type deferUnlambdaChecker struct { | ||
astwalk.WalkHandler | ||
ctx *linter.CheckerContext | ||
} | ||
|
||
func (c *deferUnlambdaChecker) VisitStmtList(x ast.Node, list []ast.Stmt) { | ||
for i, stmt := range list { | ||
switch v := stmt.(type) { | ||
case *ast.DeferStmt: | ||
innerFuncIdent, callExpr, needsUnlambda := c.deferStmtHasOnlyOneFuncCallWithNoReturns(v) | ||
if needsUnlambda && !c.isSilencedBuiltinFunc(innerFuncIdent) && !c.isIdentifierAssigned(innerFuncIdent, list[i:]) { | ||
c.ctx.Warn(stmt, "can rewrite as `defer %s`", callExpr) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *deferUnlambdaChecker) deferStmtHasOnlyOneFuncCallWithNoReturns(ds *ast.DeferStmt) (*ast.Ident, ast.Expr, bool) { | ||
if len(ds.Call.Args) != 0 { | ||
return nil, nil, false | ||
} | ||
|
||
switch v := ds.Call.Fun.(type) { | ||
case *ast.FuncLit: | ||
if len(v.Body.List) != 1 { | ||
return nil, nil, false | ||
} | ||
|
||
stmt := v.Body.List[0] | ||
|
||
exprStmt, ok := stmt.(*ast.ExprStmt) | ||
if !ok { | ||
return nil, nil, false | ||
} | ||
|
||
callExpr, ok := exprStmt.X.(*ast.CallExpr) | ||
callFuncIdent := identOf(callExpr.Fun) | ||
|
||
switch v := callExpr.Fun.(type) { | ||
case *ast.SelectorExpr: | ||
objIdent, ok := v.X.(*ast.Ident) | ||
if !ok || objIdent.Obj != nil { | ||
// check if the func is called on an object | ||
return nil, nil, false | ||
} | ||
} | ||
|
||
if callFuncIdent != nil { | ||
return callFuncIdent, callExpr, c.allCallArgsAreConst(callExpr.Args) | ||
} | ||
return nil, nil, false | ||
} | ||
|
||
return nil, nil, false | ||
} | ||
|
||
func (c *deferUnlambdaChecker) isIdentifierAssigned(target *ast.Ident, list []ast.Stmt) bool { | ||
for _, stmt := range list { | ||
assignStmt, ok := stmt.(*ast.AssignStmt) | ||
if !ok { | ||
continue | ||
} | ||
|
||
for _, lhs := range assignStmt.Lhs { | ||
ident, ok := lhs.(*ast.Ident) | ||
if !ok { | ||
continue | ||
} | ||
|
||
if ident.Name == target.Name { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (c *deferUnlambdaChecker) isSilencedBuiltinFunc(i *ast.Ident) bool { | ||
if !isBuiltin(i.Name) { | ||
return false | ||
} | ||
return i.Name == "panic" || i.Name == "recover" | ||
} | ||
|
||
func (c *deferUnlambdaChecker) allCallArgsAreConst(args []ast.Expr) bool { | ||
for _, a := range args { | ||
if !c.isConstExpr(a) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (c *deferUnlambdaChecker) isConstExpr(e ast.Expr) bool { | ||
return c.ctx.TypesInfo.Types[e].Value != nil | ||
} |
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
Oops, something went wrong.