forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnusedVariableLinter.hack
More file actions
393 lines (350 loc) · 11.3 KB
/
Copy pathUnusedVariableLinter.hack
File metadata and controls
393 lines (350 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\HHAST;
use namespace HH\Lib\{C, Keyset, Str, Vec};
final class UnusedVariableLinter extends AutoFixingASTLinter {
const type TConfig = shape();
const type TNode = VariableExpression;
const type TContext = IFunctionishDeclaration;
<<__Override>>
public function getLintErrorForNode(
IFunctionishDeclaration $functionish,
VariableExpression $node,
): ?ASTLintError {
$parent = $functionish->getParentOfDescendant($node);
if ($parent is MemberSelectionExpression) {
// $this->x() : $this is used
// $this->x = $foo: all vars are used
// $x = $this->y: $this and $this->y are used, $x is checked separately
return null;
}
$var = $node->getExpression();
if (!$var is VariableToken) {
return null;
}
$name = $var->getText();
// $_ is the convention for marking a variable unused
// Use of $GLOBALS by definition may happen outside the scope of $functionish
if (Str\starts_with($name, '$_') || $name === '$GLOBALS') {
return null;
}
// If this variable is inside a lambda function, we should be looking in the
// lambda's header and body, not the enclosing function/method.
$lambda = $functionish->getClosestAncestorOfDescendantOfType<
LambdaExpression,
>($node);
$vars = $lambda is nonnull
? $this->classifyLambdaVariables($lambda)
: $this->classifyFunctionishVariables($functionish);
if (C\contains($vars['used'], $name)) {
return null;
}
// If a variable is not classified as used then we are currently
// evaluating an unused assignment.
return new ASTLintError(
$this,
'Variable is unused',
$node,
() ==> $this->getFixedNode($node, $functionish),
);
}
/**
* Get parameters that are passed by reference or marked inout which are always
* considered "used".
*
* This also looks for parameters to anonymous functions and lambda expressions
* that are passed by reference or marked inout and treats those as always
* used. This is done to avoid false positives at the cost of potential false
* negatives (unused variables that are not identified as unused).
*/
private function getAlwaysUsedVariables(
vec<ParameterDeclaration> $params,
Node $body,
): keyset<string> {
$anon_fns = $body->getDescendantsByType<AnonymousFunction>();
$lambdas = $body->getDescendantsByType<LambdaExpression>();
$anon_fn_params = Vec\map($anon_fns, $fn ==> {
$params = $fn->getParameters();
return $params is null
? vec[]
: $params->getDescendantsByType<ParameterDeclaration>();
})
|> Vec\flatten($$);
$lambda_params = Vec\map($lambdas, $lambda ==> {
$signature = $lambda->getSignature();
return $signature is LambdaSignature
? $signature->getDescendantsByType<ParameterDeclaration>()
: vec[];
})
|> Vec\flatten($$);
return Vec\filter(
Vec\concat($params, $anon_fn_params, $lambda_params),
$p ==> self::isByRefParam($p) || self::isInoutParam($p),
)
|> Keyset\map($$, $p ==> self::getParamName($p));
}
private static function isInoutParam(ParameterDeclaration $param): bool {
return $param->getCallConvention() is InoutToken;
}
private static function isByRefParam(ParameterDeclaration $param): bool {
$name = $param->getName();
return $name is DecoratedExpression &&
$name->getDecorator() is AmpersandToken;
}
private static function getParamName(ParameterDeclaration $param): string {
$name = $param->getName();
return $name is VariableToken
? $name->getText()
: (
($name as DecoratedExpression)->getExpression() as VariableToken
)->getText();
}
private function getFunctionishParts(
IFunctionishDeclaration $functionish,
): (vec<ParameterDeclaration>, CompoundStatement) {
if ($functionish is FunctionDeclaration) {
$body = $functionish->getBody();
$header = $functionish->getDeclarationHeader();
} else if ($functionish is MethodishDeclaration) {
$body = $functionish->getFunctionBody();
$header = $functionish->getFunctionDeclHeader();
} else {
invariant_violation("Couldn't find functionish for variable expression");
}
if ($body === null) {
invariant_violation(
'Abstract methods cannot contain variable expressions',
);
}
$params = vec[];
if ($header->getParameterList() is nonnull) {
foreach ($header->getParameterListx()->getChildrenOfItems() as $param) {
if ($param is ParameterDeclaration) {
$params[] = $param;
}
}
}
return tuple($params, $body as CompoundStatement);
}
private function getLambdaParts(
LambdaExpression $lambda,
): (vec<ParameterDeclaration>, Node) {
$sig = $lambda->getSignature();
if ($sig is LambdaSignature) {
$params = vec[];
if ($sig->getParameters() is nonnull) {
foreach ($sig->getParametersx()->getChildrenOfItems() as $param) {
if ($param is ParameterDeclaration) {
$params[] = $param;
}
}
}
} else {
$params = vec[
new ParameterDeclaration(
null,
null,
null,
null,
null,
new VariableExpression($sig as VariableToken),
null,
),
];
}
return tuple($params, $lambda->getBody());
}
/**
* Classify every variable expression in `$body` as an assignment or a use.
*
* An assignment is a variable expression that is one of the following:
*
* 1a. The left-hand side of a binary expression with an assignment operator
* that is not part of the index of a subscript expression.
* 2. The key or value of a foreach clause
* 3. The members of a list expression
*
* Any non-assignment of a variable is considered to be a use. This means that
* bugs or oversights in this method will cause false negatives.
*
* Examples of assignment:
* ```
* // 1.
* $a = 1;
* $b .= 'foo';
* $by_id[2] = 'baz';
*
* // 2.
* foreach ($items as $k => $v) {}
*
* // 3.
* list($a, $b) = $parts;
* ```
*/
<<__Memoize>>
private function classifyLambdaVariables(
LambdaExpression $lambda,
): shape('assigned' => keyset<string>, 'used' => keyset<string>) {
return $this->classifyVariables(...$this->getLambdaParts($lambda));
}
<<__Memoize>>
private function classifyFunctionishVariables(
IFunctionishDeclaration $fun,
): shape('assigned' => keyset<string>, 'used' => keyset<string>) {
return $this->classifyVariables(...$this->getFunctionishParts($fun));
}
private function classifyVariables(
vec<ParameterDeclaration> $params,
Node $body,
): shape('assigned' => keyset<string>, 'used' => keyset<string>) {
$ret = shape('assigned' => vec[], 'used' => vec[]);
foreach ($body->getDescendantsByType<VariableExpression>() as $var) {
if (
$this->isVariableAssignment($var, $body->getAncestorsOfDescendant($var))
) {
$ret['assigned'][] = $var;
} else {
$ret['used'][] = $var;
}
}
$ret['assigned'] = Keyset\map(
$ret['assigned'],
$v ==> ($v->getExpression() as VariableToken)->getText(),
);
$ret['used'] = Keyset\map(
$ret['used'],
$v ==> ($v->getExpression() as VariableToken)->getText(),
);
foreach ($this->getAlwaysUsedVariables($params, $body) as $name) {
$ret['used'][] = $name;
}
return $ret;
}
/**
* Return whether `$var` is being assigned in the scope of `$parents`.
*/
private function isVariableAssignment(
VariableExpression $var,
vec<Node> $parents,
): bool {
foreach (Vec\reverse($parents) as $parent) {
if ($parent === $var) {
continue;
}
// Too early to tell
if ($parent is ListItem<_> || $parent is NodeList<_>) {
continue;
}
// SubscriptExpression
// $a[$b]
// ^^ ^^
// | +-- index
// +-- receiver
//
// A variable in the index of a subscript expression is never an assignment
// A variable that is the receiver of a subscript expression may be an assignment or use; continue until a BinaryExpression is found.
//
// $a[$b] = '123'; // assignment of $a, use of $b
// f($a[$b]); // use of $a
if ($parent is SubscriptExpression) {
$index = $parent->getIndex();
if ($index !== null && $index->isAncestorOf($var)) {
return false;
} else {
continue;
}
}
if (
$parent is BinaryExpression &&
$this->isAssignmentExpression($parent) &&
$parent->getLeftOperand()->isAncestorOf($var)
) {
return true;
}
if (
$parent is ForeachStatement &&
(($parent->getValue() === $var) || ($parent->getKey() === $var))
) {
return true;
}
if (
$parent is ListExpression &&
(bool)$parent->getMembers()?->isAncestorOf($var)
) {
return true;
}
break;
}
return false;
}
/**
* These are all the return types of BinaryExpression::getOperator
* that contain "Equal" and are not comparison operators.
*/
private function isAssignmentExpression(BinaryExpression $expr): bool {
$op = $expr->getOperator();
return (
$op is AmpersandEqualToken ||
$op is BarEqualToken ||
$op is CaratEqualToken ||
$op is DotEqualToken ||
$op is EqualToken ||
$op is GreaterThanGreaterThanEqualToken ||
$op is LessThanLessThanEqualToken ||
$op is MinusEqualToken ||
$op is PercentEqualToken ||
$op is PlusEqualToken ||
$op is QuestionQuestionEqualToken ||
$op is SlashEqualToken ||
$op is StarEqualToken ||
$op is StarStarEqualToken
);
}
/**
* Return a fixed node if possible.
*
* Most unused assignments require user evaluation to fix. An unused assignment
* that has a typo should be corrected; others should be deleted. List
* expressions and foreach keys/values are exceptions where it is safe to
* suggest renaming the variable.
*/
public function getFixedNode(
VariableExpression $node,
IFunctionishDeclaration $functionish,
): VariableExpression {
$expr = $node->getExpression();
if (!$expr is VariableToken) {
return $node;
}
$parents = $functionish->getAncestorsOfDescendant($node);
$should_autofix = (
C\any($parents, $p ==> $p is ListExpression) ||
C\any(
$parents,
$p ==> $p is ForeachStatement &&
(($p->getValue() === $node) || ($p->getKey() === $node)),
)
);
if ($should_autofix) {
return $node->withExpression(
$expr->withText('$_'.Str\strip_prefix($expr->getText(), '$')),
);
} else {
return $node;
}
}
<<__Override>>
public function getTitleForFix(ASTLintError $err): string {
$expr = ($err->getBlameNode() as this::TNode);
$token = $expr->getExpression();
invariant($token is VariableToken, 'unhandled type');
$new_name = '$_'.Str\strip_prefix($token->getText(), '$');
return Str\format('Rename to `%s`', $new_name);
}
}