Prevents AI assistants from suggesting outdated Rust code patterns
cargo install rust-guard
use rust_guard_v2::Guard;
fn main() {
let guard = Guard::new()
.deny_pattern("try!")
.deny_pattern("extern crate")
.deny_pattern(".to_owned()")
.suggest_instead(".to_owned()", ".to_string() or .clone()");
let code = r#"
let result = try!(some_operation());
"#;
match guard.check(code) {
Ok(_) => println!("clean"),
Err(violations) => {
for v in violations {
eprintln!("{}", v);
}
}
}
}Catches common outdated patterns that LLMs trained on old Rust code still suggest. Add custom rules for your project's conventions.
Default rules include:
try!macro (use?operator)extern crate(2015 edition syntax).to_owned()on&str(usually want.to_string())std::ascii::AsciiExt(deprecated)- match ergonomics antipatterns
Run as pre-commit hook or in CI. Pairs well with clippy but focuses on patterns that evolved with editions rather than just correctness.
use rust_guard_v2::{Guard, Severity};
// custom project rules
let guard = Guard::new()
.with_config("rust-guard.toml")
.deny_pattern_with_context(
r"\.unwrap\(\)",
Severity::Warn,
"prefer ? or expect() with context"
)
.deny_import("tokio::prelude", "import specific items instead")
.require_feature_for_pattern(
r"unsafe \{",
"#![forbid(unsafe_code)] not set"
);
// check entire project
guard.check_workspace(".")?;
// or integrate into build.rs
guard.check_src("src/")?;MIT