1
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
10
register(mapdeleteFix)
13
var mapdeleteFix = fix{
17
`Use delete(m, k) instead of m[k] = 0, false.
19
http://codereview.appspot.com/5272045
23
func mapdelete(f *ast.File) bool {
25
walk(f, func(n interface{}) {
26
stmt, ok := n.(*ast.Stmt)
30
as, ok := (*stmt).(*ast.AssignStmt)
31
if !ok || len(as.Lhs) != 1 || len(as.Rhs) != 2 {
34
ix, ok := as.Lhs[0].(*ast.IndexExpr)
38
if !isTopName(as.Rhs[1], "false") {
39
warn(as.Pos(), "two-element map assignment with non-false second value")
42
if !canDrop(as.Rhs[0]) {
43
warn(as.Pos(), "two-element map assignment with non-trivial first value")
46
*stmt = &ast.ExprStmt{
52
Args: []ast.Expr{ix.X, ix.Index},
60
// canDrop reports whether it is safe to drop the
61
// evaluation of n from the program.
62
// It is very conservative.
63
func canDrop(n ast.Expr) bool {
64
switch n := n.(type) {
65
case *ast.Ident, *ast.BasicLit:
69
case *ast.SelectorExpr:
71
case *ast.CompositeLit:
75
for _, e := range n.Elts {
82
// Dropping *x is questionable,
83
// but we have to be able to drop (*T)(nil).
85
case *ast.ArrayType, *ast.ChanType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.StructType: