Skip to content

Commit

Permalink
code_clean: add unused_arg_as_comment edit generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ideasman42 committed Oct 11, 2022
1 parent d35b4ed commit 6bef895
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions utils_maintenance/code_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,45 @@ def edit_list_from_file(_source: str, data: str, _shared_edit_data: Any) -> List

return edits

class unused_arg_as_comment(EditGenerator):
"""
Replace `UNUSED(argument)` in C++ code.
Replace:
void function(int UNUSED(arg)) {...}
With:
void function(int /*arg*/) {...}
"""
@staticmethod
def edit_list_from_file(source: str, data: str, _shared_edit_data: Any) -> List[Edit]:
edits = []

# The user might exclude C++, if they forget, it is better not to operate on C.
if not source.lower().endswith((".h", ".c")):
return edits

# `UNUSED(arg)` -> `/*arg*/`.
for match in re.finditer(
r"\b(UNUSED)"
# # Opening parenthesis.
r"\("
# Capture the identifier as group 1.
r"([" + "".join(list(IDENTIFIER_CHARS)) + "]+)"
# # Capture any non-identifier characters as group 2.
# (e.g. `[3]`) which need to be added outside the comment.
r"([^\)]*)"
# Closing parenthesis of `UNUSED(..)`.
r"\)",
data,
):
edits.append(Edit(
span=match.span(),
content='/*%s*/%s' % (match.group(2), match.group(3)),
content_fail='__ALWAYS_FAIL__(%s%s)' % (match.group(2), match.group(3)),
))

return edits

class use_elem_macro(EditGenerator):
"""
Use the `ELEM` macro for more abbreviated expressions.
Expand Down

0 comments on commit 6bef895

Please sign in to comment.