| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package unusedparams |
| 6 |
|
| 7 |
import ( |
| 8 |
_ "embed" |
| 9 |
"fmt" |
| 10 |
"go/ast" |
| 11 |
"go/types" |
| 12 |
|
| 13 |
"golang.org/x/tools/go/analysis" |
| 14 |
"golang.org/x/tools/go/analysis/passes/inspect" |
| 15 |
"golang.org/x/tools/go/ast/edge" |
| 16 |
"golang.org/x/tools/go/ast/inspector" |
| 17 |
"golang.org/x/tools/gopls/internal/util/moreslices" |
| 18 |
"golang.org/x/tools/internal/analysis/analyzerutil" |
| 19 |
"golang.org/x/tools/internal/astutil" |
| 20 |
"golang.org/x/tools/internal/typesinternal" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
var doc string |
| 25 |
|
| 26 |
var Analyzer = &analysis.Analyzer{ |
| 27 |
Name: "unusedparams", |
| 28 |
Doc: analyzerutil.MustExtractDoc(doc, "unusedparams"), |
| 29 |
Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 30 |
Run: run, |
| 31 |
URL: "https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/unusedparams", |
| 32 |
} |
| 33 |
|
| 34 |
const FixCategory = "unusedparams" |
| 35 |
|
| 36 |
func run(pass *analysis.Pass) (any, error) { |
| 37 |
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
usesOutsideCall := make(map[types.Object][]*ast.Ident) |
| 73 |
unexportedIMethodNames := make(map[string]bool) |
| 74 |
{ |
| 75 |
callPosn := make(map[*ast.Ident]bool) |
| 76 |
filter := []ast.Node{ |
| 77 |
(*ast.CallExpr)(nil), |
| 78 |
(*ast.InterfaceType)(nil), |
| 79 |
} |
| 80 |
inspect.Preorder(filter, func(n ast.Node) { |
| 81 |
switch n := n.(type) { |
| 82 |
case *ast.CallExpr: |
| 83 |
id := typesinternal.UsedIdent(pass.TypesInfo, n.Fun) |
| 84 |
|
| 85 |
|
| 86 |
if id != nil && !id.IsExported() { |
| 87 |
switch pass.TypesInfo.Uses[id].(type) { |
| 88 |
case *types.Func, *types.Var: |
| 89 |
callPosn[id] = true |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
case *ast.InterfaceType: |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
t := pass.TypesInfo.TypeOf(n).(*types.Interface) |
| 99 |
for m := range t.ExplicitMethods() { |
| 100 |
if !m.Exported() && m.Name() != "_" { |
| 101 |
unexportedIMethodNames[m.Name()] = true |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
}) |
| 106 |
|
| 107 |
for id, obj := range pass.TypesInfo.Uses { |
| 108 |
if !callPosn[id] { |
| 109 |
|
| 110 |
usesOutsideCall[obj] = append(usesOutsideCall[obj], id) |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
usedVars := make(map[*types.Var]bool) |
| 117 |
for _, obj := range pass.TypesInfo.Uses { |
| 118 |
if v, ok := obj.(*types.Var); ok { |
| 119 |
if v.IsField() { |
| 120 |
continue |
| 121 |
} |
| 122 |
usedVars[v] = true |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
funcloop: |
| 128 |
for c := range inspect.Root().Preorder((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)) { |
| 129 |
var ( |
| 130 |
fn types.Object |
| 131 |
ftype *ast.FuncType |
| 132 |
body *ast.BlockStmt |
| 133 |
) |
| 134 |
switch n := c.Node().(type) { |
| 135 |
case *ast.FuncDecl: |
| 136 |
|
| 137 |
if n.Body == nil { |
| 138 |
continue |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if n.Name.IsExported() { |
| 145 |
continue |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
if n.Recv != nil && unexportedIMethodNames[n.Name.Name] { |
| 153 |
continue |
| 154 |
} |
| 155 |
|
| 156 |
fn = pass.TypesInfo.Defs[n.Name].(*types.Func) |
| 157 |
ftype, body = n.Type, n.Body |
| 158 |
|
| 159 |
case *ast.FuncLit: |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
switch parent := c.Parent().Node().(type) { |
| 164 |
case *ast.AssignStmt: |
| 165 |
|
| 166 |
|
| 167 |
if ek, idx := c.ParentEdge(); ek == edge.AssignStmt_Rhs { |
| 168 |
|
| 169 |
if id, ok := parent.Lhs[idx].(*ast.Ident); ok { |
| 170 |
fn = pass.TypesInfo.ObjectOf(id) |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
if pass.TypesInfo.Uses[id] != nil { |
| 175 |
usesOutsideCall[fn] = moreslices.Remove(usesOutsideCall[fn], id) |
| 176 |
} |
| 177 |
|
| 178 |
if fn == nil && id.Name == "_" { |
| 179 |
|
| 180 |
|
| 181 |
v := types.NewVar(id.Pos(), pass.Pkg, id.Name, pass.TypesInfo.TypeOf(n)) |
| 182 |
v.SetKind(types.LocalVar) |
| 183 |
fn = v |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
case *ast.ValueSpec: |
| 189 |
|
| 190 |
|
| 191 |
for i, val := range parent.Values { |
| 192 |
if val == n { |
| 193 |
v := pass.TypesInfo.Defs[parent.Names[i]] |
| 194 |
if !(v.Parent() == pass.Pkg.Scope() && v.Exported()) { |
| 195 |
fn = v |
| 196 |
} |
| 197 |
break |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
ftype, body = n.Type, n.Body |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if fn == nil || len(usesOutsideCall[fn]) > 0 { |
| 208 |
continue |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
if ftype.Params.NumFields() == 0 { |
| 213 |
continue |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
switch len(body.List) { |
| 226 |
case 0: |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
continue |
| 231 |
case 1: |
| 232 |
if stmt, ok := body.List[0].(*ast.ExprStmt); ok { |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
if call, ok := stmt.X.(*ast.CallExpr); ok { |
| 237 |
if fun, ok := call.Fun.(*ast.Ident); ok && fun.Name == "panic" { |
| 238 |
continue |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
if ast.IsGenerated(astutil.EnclosingFile(c)) { |
| 247 |
continue funcloop |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
for _, field := range ftype.Params.List { |
| 252 |
for _, id := range field.Names { |
| 253 |
if id.Name == "_" { |
| 254 |
continue |
| 255 |
} |
| 256 |
param := pass.TypesInfo.Defs[id].(*types.Var) |
| 257 |
if !usedVars[param] { |
| 258 |
start, end := field.Pos(), field.End() |
| 259 |
if len(field.Names) > 1 { |
| 260 |
start, end = id.Pos(), id.End() |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
pass.Report(analysis.Diagnostic{ |
| 267 |
Pos: start, |
| 268 |
End: end, |
| 269 |
Message: fmt.Sprintf("unused parameter: %s", id.Name), |
| 270 |
Category: FixCategory, |
| 271 |
SuggestedFixes: []analysis.SuggestedFix{ |
| 272 |
{ |
| 273 |
Message: `Rename parameter to "_"`, |
| 274 |
TextEdits: []analysis.TextEdit{{ |
| 275 |
Pos: id.Pos(), |
| 276 |
End: id.End(), |
| 277 |
NewText: []byte("_"), |
| 278 |
}}, |
| 279 |
}, |
| 280 |
{ |
| 281 |
Message: fmt.Sprintf("Remove unused parameter %q", id.Name), |
| 282 |
|
| 283 |
}, |
| 284 |
}, |
| 285 |
}) |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
return nil, nil |
| 291 |
} |