Skip to content

Commit 000f33f

Browse files
authored
Merge pull request #20765 from hvitved/rust/variable-if-let-guard
Rust: Handle variables introduced in if-let guards
2 parents b4b444d + c80301d commit 000f33f

File tree

5 files changed

+1851
-1787
lines changed

5 files changed

+1851
-1787
lines changed

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,12 @@ module Impl {
1515

1616
class BlockExprScope extends VariableScope, BlockExpr { }
1717

18-
abstract class MatchArmScope extends VariableScope {
19-
MatchArm arm;
20-
21-
bindingset[arm]
22-
MatchArmScope() { exists(arm) }
23-
24-
Pat getPat() { result = arm.getPat() }
18+
class MatchArmExprScope extends VariableScope {
19+
MatchArmExprScope() { this = any(MatchArm arm).getExpr() }
2520
}
2621

27-
class MatchArmExprScope extends MatchArmScope {
28-
MatchArmExprScope() { this = arm.getExpr() }
29-
}
30-
31-
class MatchArmGuardScope extends MatchArmScope {
32-
MatchArmGuardScope() { this = arm.getGuard() }
22+
class MatchArmGuardScope extends VariableScope {
23+
MatchArmGuardScope() { this = any(MatchArm arm).getGuard() }
3324
}
3425

3526
class ClosureBodyScope extends VariableScope {
@@ -41,7 +32,7 @@ module Impl {
4132
*
4233
* Such variables are only available in the body guarded by the condition.
4334
*/
44-
class ConditionScope extends VariableScope, Expr {
35+
class ConditionScope extends VariableScope {
4536
private AstNode parent;
4637
private AstNode body;
4738

@@ -57,6 +48,12 @@ module Impl {
5748
this = we.getCondition() and
5849
body = we.getLoopBody()
5950
)
51+
or
52+
parent =
53+
any(MatchArm ma |
54+
this = ma.getGuard() and
55+
body = ma.getExpr()
56+
)
6057
}
6158

6259
/** Gets the parent of this condition. */
@@ -417,11 +414,14 @@ module Impl {
417414
ord = getPreOrderNumbering(scope, scope)
418415
or
419416
exists(Pat pat | pat = getAVariablePatAncestor(v) |
420-
scope =
421-
any(MatchArmScope arm |
422-
arm.getPat() = pat and
423-
ord = getPreOrderNumbering(scope, arm)
424-
)
417+
exists(MatchArm arm |
418+
pat = arm.getPat() and
419+
ord = getPreOrderNumbering(scope, scope)
420+
|
421+
scope = arm.getGuard()
422+
or
423+
not arm.hasGuard() and scope = arm.getExpr()
424+
)
425425
or
426426
exists(LetStmt let |
427427
let.getPat() = pat and

0 commit comments

Comments
 (0)