github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/vet/assign.go (about) 1 // Copyright 2013 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. 4 5 /* 6 This file contains the code to check for useless assignments. 7 */ 8 9 package main 10 11 import ( 12 "go/ast" 13 "go/token" 14 "reflect" 15 ) 16 17 // TODO: should also check for assignments to struct fields inside methods 18 // that are on T instead of *T. 19 20 // checkAssignStmt checks for assignments of the form "<expr> = <expr>". 21 // These are almost always useless, and even when they aren't they are usually a mistake. 22 func (f *File) checkAssignStmt(stmt *ast.AssignStmt) { 23 if !vet("assign") { 24 return 25 } 26 if stmt.Tok != token.ASSIGN { 27 return // ignore := 28 } 29 if len(stmt.Lhs) != len(stmt.Rhs) { 30 // If LHS and RHS have different cardinality, they can't be the same. 31 return 32 } 33 for i, lhs := range stmt.Lhs { 34 rhs := stmt.Rhs[i] 35 if reflect.TypeOf(lhs) != reflect.TypeOf(rhs) { 36 continue // short-circuit the heavy-weight gofmt check 37 } 38 le := f.gofmt(lhs) 39 re := f.gofmt(rhs) 40 if le == re { 41 f.Warnf(stmt.Pos(), "self-assignment of %s to %s", re, le) 42 } 43 } 44 }