https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/176927
>From 3d4bee4370cebb6ff483c547f32d352e7a7b96f8 Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Tue, 20 Jan 2026 17:04:43 +0300 Subject: [PATCH 1/2] [clang-tidy][NFC] Run ruff over python scripts --- clang-tools-extra/clang-tidy/add_new_check.py | 3 +-- clang-tools-extra/clang-tidy/rename_check.py | 5 ++--- clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py | 2 +- clang-tools-extra/clang-tidy/tool/run-clang-tidy.py | 8 ++++---- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index 80e1afe1a121c..dfa60ce7f3990 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -176,7 +176,7 @@ def write_implementation( } // namespace clang::tidy::%(namespace)s """ - % {"check_name": check_name_camel, "module": module, "namespace": namespace} + % {"check_name": check_name_camel, "namespace": namespace} ) @@ -578,7 +578,6 @@ def format_link_alias(doc_file: Tuple[str, str]) -> str: "check_name": check_name, "module": module, "check_file": check_file, - "target": target, "title": title, "autofix": autofix, } diff --git a/clang-tools-extra/clang-tidy/rename_check.py b/clang-tools-extra/clang-tidy/rename_check.py index b864bff814485..d5432eadcff6b 100755 --- a/clang-tools-extra/clang-tidy/rename_check.py +++ b/clang-tools-extra/clang-tidy/rename_check.py @@ -65,12 +65,12 @@ def deleteMatchingLines(fileName: str, pattern: str) -> bool: with io.open(fileName, "r", encoding="utf8") as f: lines = f.readlines() - not_matching_lines = [l for l in lines if not re.search(pattern, l)] + not_matching_lines = [line for line in lines if not re.search(pattern, line)] if len(not_matching_lines) == len(lines): return False print("Removing lines matching '%s' in '%s'..." % (pattern, fileName)) - print(" " + " ".join([l for l in lines if re.search(pattern, l)])) + print(" " + " ".join([line for line in lines if re.search(pattern, line)])) with io.open(fileName, "w", encoding="utf8") as f: f.writelines(not_matching_lines) @@ -307,7 +307,6 @@ def main() -> None: ) for filename in getListOfFiles(clang_tidy_path): - originalName = filename filename = fileRename( filename, old_module + "/" + old_name, new_module + "/" + new_name ) diff --git a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py index c090bdc1151df..07f2b911a03a2 100755 --- a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py +++ b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py @@ -427,7 +427,7 @@ def main(): print(f"Writing fixes to {args.export_fixes} ...") try: merge_replacement_files(export_fixes_dir, args.export_fixes) - except: + except Exception: sys.stderr.write("Error exporting fixes.\n") traceback.print_exc() return_code = 1 diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py index 59523fd131185..f4c3d00734389 100755 --- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py +++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py @@ -667,7 +667,7 @@ async def main() -> None: subprocess.check_call( invocation, stdout=subprocess.DEVNULL if args.quiet else None ) - except: + except Exception: print("Unable to run clang-tidy.", file=sys.stderr) sys.exit(1) @@ -681,7 +681,7 @@ async def main() -> None: if args.source_filter: try: source_filter_re = re.compile(args.source_filter) - except: + except Exception: print( "Error: unable to compile regex from arg -source-filter:", file=sys.stderr, @@ -764,7 +764,7 @@ async def main() -> None: try: assert export_fixes_dir merge_replacement_files(export_fixes_dir, args.export_fixes) - except: + except Exception: print("Error exporting fixes.\n", file=sys.stderr) traceback.print_exc() returncode = 1 @@ -775,7 +775,7 @@ async def main() -> None: try: assert export_fixes_dir apply_fixes(args, clang_apply_replacements_binary, export_fixes_dir) - except: + except Exception: print("Error applying fixes.\n", file=sys.stderr) traceback.print_exc() returncode = 1 >From b7141da79518eecbaab4bd018681725743dd5ed3 Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Tue, 20 Jan 2026 20:09:39 +0300 Subject: [PATCH 2/2] better --- clang-tools-extra/clang-tidy/add_new_check.py | 30 +++++++------------ clang-tools-extra/clang-tidy/rename_check.py | 2 +- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py index dfa60ce7f3990..1e4beac0e9344 100755 --- a/clang-tools-extra/clang-tidy/add_new_check.py +++ b/clang-tools-extra/clang-tidy/add_new_check.py @@ -218,8 +218,7 @@ def adapt_module( while True: line = next(lines_iter) if not header_added: - match = re.search('#include "(.*)"', line) - if match: + if match := re.search('#include "(.*)"', line): header_found = True if match.group(1) > check_name_camel: header_added = True @@ -233,11 +232,10 @@ def adapt_module( check_added = True f.write(check_decl) else: - match = re.search( - r'registerCheck<(.*)> *\( *(?:"([^"]*)")?', line - ) prev_line = None - if match: + if match := re.search( + r'registerCheck<(.*)> *\( *(?:"([^"]*)")?', line + ): current_check_name = match.group(2) if current_check_name is None: # If we didn't find the check name on this line, look on the @@ -410,17 +408,13 @@ def filename_from_module(module_name: str, check_name: str) -> str: with io.open(module_file, "r") as f: code = f.read() full_check_name = module_name + "-" + check_name - name_pos = code.find('"' + full_check_name + '"') - if name_pos == -1: + if (name_pos := code.find('"' + full_check_name + '"')) == -1: return "" - stmt_end_pos = code.find(";", name_pos) - if stmt_end_pos == -1: - return "" - stmt_start_pos = code.rfind(";", 0, name_pos) - if stmt_start_pos == -1: - stmt_start_pos = code.rfind("{", 0, name_pos) - if stmt_start_pos == -1: + if (stmt_end_pos := code.find(";", name_pos)) == -1: return "" + if (stmt_start_pos := code.rfind(";", 0, name_pos)) == -1: + if (stmt_start_pos := code.rfind("{", 0, name_pos)) == -1: + return "" stmt = code[stmt_start_pos + 1 : stmt_end_pos] matches = re.search(r'registerCheck<([^>:]*)>\(\s*"([^"]*)"\s*\)', stmt) if matches and matches[2] == full_check_name: @@ -493,8 +487,7 @@ def has_auto_fix(check_name: str) -> str: if has_fixits(code): return ' "Yes"' - base_class = get_base_class(code, check_file) - if base_class: + if base_class := get_base_class(code, check_file): base_file = os.path.join(clang_tidy_path, dirname, base_class + ".cpp") if os.path.isfile(base_file): with io.open(base_file, encoding="utf8") as f: @@ -752,8 +745,7 @@ def main() -> None: if language == "c": language_restrict = "!%(lang)s.CPlusPlus" - extra = c_language_to_requirements.get(args.standard, None) - if extra: + if extra := c_language_to_requirements.get(args.standard, None): language_restrict += f" && %(lang)s.{extra}" elif language == "c++": language_restrict = ( diff --git a/clang-tools-extra/clang-tidy/rename_check.py b/clang-tools-extra/clang-tidy/rename_check.py index d5432eadcff6b..51759dd12addf 100755 --- a/clang-tools-extra/clang-tidy/rename_check.py +++ b/clang-tools-extra/clang-tidy/rename_check.py @@ -70,7 +70,7 @@ def deleteMatchingLines(fileName: str, pattern: str) -> bool: return False print("Removing lines matching '%s' in '%s'..." % (pattern, fileName)) - print(" " + " ".join([line for line in lines if re.search(pattern, line)])) + print(" " + " ".join(line for line in lines if re.search(pattern, line))) with io.open(fileName, "w", encoding="utf8") as f: f.writelines(not_matching_lines) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
