-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUnusedArguments.py
38 lines (28 loc) · 1.3 KB
/
UnusedArguments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from tree_sitter import Node
from stacy_analyzer.visitor import Visitor
from stacy_analyzer.node_iterator import NodeIterator
class UnusedArguments(Visitor):
def __init__(self, print_output: bool = True):
super().__init__(print_output)
def visit_node(self, node: Node, i):
arguments = {}
if i > 1:
return
if node.grammar_name == "function_definition":
# parameters' name
for child in node.child(0).child(2).children:
if child.grammar_name == "function_parameter":
argument = child.child(1).text.decode("utf-8")
arguments[argument] = child
# function's body
fn_body = node.child(0).children[3:] # all but the fn name, signature and first (
for child in fn_body:
for gchild in NodeIterator(child):
if gchild.text.decode("utf-8") in arguments:
del arguments[gchild.text.decode("utf-8")]
for k, v in arguments.items():
self.MSG = f"'{k}' argument passed but not used."
self.HELP = None
self.FOOTNOTE = f"Consider removing '{k}' since its not used inside the function."
self.add_finding(v, v)
self.arguments = {}