Skip to content

Commit

Permalink
fix: deadstore FP where use happens after first return statement (#72)
Browse files Browse the repository at this point in the history
* do not report results in first return stmt
* add test case
  • Loading branch information
coolhill authored Aug 19, 2022
1 parent be8b25d commit 00d8496
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
31 changes: 16 additions & 15 deletions amarna/rules/local_rules/DeadStoreRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def code_element_function(self, tree: Tree) -> None:
implicits_and_arguments.append(str(args.children[0]))

defines: Set[Token] = set()
in_return = []
for main_child in tree.children:
if main_child.data != "code_block":
continue
Expand All @@ -62,18 +63,18 @@ def code_element_function(self, tree: Tree) -> None:

# in a return statement, check which variables were not used
for subcode in child.children[0].find_data("code_element_return"):
tokens = [
str(tok) for tok in subcode.scan_values(lambda v: isinstance(v, Token))
]
for dead_store in sorted(defines):
if dead_store in tokens or dead_store in implicits_and_arguments:
continue
sarif = create_result_token(
self.fname,
self.RULE_NAME,
self.RULE_TEXT,
dead_store,
)
if sarif in self.results:
continue
self.results.append(sarif)
for tok in subcode.scan_values(lambda v: isinstance(v, Token)):
in_return.append(str(tok))

for dead_store in sorted(defines):
if dead_store in in_return or dead_store in implicits_and_arguments:
continue
sarif = create_result_token(
self.fname,
self.RULE_NAME,
self.RULE_TEXT,
dead_store,
)
if sarif in self.results:
continue
self.results.append(sarif)
9 changes: 9 additions & 0 deletions tests/dead_store3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
func are_equal(x, y) -> (eq):
let (a) = 44
if x == y:
return (FALSE)
end
if x == a:
return (TRUE)
end
end
1 change: 1 addition & 0 deletions tests/expected/dead_store3.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[unused-function] in dead_store3.cairo:1:1

0 comments on commit 00d8496

Please sign in to comment.