Skip to content

Commit 862b892

Browse files
committed
Python: Disable "usused default" logic
Adds a new boolean parameter `is_unused_default` that indicates whether the given result is one where a parameter to a special method has a default value (which will never be used when invoked in the normal way). These results are somewhat less useful (because the special method _might_ be invoked directly, in which case the default value would still be relevant), but it seemed like a shame to simply remove the code, so instead I opted to disable it in this way.
1 parent 24b2eb2 commit 862b892

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

python/ql/src/Functions/SignatureSpecialMethods.ql

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,51 +51,71 @@ int argument_count(string name) {
5151
}
5252

5353
predicate incorrect_special_method_defn(
54-
Function func, string message, boolean show_counts, string name
54+
Function func, string message, boolean show_counts, string name, boolean is_unused_default
5555
) {
5656
exists(int required | required = argument_count(name) |
5757
/* actual_non_default <= actual */
5858
if required > func.getMaxPositionalArguments()
59-
then message = "Too few parameters" and show_counts = true
59+
then message = "Too few parameters" and show_counts = true and is_unused_default = false
6060
else
6161
if required < func.getMinPositionalArguments()
62-
then message = "Too many parameters" and show_counts = true
62+
then message = "Too many parameters" and show_counts = true and is_unused_default = false
6363
else (
6464
func.getMinPositionalArguments() < required and
6565
not func.hasVarArg() and
6666
message =
6767
(required - func.getMinPositionalArguments()) + " default values(s) will never be used" and
68-
show_counts = false
68+
show_counts = false and
69+
is_unused_default = true
6970
)
7071
)
7172
}
7273

73-
predicate incorrect_pow(Function func, string message, boolean show_counts) {
74+
predicate incorrect_pow(
75+
Function func, string message, boolean show_counts, boolean is_unused_default
76+
) {
7477
(
75-
func.getMaxPositionalArguments() < 2 and message = "Too few parameters" and show_counts = true
78+
func.getMaxPositionalArguments() < 2 and
79+
message = "Too few parameters" and
80+
show_counts = true and
81+
is_unused_default = false
7682
or
77-
func.getMinPositionalArguments() > 3 and message = "Too many parameters" and show_counts = true
83+
func.getMinPositionalArguments() > 3 and
84+
message = "Too many parameters" and
85+
show_counts = true and
86+
is_unused_default = false
7887
or
7988
func.getMinPositionalArguments() < 2 and
8089
message = (2 - func.getMinPositionalArguments()) + " default value(s) will never be used" and
81-
show_counts = false
90+
show_counts = false and
91+
is_unused_default = true
8292
or
8393
func.getMinPositionalArguments() = 3 and
8494
message = "Third parameter to __pow__ should have a default value" and
85-
show_counts = false
95+
show_counts = false and
96+
is_unused_default = false
8697
)
8798
}
8899

89-
predicate incorrect_get(Function func, string message, boolean show_counts) {
100+
predicate incorrect_get(
101+
Function func, string message, boolean show_counts, boolean is_unused_default
102+
) {
90103
(
91-
func.getMaxPositionalArguments() < 3 and message = "Too few parameters" and show_counts = true
104+
func.getMaxPositionalArguments() < 3 and
105+
message = "Too few parameters" and
106+
show_counts = true and
107+
is_unused_default = false
92108
or
93-
func.getMinPositionalArguments() > 3 and message = "Too many parameters" and show_counts = true
109+
func.getMinPositionalArguments() > 3 and
110+
message = "Too many parameters" and
111+
show_counts = true and
112+
is_unused_default = false
94113
or
95114
func.getMinPositionalArguments() < 2 and
96115
not func.hasVarArg() and
97116
message = (2 - func.getMinPositionalArguments()) + " default value(s) will never be used" and
98-
show_counts = false
117+
show_counts = false and
118+
is_unused_default = true
99119
)
100120
}
101121

@@ -117,16 +137,18 @@ string has_parameters(Function f) {
117137

118138
from
119139
PythonFunctionValue f, string message, string sizes, boolean show_counts, string name,
120-
ClassValue owner
140+
ClassValue owner, boolean show_unused_defaults
121141
where
122142
owner.declaredAttribute(name) = f and
123143
(
124-
incorrect_special_method_defn(f.getScope(), message, show_counts, name)
144+
incorrect_special_method_defn(f.getScope(), message, show_counts, name, show_unused_defaults)
145+
or
146+
incorrect_pow(f.getScope(), message, show_counts, show_unused_defaults) and name = "__pow__"
125147
or
126-
incorrect_pow(f.getScope(), message, show_counts) and name = "__pow__"
148+
incorrect_get(f.getScope(), message, show_counts, show_unused_defaults) and name = "__get__"
127149
or
128-
incorrect_get(f.getScope(), message, show_counts) and name = "__get__"
129150
) and
151+
show_unused_defaults = false and
130152
(
131153
show_counts = false and sizes = ""
132154
or

0 commit comments

Comments
 (0)