Skip to content

Commit

Permalink
Use the data field to communicate subject
Browse files Browse the repository at this point in the history
Signed-off-by: Danila Fedorin <[email protected]>
  • Loading branch information
DanilaFe committed Jul 1, 2024
1 parent 777657a commit e0ce03b
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions tools/chplcheck/src/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,6 @@ def RemoveUnnecessaryDo(context: Context, result: BasicRuleResult):
fixit = Fixit.build(Edit.build(node.location(), new_text))
return fixit

def _RedundantParenthesesSubject(node: AstNode):
if isinstance(node, (DoWhile, While, Conditional)):
return node.condition()
elif isinstance(node, IndexableLoop):
return node.index()

@driver.basic_rule(set((Loop, Conditional)))
def ControlFlowParentheses(
context: Context, node: typing.Union[Loop, Conditional]
Expand All @@ -250,7 +244,11 @@ def ControlFlowParentheses(
Warn for unnecessary parentheses in conditional statements and loops.
"""

subject = _RedundantParenthesesSubject(node)
subject = None
if isinstance(node, (DoWhile, While, Conditional)):
subject = node.condition()
elif isinstance(node, IndexableLoop):
subject = node.index()

# No supported node to examine for redundant parentheses.
if subject is None:
Expand All @@ -267,12 +265,12 @@ def ControlFlowParentheses(

# Now, we should warn: there's a node in a conditional or
# if/else, it has parentheses at the top level, but it doesn't need them.
return False
return BasicRuleResult(node, data=subject)

@driver.fixit(ControlFlowParentheses)
def RemoveControlFlowParentheses(context: Context, result: BasicRuleResult):
# Since we're here, these should already be non-None.
subject = _RedundantParenthesesSubject(result.node)
subject = result.data
assert subject
paren_loc = subject.parenth_location()
assert paren_loc
Expand Down

0 comments on commit e0ce03b

Please sign in to comment.