Skip to content

Commit

Permalink
unused: detect struct conversions involving generics
Browse files Browse the repository at this point in the history
(cherry picked from commit d694aad)
  • Loading branch information
dominikh committed Apr 24, 2022
1 parent 0ccdb5c commit 90804df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions unused/testdata/src/typeparams/typeparams.go
Expand Up @@ -89,3 +89,17 @@ func fn11[T ~struct{ Field int }]() { // unused
// don't crash because of the composite literal
_ = T{Field: 42}
}

type convertGeneric1 struct { // used
field int // used
}

type convertGeneric2 struct { // used
field int // used
}

var _ = convertGeneric1{}.field // mark field as used

func Fn12[T1 convertGeneric1, T2 convertGeneric2](a T1) { // used
_ = T2(a) // conversion marks T2.field as used
}
4 changes: 2 additions & 2 deletions unused/unused.go
Expand Up @@ -1662,8 +1662,8 @@ func (g *graph) instructions(fn *ir.Function) {
case *ir.ChangeType:
// conversion type handled generically

s1, ok1 := typeutil.Dereference(instr.Type()).Underlying().(*types.Struct)
s2, ok2 := typeutil.Dereference(instr.X.Type()).Underlying().(*types.Struct)
s1, ok1 := typeutil.CoreType(typeutil.Dereference(instr.Type())).(*types.Struct)
s2, ok2 := typeutil.CoreType(typeutil.Dereference(instr.X.Type())).(*types.Struct)
if ok1 && ok2 {
// Converting between two structs. The fields are
// relevant for the conversion, but only if the
Expand Down

0 comments on commit 90804df

Please sign in to comment.