Skip to content

Commit a5ab0b9

Browse files
authored
Merge pull request #6871 from tausbn/python-fix-uses-of-implicit-this
Python: Fix uses of "implicit `this`"
2 parents 10d6803 + a611568 commit a5ab0b9

File tree

14 files changed

+71
-63
lines changed

14 files changed

+71
-63
lines changed

python/ql/lib/semmle/python/ApiGraphs.qll

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module API {
5555
/**
5656
* Gets a call to the function represented by this API component.
5757
*/
58-
DataFlow::CallCfgNode getACall() { result = getReturn().getAnImmediateUse() }
58+
DataFlow::CallCfgNode getACall() { result = this.getReturn().getAnImmediateUse() }
5959

6060
/**
6161
* Gets a node representing member `m` of this API component.
@@ -67,21 +67,21 @@ module API {
6767
*/
6868
bindingset[m]
6969
bindingset[result]
70-
Node getMember(string m) { result = getASuccessor(Label::member(m)) }
70+
Node getMember(string m) { result = this.getASuccessor(Label::member(m)) }
7171

7272
/**
7373
* Gets a node representing a member of this API component where the name of the member is
7474
* not known statically.
7575
*/
76-
Node getUnknownMember() { result = getASuccessor(Label::unknownMember()) }
76+
Node getUnknownMember() { result = this.getASuccessor(Label::unknownMember()) }
7777

7878
/**
7979
* Gets a node representing a member of this API component where the name of the member may
8080
* or may not be known statically.
8181
*/
8282
Node getAMember() {
83-
result = getASuccessor(Label::member(_)) or
84-
result = getUnknownMember()
83+
result = this.getASuccessor(Label::member(_)) or
84+
result = this.getUnknownMember()
8585
}
8686

8787
/**
@@ -90,23 +90,25 @@ module API {
9090
* This predicate may have multiple results when there are multiple invocations of this API component.
9191
* Consider using `getACall()` if there is a need to distinguish between individual calls.
9292
*/
93-
Node getReturn() { result = getASuccessor(Label::return()) }
93+
Node getReturn() { result = this.getASuccessor(Label::return()) }
9494

9595
/**
9696
* Gets a node representing a subclass of the class represented by this node.
9797
*/
98-
Node getASubclass() { result = getASuccessor(Label::subclass()) }
98+
Node getASubclass() { result = this.getASuccessor(Label::subclass()) }
9999

100100
/**
101101
* Gets a node representing the result from awaiting this node.
102102
*/
103-
Node getAwaited() { result = getASuccessor(Label::await()) }
103+
Node getAwaited() { result = this.getASuccessor(Label::await()) }
104104

105105
/**
106106
* Gets a string representation of the lexicographically least among all shortest access paths
107107
* from the root to this node.
108108
*/
109-
string getPath() { result = min(string p | p = getAPath(Impl::distanceFromRoot(this)) | p) }
109+
string getPath() {
110+
result = min(string p | p = this.getAPath(Impl::distanceFromRoot(this)) | p)
111+
}
110112

111113
/**
112114
* Gets a node such that there is an edge in the API graph between this node and the other
@@ -124,13 +126,13 @@ module API {
124126
* Gets a node such that there is an edge in the API graph between this node and the other
125127
* one.
126128
*/
127-
Node getAPredecessor() { result = getAPredecessor(_) }
129+
Node getAPredecessor() { result = this.getAPredecessor(_) }
128130

129131
/**
130132
* Gets a node such that there is an edge in the API graph between that other node and
131133
* this one.
132134
*/
133-
Node getASuccessor() { result = getASuccessor(_) }
135+
Node getASuccessor() { result = this.getASuccessor(_) }
134136

135137
/**
136138
* Gets the data-flow node that gives rise to this node, if any.
@@ -147,11 +149,11 @@ module API {
147149
predicate hasLocationInfo(
148150
string filepath, int startline, int startcolumn, int endline, int endcolumn
149151
) {
150-
getInducingNode().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
152+
this.getInducingNode().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
151153
or
152154
// For nodes that do not have a meaningful location, `path` is the empty string and all other
153155
// parameters are zero.
154-
not exists(getInducingNode()) and
156+
not exists(this.getInducingNode()) and
155157
filepath = "" and
156158
startline = 0 and
157159
startcolumn = 0 and
@@ -202,7 +204,7 @@ module API {
202204
or
203205
this = Impl::MkModuleImport(_) and type = "ModuleImport "
204206
|
205-
result = type + getPath()
207+
result = type + this.getPath()
206208
or
207209
not exists(this.getPath()) and result = type + "with no path"
208210
)

python/ql/lib/semmle/python/Exprs.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class Call extends Call_ {
240240
/** Gets the tuple (*) argument of this call, provided there is exactly one. */
241241
Expr getStarArg() {
242242
count(this.getStarargs()) < 2 and
243-
result = getStarargs()
243+
result = this.getStarargs()
244244
}
245245
}
246246

python/ql/lib/semmle/python/Files.qll

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ abstract class Container extends @container {
256256
* </table>
257257
*/
258258
string getBaseName() {
259-
result = getAbsolutePath().regexpCapture(".*/(([^/]*?)(?:\\.([^.]*))?)", 1)
259+
result = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(?:\\.([^.]*))?)", 1)
260260
}
261261

262262
/**
@@ -282,7 +282,9 @@ abstract class Container extends @container {
282282
* <tr><td>"/tmp/x.tar.gz"</td><td>"gz"</td></tr>
283283
* </table>
284284
*/
285-
string getExtension() { result = getAbsolutePath().regexpCapture(".*/([^/]*?)(\\.([^.]*))?", 3) }
285+
string getExtension() {
286+
result = this.getAbsolutePath().regexpCapture(".*/([^/]*?)(\\.([^.]*))?", 3)
287+
}
286288

287289
/**
288290
* Gets the stem of this container, that is, the prefix of its base name up to
@@ -301,7 +303,9 @@ abstract class Container extends @container {
301303
* <tr><td>"/tmp/x.tar.gz"</td><td>"x.tar"</td></tr>
302304
* </table>
303305
*/
304-
string getStem() { result = getAbsolutePath().regexpCapture(".*/([^/]*?)(?:\\.([^.]*))?", 1) }
306+
string getStem() {
307+
result = this.getAbsolutePath().regexpCapture(".*/([^/]*?)(?:\\.([^.]*))?", 1)
308+
}
305309

306310
File getFile(string baseName) {
307311
result = this.getAFile() and

python/ql/lib/semmle/python/Flow.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,9 @@ class ForNode extends ControlFlowNode {
851851

852852
/** Holds if this `for` statement causes iteration over `sequence` storing each step of the iteration in `target` */
853853
predicate iterates(ControlFlowNode target, ControlFlowNode sequence) {
854-
sequence = getSequence() and
855-
target = possibleTarget() and
856-
not target = unrolledSuffix().possibleTarget()
854+
sequence = this.getSequence() and
855+
target = this.possibleTarget() and
856+
not target = this.unrolledSuffix().possibleTarget()
857857
}
858858

859859
/** Gets the sequence node for this `for` statement. */

python/ql/lib/semmle/python/Import.qll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ImportExpr extends ImportExpr_ {
3131
// relative imports are no longer allowed in Python 3
3232
major_version() < 3 and
3333
// and can be explicitly turned off in later versions of Python 2
34-
not getEnclosingModule().hasFromFuture("absolute_import")
34+
not this.getEnclosingModule().hasFromFuture("absolute_import")
3535
}
3636

3737
/**
@@ -53,16 +53,16 @@ class ImportExpr extends ImportExpr_ {
5353
* the name of the topmost module that will be imported.
5454
*/
5555
private string relativeTopName() {
56-
getLevel() = -1 and
57-
result = basePackageName(1) + "." + this.getTopName() and
56+
this.getLevel() = -1 and
57+
result = this.basePackageName(1) + "." + this.getTopName() and
5858
valid_module_name(result)
5959
}
6060

6161
private string qualifiedTopName() {
6262
if this.getLevel() <= 0
6363
then result = this.getTopName()
6464
else (
65-
result = basePackageName(this.getLevel()) and
65+
result = this.basePackageName(this.getLevel()) and
6666
valid_module_name(result)
6767
)
6868
}
@@ -73,17 +73,17 @@ class ImportExpr extends ImportExpr_ {
7373
* which may not be the name of the module.
7474
*/
7575
string bottomModuleName() {
76-
result = relativeTopName() + this.remainderOfName()
76+
result = this.relativeTopName() + this.remainderOfName()
7777
or
78-
not exists(relativeTopName()) and
78+
not exists(this.relativeTopName()) and
7979
result = this.qualifiedTopName() + this.remainderOfName()
8080
}
8181

8282
/** Gets the name of topmost module or package being imported */
8383
string topModuleName() {
84-
result = relativeTopName()
84+
result = this.relativeTopName()
8585
or
86-
not exists(relativeTopName()) and
86+
not exists(this.relativeTopName()) and
8787
result = this.qualifiedTopName()
8888
}
8989

@@ -94,7 +94,7 @@ class ImportExpr extends ImportExpr_ {
9494
*/
9595
string getImportedModuleName() {
9696
exists(string bottomName | bottomName = this.bottomModuleName() |
97-
if this.isTop() then result = topModuleName() else result = bottomName
97+
if this.isTop() then result = this.topModuleName() else result = bottomName
9898
)
9999
}
100100

python/ql/lib/semmle/python/Module.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ class Module extends Module_, Scope, AstNode {
8686
/** Gets the package containing this module (or parent package if this is a package) */
8787
Module getPackage() {
8888
this.getName().matches("%.%") and
89-
result.getName() = getName().regexpReplaceAll("\\.[^.]*$", "")
89+
result.getName() = this.getName().regexpReplaceAll("\\.[^.]*$", "")
9090
}
9191

9292
/** Gets the name of the package containing this module */
9393
string getPackageName() {
9494
this.getName().matches("%.%") and
95-
result = getName().regexpReplaceAll("\\.[^.]*$", "")
95+
result = this.getName().regexpReplaceAll("\\.[^.]*$", "")
9696
}
9797

9898
/** Gets the metrics for this module */

python/ql/lib/semmle/python/RegexTreeView.qll

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ class RegExpParent extends TRegExpParent {
5555
abstract RegExpTerm getChild(int i);
5656

5757
/** Gets a child term . */
58-
RegExpTerm getAChild() { result = getChild(_) }
58+
RegExpTerm getAChild() { result = this.getChild(_) }
5959

6060
/** Gets the number of child terms. */
61-
int getNumChild() { result = count(getAChild()) }
61+
int getNumChild() { result = count(this.getAChild()) }
6262

6363
/** Gets the associated regex. */
6464
abstract Regex getRegex();
@@ -117,7 +117,7 @@ class RegExpTerm extends RegExpParent {
117117
RegExpTerm getRootTerm() {
118118
this.isRootTerm() and result = this
119119
or
120-
result = getParent().(RegExpTerm).getRootTerm()
120+
result = this.getParent().(RegExpTerm).getRootTerm()
121121
}
122122

123123
/**
@@ -196,7 +196,7 @@ class RegExpTerm extends RegExpParent {
196196

197197
/** Gets the regular expression term that is matched (textually) before this one, if any. */
198198
RegExpTerm getPredecessor() {
199-
exists(RegExpTerm parent | parent = getParent() |
199+
exists(RegExpTerm parent | parent = this.getParent() |
200200
result = parent.(RegExpSequence).previousElement(this)
201201
or
202202
not exists(parent.(RegExpSequence).previousElement(this)) and
@@ -207,7 +207,7 @@ class RegExpTerm extends RegExpParent {
207207

208208
/** Gets the regular expression term that is matched (textually) after this one, if any. */
209209
RegExpTerm getSuccessor() {
210-
exists(RegExpTerm parent | parent = getParent() |
210+
exists(RegExpTerm parent | parent = this.getParent() |
211211
result = parent.(RegExpSequence).nextElement(this)
212212
or
213213
not exists(parent.(RegExpSequence).nextElement(this)) and
@@ -358,7 +358,7 @@ class RegExpSequence extends RegExpTerm, TRegExpSequence {
358358
override RegExpTerm getChild(int i) { result = seqChild(re, start, end, i) }
359359

360360
/** Gets the element preceding `element` in this sequence. */
361-
RegExpTerm previousElement(RegExpTerm element) { element = nextElement(result) }
361+
RegExpTerm previousElement(RegExpTerm element) { element = this.nextElement(result) }
362362

363363
/** Gets the element following `element` in this sequence. */
364364
RegExpTerm nextElement(RegExpTerm element) {
@@ -461,8 +461,8 @@ class RegExpEscape extends RegExpNormalChar {
461461
// TODO: Find a way to include a formfeed character
462462
// this.getUnescaped() = "f" and result = " "
463463
// or
464-
isUnicode() and
465-
result = getUnicode()
464+
this.isUnicode() and
465+
result = this.getUnicode()
466466
}
467467

468468
predicate isIdentityEscape() { not this.getUnescaped() in ["n", "r", "t", "f"] }
@@ -479,7 +479,7 @@ class RegExpEscape extends RegExpNormalChar {
479479
/**
480480
* Holds if this is a unicode escape.
481481
*/
482-
private predicate isUnicode() { getText().prefix(2) = ["\\u", "\\U"] }
482+
private predicate isUnicode() { this.getText().prefix(2) = ["\\u", "\\U"] }
483483

484484
/**
485485
* Gets the unicode char for this escape.
@@ -569,13 +569,13 @@ class RegExpCharacterClass extends RegExpTerm, TRegExpCharacterClass {
569569

570570
predicate isUniversalClass() {
571571
// [^]
572-
isInverted() and not exists(getAChild())
572+
this.isInverted() and not exists(this.getAChild())
573573
or
574574
// [\w\W] and similar
575-
not isInverted() and
575+
not this.isInverted() and
576576
exists(string cce1, string cce2 |
577-
cce1 = getAChild().(RegExpCharacterClassEscape).getValue() and
578-
cce2 = getAChild().(RegExpCharacterClassEscape).getValue()
577+
cce1 = this.getAChild().(RegExpCharacterClassEscape).getValue() and
578+
cce2 = this.getAChild().(RegExpCharacterClassEscape).getValue()
579579
|
580580
cce1 != cce2 and cce1.toLowerCase() = cce2.toLowerCase()
581581
)

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowPrivate.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,11 @@ class DataFlowLambda extends DataFlowCallable, TLambda {
610610

611611
override string toString() { result = lambda.toString() }
612612

613-
override CallNode getACall() { result = getCallableValue().getACall() }
613+
override CallNode getACall() { result = this.getCallableValue().getACall() }
614614

615615
override Scope getScope() { result = lambda.getEvaluatingScope() }
616616

617-
override NameNode getParameter(int n) { result = getParameter(getCallableValue(), n) }
617+
override NameNode getParameter(int n) { result = getParameter(this.getCallableValue(), n) }
618618

619619
override string getName() { result = "Lambda callable" }
620620

python/ql/lib/semmle/python/dataflow/new/internal/LocalSources.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ class LocalSourceNode extends Node {
6262
/**
6363
* Gets a read of attribute `attrName` on this node.
6464
*/
65-
AttrRead getAnAttributeRead(string attrName) { result = getAnAttributeReference(attrName) }
65+
AttrRead getAnAttributeRead(string attrName) { result = this.getAnAttributeReference(attrName) }
6666

6767
/**
6868
* Gets a write of attribute `attrName` on this node.
6969
*/
70-
AttrWrite getAnAttributeWrite(string attrName) { result = getAnAttributeReference(attrName) }
70+
AttrWrite getAnAttributeWrite(string attrName) { result = this.getAnAttributeReference(attrName) }
7171

7272
/**
7373
* Gets a reference (read or write) of any attribute on this node.
@@ -81,12 +81,12 @@ class LocalSourceNode extends Node {
8181
/**
8282
* Gets a read of any attribute on this node.
8383
*/
84-
AttrRead getAnAttributeRead() { result = getAnAttributeReference() }
84+
AttrRead getAnAttributeRead() { result = this.getAnAttributeReference() }
8585

8686
/**
8787
* Gets a write of any attribute on this node.
8888
*/
89-
AttrWrite getAnAttributeWrite() { result = getAnAttributeReference() }
89+
AttrWrite getAnAttributeWrite() { result = this.getAnAttributeReference() }
9090

9191
/**
9292
* Gets a call to this node.

python/ql/lib/semmle/python/essa/Definitions.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ class ModuleVariable extends SsaSourceVariable {
225225
}
226226

227227
override ControlFlowNode getAnImplicitUse() {
228-
result = global_variable_callnode()
228+
result = this.global_variable_callnode()
229229
or
230-
result = global_variable_import()
230+
result = this.global_variable_import()
231231
or
232232
exists(ImportTimeScope scope | scope.entryEdge(result, _) |
233233
this = scope.getOuterVariable(_) or

0 commit comments

Comments
 (0)