v0.1.0-pre.013

This commit is contained in:
2026-07-24 16:44:51 +02:00
parent e753a80c3a
commit 5f6f7df509
38 changed files with 1202 additions and 172 deletions

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# file: scripts/audit_rust_general_rules.py
# version: 6
# version: 7
"""Audit mechanically verifiable Rust rules shared by all Rust projects."""
@@ -38,6 +38,24 @@ def rust_files(root: pathlib.Path) -> list[pathlib.Path]:
)
def rustfmt_re_export_key(export_path: str) -> tuple[object, ...]:
"""Return the natural path order used by rustfmt for separate imports."""
path = export_path.removesuffix(";")
segments = path.split("::")
root_priority = 0 if segments[0] == "self" else 1 if segments[0] == "super" else 2
segment_keys: list[tuple[tuple[int, object], ...]] = []
for segment in segments:
tokens: list[tuple[int, object]] = []
for token in re.findall(r"[A-Za-z]+|[0-9]+", segment):
if token.isdigit():
tokens.append((1, int(token)))
else:
tokens.append((0, token))
segment_keys.append(tuple(tokens))
return (root_priority, *segment_keys)
def audit_file(root: pathlib.Path, path: pathlib.Path) -> list[Violation]:
"""Audit one Rust source against general rules."""
@@ -96,7 +114,7 @@ def audit_file(root: pathlib.Path, path: pathlib.Path) -> list[Violation]:
violations.append(Violation("RUST016", relative, index, "re-export aliases are forbidden"))
previous_export_line: int | None = None
previous_export_visibility: str | None = None
previous_export_path: str | None = None
previous_export_key: tuple[object, ...] | None = None
blank_since_export = False
for index, line in enumerate(lines, 1):
if not line.strip():
@@ -121,12 +139,13 @@ def audit_file(root: pathlib.Path, path: pathlib.Path) -> list[Violation]:
)
)
export_path = line.split(" use ", 1)[1]
export_key = rustfmt_re_export_key(export_path)
if (
previous_export_line is not None
and previous_export_visibility == visibility
and not blank_since_export
and previous_export_path is not None
and export_path < previous_export_path
and previous_export_key is not None
and export_key < previous_export_key
):
violations.append(
Violation(
@@ -138,12 +157,12 @@ def audit_file(root: pathlib.Path, path: pathlib.Path) -> list[Violation]:
)
previous_export_line = index
previous_export_visibility = visibility
previous_export_path = export_path
previous_export_key = export_key
blank_since_export = False
continue
previous_export_line = None
previous_export_visibility = None
previous_export_path = None
previous_export_key = None
blank_since_export = False
if path.name in {"lib.rs", "main.rs"}:
for attribute in ("#![warn(missing_docs)]", "#![deny(unreachable_pub)]", "#![forbid(unsafe_code)]"):