Skip to content

Commit f3353dc

Browse files
committed
Python: Ignore special methods with placeholder bodies
Instances of this include - Bodies that contain just a docstring (common in Zope interfaces) - Bodies that do nothing but raise an exception.
1 parent 862b892 commit f3353dc

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

python/ql/src/Functions/SignatureSpecialMethods.ql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ string has_parameters(Function f) {
135135
)
136136
}
137137

138+
/** Holds if `f` is likely to be a placeholder, and hence not interesting enough to report. */
139+
predicate isLikelyPlaceholderFunction(Function f) {
140+
// Body has only a single statement.
141+
f.getBody().getItem(0) = f.getBody().getLastItem() and
142+
(
143+
// Body is a string literal. This is a common pattern for Zope interfaces.
144+
f.getBody().getLastItem().(ExprStmt).getValue() instanceof StringLiteral
145+
or
146+
// Body just raises an exception.
147+
f.getBody().getLastItem() instanceof Raise
148+
)
149+
}
150+
138151
from
139152
PythonFunctionValue f, string message, string sizes, boolean show_counts, string name,
140153
ClassValue owner, boolean show_unused_defaults
@@ -148,6 +161,7 @@ where
148161
incorrect_get(f.getScope(), message, show_counts, show_unused_defaults) and name = "__get__"
149162
or
150163
) and
164+
not isLikelyPlaceholderFunction(f.getScope()) and
151165
show_unused_defaults = false and
152166
(
153167
show_counts = false and sizes = ""

0 commit comments

Comments
 (0)