This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathUnreachableCodeLinter.hack
66 lines (59 loc) · 1.74 KB
/
UnreachableCodeLinter.hack
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
/*
* 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, Str};
final class UnreachableCodeLinter extends ASTLinter {
const type TConfig = shape();
const type TNode = IStatement;
const type TContext = CompoundStatement;
<<__Override>>
public function getLintErrorForNode(
CompoundStatement $parent,
IStatement $stmt,
): ?ASTLintError {
// Evaluate all IStatement because we need to look for additional statements
// after return, throw, continue
if (
!(
$stmt is ThrowStatement ||
$stmt is ReturnStatement ||
$stmt is ContinueStatement
)
) {
return null;
}
$statements = $parent->getStatements();
invariant(
$statements is nonnull,
'parent list of statement cannot be null',
);
$children = $statements->getChildren();
// A statement causes unreachable code when it is not the last statement in
// a block.
//
// This occurs when it is not the last child of a CompoundStatement.
// In the case of conditional statements used without braces, statements
// are not a direct child of CompoundStatement but cannot cause unreachable code.
//
// For example:
// if ($cond) { $a = 5; return "last statement"; }
// if ($cond) return;
if (C\last($children) === $stmt || !C\contains($children, $stmt)) {
return null;
}
return new ASTLintError(
$this,
Str\format(
'This %s statement creates unreachable code',
$stmt->getKeyword()->getText(),
),
$stmt,
);
}
}