go/analysis: unindent some pieces of code

All of these were quite heavily indented for no good reason; breaking or
returning early makes the code easier to read and follow.

Change-Id: Ic539517b07604d71495277b16f1b7eb60d2e3d3c
Reviewed-on: https://go-review.googlesource.com/c/149978
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Daniel Martí 2018-11-16 16:13:26 +00:00
parent 2ddaf7f79a
commit 70b12541d3
2 changed files with 79 additions and 74 deletions

View File

@ -93,7 +93,9 @@ func runFunc(pass *analysis.Pass, node ast.Node) {
// ctx, cancel = context.WithCancel(...) // ctx, cancel = context.WithCancel(...)
// var ctx, cancel = context.WithCancel(...) // var ctx, cancel = context.WithCancel(...)
// //
if isContextWithCancel(pass.TypesInfo, n) && isCall(stack[len(stack)-2]) { if !isContextWithCancel(pass.TypesInfo, n) || !isCall(stack[len(stack)-2]) {
return true
}
var id *ast.Ident // id of cancel var var id *ast.Ident // id of cancel var
stmt := stack[len(stack)-3] stmt := stack[len(stack)-3]
switch stmt := stmt.(type) { switch stmt := stmt.(type) {
@ -117,8 +119,6 @@ func runFunc(pass *analysis.Pass, node ast.Node) {
cancelvars[v] = stmt cancelvars[v] = stmt
} }
} }
}
return true return true
}) })
@ -179,9 +179,15 @@ func hasImport(pkg *types.Package, path string) bool {
// isContextWithCancel reports whether n is one of the qualified identifiers // isContextWithCancel reports whether n is one of the qualified identifiers
// context.With{Cancel,Timeout,Deadline}. // context.With{Cancel,Timeout,Deadline}.
func isContextWithCancel(info *types.Info, n ast.Node) bool { func isContextWithCancel(info *types.Info, n ast.Node) bool {
if sel, ok := n.(*ast.SelectorExpr); ok { sel, ok := n.(*ast.SelectorExpr)
if !ok {
return false
}
switch sel.Sel.Name { switch sel.Sel.Name {
case "WithCancel", "WithTimeout", "WithDeadline": case "WithCancel", "WithTimeout", "WithDeadline":
default:
return false
}
if x, ok := sel.X.(*ast.Ident); ok { if x, ok := sel.X.(*ast.Ident); ok {
if pkgname, ok := info.Uses[x].(*types.PkgName); ok { if pkgname, ok := info.Uses[x].(*types.PkgName); ok {
return pkgname.Imported().Path() == contextPackage return pkgname.Imported().Path() == contextPackage
@ -190,8 +196,6 @@ func isContextWithCancel(info *types.Info, n ast.Node) bool {
// Just check the local package name (heuristic). // Just check the local package name (heuristic).
return x.Name == "context" return x.Name == "context"
} }
}
}
return false return false
} }
@ -270,7 +274,9 @@ outer:
var search func(blocks []*cfg.Block) *ast.ReturnStmt var search func(blocks []*cfg.Block) *ast.ReturnStmt
search = func(blocks []*cfg.Block) *ast.ReturnStmt { search = func(blocks []*cfg.Block) *ast.ReturnStmt {
for _, b := range blocks { for _, b := range blocks {
if !seen[b] { if seen[b] {
continue
}
seen[b] = true seen[b] = true
// Prune the search if the block uses v. // Prune the search if the block uses v.
@ -294,7 +300,6 @@ outer:
return ret return ret
} }
} }
}
return nil return nil
} }
return search(defblock.Succs) return search(defblock.Succs)

View File

@ -62,8 +62,9 @@ func isSafeUintptr(info *types.Info, x ast.Expr) bool {
return isSafeUintptr(info, x.X) return isSafeUintptr(info, x.X)
case *ast.SelectorExpr: case *ast.SelectorExpr:
switch x.Sel.Name { if x.Sel.Name != "Data" {
case "Data": break
}
// reflect.SliceHeader and reflect.StringHeader are okay, // reflect.SliceHeader and reflect.StringHeader are okay,
// but only if they are pointing at a real slice or string. // but only if they are pointing at a real slice or string.
// It's not okay to do: // It's not okay to do:
@ -86,7 +87,6 @@ func isSafeUintptr(info *types.Info, x ast.Expr) bool {
} }
} }
} }
}
case *ast.CallExpr: case *ast.CallExpr:
switch len(x.Args) { switch len(x.Args) {