From feb83e66c6ee656a60f888698c64f976756c614c Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 2 Jul 2023 19:37:20 +1000 Subject: [PATCH] code_clean: add use_empty_void_arg cleanup operation Removes the use of (void) in C++ for functions with no arguments --- tools/utils_maintenance/code_clean.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tools/utils_maintenance/code_clean.py b/tools/utils_maintenance/code_clean.py index 13a90e31631..c744f35f47b 100755 --- a/tools/utils_maintenance/code_clean.py +++ b/tools/utils_maintenance/code_clean.py @@ -569,6 +569,32 @@ class edit_generators: return edits + class use_empty_void_arg(EditGenerator): + """ + Use ``()`` instead of ``(void)`` for C++ code. + + Replace: + function(void) {} + With: + function() {} + """ + @staticmethod + def edit_list_from_file(source: str, data: str, _shared_edit_data: Any) -> List[Edit]: + edits: List[Edit] = [] + + # The user might include C & C++, if they forget, it is better not to operate on C. + if source.lower().endswith((".h", ".c")): + return edits + + # `(void)` -> `()`. + for match in re.finditer(r"(\(void\))(\s*{)", data, flags=re.MULTILINE): + edits.append(Edit( + span=match.span(), + content="()" + match.group(2), + content_fail="(__ALWAYS_FAIL__) {", + )) + return edits + class unused_arg_as_comment(EditGenerator): """ Replace `UNUSED(argument)` in C++ code.