| 1 | // Copyright 2021 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 | package unusedwrite |
| 6 | |
| 7 | import ( |
| 8 | _ "embed" |
| 9 | "go/types" |
| 10 | |
| 11 | "golang.org/x/tools/go/analysis" |
| 12 | "golang.org/x/tools/go/analysis/passes/buildssa" |
| 13 | "golang.org/x/tools/go/ssa" |
| 14 | "golang.org/x/tools/internal/analysis/analyzerutil" |
| 15 | "golang.org/x/tools/internal/typeparams" |
| 16 | ) |
| 17 | |
| 18 | //go:embed doc.go |
| 19 | var doc string |
| 20 | |
| 21 | // Analyzer reports instances of writes to struct fields and arrays |
| 22 | // that are never read. |
| 23 | var Analyzer = &analysis.Analyzer{ |
| 24 | Name: "unusedwrite", |
| 25 | Doc: analyzerutil.MustExtractDoc(doc, "unusedwrite"), |
| 26 | URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/unusedwrite", |
| 27 | Requires: []*analysis.Analyzer{buildssa.Analyzer}, |
| 28 | Run: run, |
| 29 | } |
| 30 | |
| 31 | func run(pass *analysis.Pass) (any, error) { |
| 32 | for _, pkg := range pass.Pkg.Imports() { |
| 33 | if pkg.Path() == "unsafe" { |
| 34 | // See golang/go#67684, or testdata/src/importsunsafe: the unusedwrite |
| 35 | // analyzer may have false positives when used with unsafe. |
| 36 | return nil, nil |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | ssainput := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) |
| 41 | for _, fn := range ssainput.SrcFuncs { |
| 42 | reports := checkStores(fn) |
| 43 | for _, store := range reports { |
| 44 | switch addr := store.Addr.(type) { |
| 45 | case *ssa.FieldAddr: |
| 46 | field := typeparams.CoreType(typeparams.MustDeref(addr.X.Type())).(*types.Struct).Field(addr.Field) |
| 47 | pass.Reportf(store.Pos(), |
| 48 | "unused write to field %s", field.Name()) |
| 49 | case *ssa.IndexAddr: |
| 50 | pass.Reportf(store.Pos(), |
| 51 | "unused write to array index %s", addr.Index) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | return nil, nil |
| 56 | } |
| 57 | |
| 58 | // checkStores returns *Stores in fn whose address is written to but never used. |
| 59 | func checkStores(fn *ssa.Function) []*ssa.Store { |
| 60 | var reports []*ssa.Store |
| 61 | // Visit each block. No need to visit fn.Recover. |
| 62 | for _, blk := range fn.Blocks { |
| 63 | for _, instr := range blk.Instrs { |
| 64 | // Identify writes. |
| 65 | if store, ok := instr.(*ssa.Store); ok { |
| 66 | // Consider field/index writes to an object whose elements are copied and not shared. |
| 67 | // MapUpdate is excluded since only the reference of the map is copied. |
| 68 | switch addr := store.Addr.(type) { |
| 69 | case *ssa.FieldAddr: |
| 70 | if isDeadStore(store, addr.X, addr) { |
| 71 | reports = append(reports, store) |
| 72 | } |
| 73 | case *ssa.IndexAddr: |
| 74 | if isDeadStore(store, addr.X, addr) { |
| 75 | reports = append(reports, store) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return reports |
| 82 | } |
| 83 | |
| 84 | // isDeadStore determines whether a field/index write to an object is dead. |
| 85 | // Argument "obj" is the object, and "addr" is the instruction fetching the field/index. |
| 86 | func isDeadStore(store *ssa.Store, obj ssa.Value, addr ssa.Instruction) bool { |
| 87 | // Consider only struct or array objects. |
| 88 | if !hasStructOrArrayType(obj) { |
| 89 | return false |
| 90 | } |
| 91 | // Check liveness: if the value is used later, then don't report the write. |
| 92 | for _, ref := range *obj.Referrers() { |
| 93 | if ref == store || ref == addr { |
| 94 | continue |
| 95 | } |
| 96 | switch ins := ref.(type) { |
| 97 | case ssa.CallInstruction: |
| 98 | return false |
| 99 | case *ssa.FieldAddr: |
| 100 | // Check whether the same field is used. |
| 101 | if ins.X == obj { |
| 102 | if faddr, ok := addr.(*ssa.FieldAddr); ok { |
| 103 | if faddr.Field == ins.Field { |
| 104 | return false |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | // Otherwise another field is used, and this usage doesn't count. |
| 109 | continue |
| 110 | case *ssa.IndexAddr: |
| 111 | if ins.X == obj { |
| 112 | return false |
| 113 | } |
| 114 | continue // Otherwise another object is used |
| 115 | case *ssa.Lookup: |
| 116 | if ins.X == obj { |
| 117 | return false |
| 118 | } |
| 119 | continue // Otherwise another object is used |
| 120 | case *ssa.Store: |
| 121 | if ins.Val == obj { |
| 122 | return false |
| 123 | } |
| 124 | continue // Otherwise other object is stored |
| 125 | default: // consider live if the object is used in any other instruction |
| 126 | return false |
| 127 | } |
| 128 | } |
| 129 | return true |
| 130 | } |
| 131 | |
| 132 | // isStructOrArray returns whether the underlying type is struct or array. |
| 133 | func isStructOrArray(tp types.Type) bool { |
| 134 | switch tp.Underlying().(type) { |
| 135 | case *types.Array: |
| 136 | return true |
| 137 | case *types.Struct: |
| 138 | return true |
| 139 | } |
| 140 | return false |
| 141 | } |
| 142 | |
| 143 | // hasStructOrArrayType returns whether a value is of struct or array type. |
| 144 | func hasStructOrArrayType(v ssa.Value) bool { |
| 145 | if instr, ok := v.(ssa.Instruction); ok { |
| 146 | if alloc, ok := instr.(*ssa.Alloc); ok { |
| 147 | // Check the element type of an allocated register (which always has pointer type) |
| 148 | // e.g., for |
| 149 | // func (t T) f() { ...} |
| 150 | // the receiver object is of type *T: |
| 151 | // t0 = local T (t) *T |
| 152 | if tp, ok := types.Unalias(alloc.Type()).(*types.Pointer); ok { |
| 153 | return isStructOrArray(tp.Elem()) |
| 154 | } |
| 155 | return false |
| 156 | } |
| 157 | } |
| 158 | return isStructOrArray(v.Type()) |
| 159 | } |