Skip to content

Commit 7ab5663

Browse files
committed
Rust: Address PR feedback
1 parent 44b1ad5 commit 7ab5663

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,34 @@ module Impl {
7474
* pattern.
7575
*/
7676
private predicate variableDecl(AstNode definingNode, AstNode p, string name) {
77-
exists(SelfParam sp | sp = p |
78-
definingNode = sp.getName() and
79-
name = sp.getName().getText() and
80-
// exclude self parameters from functions without a body as these are
81-
// trait method declarations without implementations
82-
not exists(Function f | not f.hasBody() and f.getParamList().getSelfParam() = sp)
83-
)
77+
p =
78+
any(SelfParam sp |
79+
definingNode = sp.getName() and
80+
name = sp.getName().getText() and
81+
// exclude self parameters from functions without a body as these are
82+
// trait method declarations without implementations
83+
not exists(Function f | not f.hasBody() and f.getParamList().getSelfParam() = sp)
84+
)
8485
or
85-
exists(IdentPat pat | pat = p |
86-
(
87-
definingNode = getOutermostEnclosingOrPat(pat)
88-
or
89-
not exists(getOutermostEnclosingOrPat(pat)) and definingNode = pat.getName()
90-
) and
91-
name = pat.getName().getText() and
92-
// exclude for now anything starting with an uppercase character, which may be a reference to
93-
// an enum constant (e.g. `None`). This excludes static and constant variables (UPPERCASE),
94-
// which we don't appear to recognize yet anyway. This also assumes programmers follow the
95-
// naming guidelines, which they generally do, but they're not enforced.
96-
not name.charAt(0).isUppercase() and
97-
// exclude parameters from functions without a body as these are trait method declarations
98-
// without implementations
99-
not exists(Function f | not f.hasBody() and f.getParamList().getAParam().getPat() = pat) and
100-
// exclude parameters from function pointer types (e.g. `x` in `fn(x: i32) -> i32`)
101-
not exists(FnPtrType fp | fp.getParamList().getParam(_).getPat() = pat)
102-
)
86+
p =
87+
any(IdentPat pat |
88+
(
89+
definingNode = getOutermostEnclosingOrPat(pat)
90+
or
91+
not exists(getOutermostEnclosingOrPat(pat)) and definingNode = pat.getName()
92+
) and
93+
name = pat.getName().getText() and
94+
// exclude for now anything starting with an uppercase character, which may be a reference to
95+
// an enum constant (e.g. `None`). This excludes static and constant variables (UPPERCASE),
96+
// which we don't appear to recognize yet anyway. This also assumes programmers follow the
97+
// naming guidelines, which they generally do, but they're not enforced.
98+
not name.charAt(0).isUppercase() and
99+
// exclude parameters from functions without a body as these are trait method declarations
100+
// without implementations
101+
not exists(Function f | not f.hasBody() and f.getParamList().getAParam().getPat() = pat) and
102+
// exclude parameters from function pointer types (e.g. `x` in `fn(x: i32) -> i32`)
103+
not exists(FnPtrType fp | fp.getParamList().getParam(_).getPat() = pat)
104+
)
103105
}
104106

105107
/** A variable. */
@@ -125,7 +127,7 @@ module Impl {
125127
SelfParam getSelfParam() { variableDecl(definingNode, result, name) }
126128

127129
/**
128-
* Gets the pattern that declares this variable, if one exists.
130+
* Gets the pattern that declares this variable, if any.
129131
*
130132
* Normally, the pattern is unique, except when introduced in an or pattern:
131133
*
@@ -148,7 +150,7 @@ module Impl {
148150

149151
/** Gets the parameter that introduces this variable, if any. */
150152
ParamBase getParameter() {
151-
result = this.getSelfParam() or result = getAVariablePatAncestor(this).getParentNode()
153+
result = this.getSelfParam() or result.(Param).getPat() = getAVariablePatAncestor(this)
152154
}
153155

154156
/** Hold is this variable is mutable. */
@@ -217,8 +219,7 @@ module Impl {
217219
}
218220

219221
/**
220-
* Holds if parameter `p` introduces the variable `v` inside variable scope
221-
* `scope`.
222+
* Holds if a parameter declares the variable `v` inside variable scope `scope`.
222223
*/
223224
private predicate parameterDeclInScope(Variable v, VariableScope scope) {
224225
exists(Callable f |

0 commit comments

Comments
 (0)