Skip to content

Commit af1c502

Browse files
authored
Merge pull request #1098 from markshannon/python-2-print
Python: Don't report Python 2 print statements as having no effect.
2 parents eec59c2 + e9a4526 commit af1c502

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

python/ql/src/Statements/StatementNoEffect.ql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ predicate in_raises_test(Expr e) {
9595
)
9696
}
9797

98+
/** Holds if expression has the form of a Python 2 `print >> out, ...` statement */
99+
predicate python2_print(Expr e) {
100+
e.(BinaryExpr).getLeft().(Name).getId() = "print" and
101+
e.(BinaryExpr).getOp() instanceof RShift
102+
or
103+
python2_print(e.(Tuple).getElt(0))
104+
}
105+
98106
predicate no_effect(Expr e) {
99107
not e instanceof StrConst and
100108
not ((StrConst)e).isDocString() and
@@ -107,7 +115,8 @@ predicate no_effect(Expr e) {
107115
not maybe_side_effecting_attribute(sub)
108116
) and
109117
not in_notebook(e) and
110-
not in_raises_test(e)
118+
not in_raises_test(e) and
119+
not python2_print(e)
111120
}
112121

113122
from ExprStmt stmt

python/ql/test/query-tests/Statements/no_effect/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,7 @@ def do_action(action):
133133
stop()
134134
else:
135135
raise ValueError(action)
136+
137+
#Python 2 print
138+
print >> out, message
139+

0 commit comments

Comments
 (0)