|
| 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 | +//go:build go1.18 |
| 6 | +// +build go1.18 |
| 7 | + |
| 8 | +package infertypeargs |
| 9 | + |
| 10 | +import ( |
| 11 | + "go/ast" |
| 12 | + "go/token" |
| 13 | + "go/types" |
| 14 | + |
| 15 | + "golang.org/x/tools/go/analysis" |
| 16 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 17 | + "golang.org/x/tools/go/ast/inspector" |
| 18 | + "golang.org/x/tools/internal/typeparams" |
| 19 | +) |
| 20 | + |
| 21 | +func run(pass *analysis.Pass) (interface{}, error) { |
| 22 | + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 23 | + |
| 24 | + nodeFilter := []ast.Node{ |
| 25 | + (*ast.CallExpr)(nil), |
| 26 | + } |
| 27 | + |
| 28 | + inspect.Preorder(nodeFilter, func(node ast.Node) { |
| 29 | + call := node.(*ast.CallExpr) |
| 30 | + ident, ix := instanceData(call) |
| 31 | + if ix == nil || len(ix.Indices) == 0 { |
| 32 | + return // no explicit args, nothing to do |
| 33 | + } |
| 34 | + |
| 35 | + // Confirm that instantiation actually occurred at this ident. |
| 36 | + _, instance := typeparams.GetInstance(pass.TypesInfo, ident) |
| 37 | + if instance == nil { |
| 38 | + return // something went wrong, but fail open |
| 39 | + } |
| 40 | + |
| 41 | + // Start removing argument expressions from the right, and check if we can |
| 42 | + // still infer the call expression. |
| 43 | + required := len(ix.Indices) // number of type expressions that are required |
| 44 | + for i := len(ix.Indices) - 1; i >= 0; i-- { |
| 45 | + var fun ast.Expr |
| 46 | + if i == 0 { |
| 47 | + // No longer an index expression: just use the parameterized operand. |
| 48 | + fun = ix.X |
| 49 | + } else { |
| 50 | + fun = typeparams.PackIndexExpr(ix.X, ix.Lbrack, ix.Indices[:i], ix.Indices[i-1].End()) |
| 51 | + } |
| 52 | + newCall := &ast.CallExpr{ |
| 53 | + Fun: fun, |
| 54 | + Lparen: call.Lparen, |
| 55 | + Args: call.Args, |
| 56 | + Ellipsis: call.Ellipsis, |
| 57 | + Rparen: call.Rparen, |
| 58 | + } |
| 59 | + info := new(types.Info) |
| 60 | + typeparams.InitInstanceInfo(info) |
| 61 | + if err := types.CheckExpr(pass.Fset, pass.Pkg, call.Pos(), newCall, info); err != nil { |
| 62 | + // Most likely inference failed. |
| 63 | + break |
| 64 | + } |
| 65 | + _, newInstance := typeparams.GetInstance(info, ident) |
| 66 | + if !types.Identical(instance, newInstance) { |
| 67 | + // The inferred result type does not match the original result type, so |
| 68 | + // this simplification is not valid. |
| 69 | + break |
| 70 | + } |
| 71 | + required = i |
| 72 | + } |
| 73 | + if required < len(ix.Indices) { |
| 74 | + var start, end token.Pos |
| 75 | + if required == 0 { |
| 76 | + start, end = ix.Lbrack, ix.Rbrack+1 // erase the entire index |
| 77 | + } else { |
| 78 | + start = ix.Indices[required-1].End() |
| 79 | + end = ix.Rbrack |
| 80 | + } |
| 81 | + pass.Report(analysis.Diagnostic{ |
| 82 | + Pos: start, |
| 83 | + End: end, |
| 84 | + Message: "unnecessary type arguments", |
| 85 | + SuggestedFixes: []analysis.SuggestedFix{{ |
| 86 | + Message: "simplify type arguments", |
| 87 | + TextEdits: []analysis.TextEdit{{ |
| 88 | + Pos: start, |
| 89 | + End: end, |
| 90 | + }}, |
| 91 | + }}, |
| 92 | + }) |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + return nil, nil |
| 97 | +} |
| 98 | + |
| 99 | +// instanceData returns the instantiated identifier and index data. |
| 100 | +func instanceData(call *ast.CallExpr) (*ast.Ident, *typeparams.IndexExprData) { |
| 101 | + ix := typeparams.GetIndexExprData(call.Fun) |
| 102 | + if ix == nil { |
| 103 | + return nil, nil |
| 104 | + } |
| 105 | + var id *ast.Ident |
| 106 | + switch x := ix.X.(type) { |
| 107 | + case *ast.SelectorExpr: |
| 108 | + id = x.Sel |
| 109 | + case *ast.Ident: |
| 110 | + id = x |
| 111 | + default: |
| 112 | + return nil, nil |
| 113 | + } |
| 114 | + return id, ix |
| 115 | +} |
0 commit comments