# How do I review AI-written Rust code before trusting it?
Source: https://ruststudio.org/community/t/153/how-do-i-review-ai-written-rust-code-before-trusting-it
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0 and clippy 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: ai, review, security | Views: 0

## Short answer
Treat it like code from a stranger. Build it, run the tests, run Clippy, search the diff for unsafe, unwrap and new dependencies, and read it until you can explain every line. The compiler and tests catch wrong code. They do not catch code that is correct but not what you intended.

## Answer
## Answer
An assistant's reply is a proposal. Rust's compiler checks memory safety and types for you, which removes a whole class of mistakes, but it cannot check that the logic does what you meant. Review in layers, from cheapest to most careful.

## How to do it
1. **Build and test.** `cargo build` and `cargo test` (in Ruststudio: Run > Build, Run > Test: Run All Tests). If the assistant changed behaviour, an existing test should fail; if none does, add one first.
2. **Run Clippy.** It flags unidiomatic and suspicious code. Add `-W clippy::unwrap_used` to flag every `unwrap()`:

```rust title=src/main.rs
fn main() {
    let text = std::env::args().nth(1).unwrap();
    let n: i32 = text.parse().unwrap();
    println!("{n}");
}
```

```text
> cargo clippy -- -W clippy::unwrap_used
warning: used `unwrap()` on an `Option` value
 --> src\main.rs:2:16
  |
2 |     let text = std::env::args().nth(1).unwrap();
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: if this value is `None`, it will panic
  = help: consider using `expect()` to provide a better panic message
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#unwrap_used
  = note: requested on the command line with `-W clippy::unwrap-used`

warning: used `unwrap()` on a `Result` value
 --> src\main.rs:3:18
  |
3 |     let n: i32 = text.parse().unwrap();
  |                  ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: if this value is an `Err`, it will panic
  = help: consider using `expect()` to provide a better panic message
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.93.0/index.html#unwrap_used

warning: `rev` (bin "rev") generated 2 warnings
```

3. **Search for the risky words** in the changed files: `unsafe`, `unwrap`, `expect`, `panic!`, `todo!`, `unimplemented!`, and `as` casts between number types. In Ruststudio use Edit > Find in Files.
4. **Forbid unsafe where you do not need it.** Put `#![forbid(unsafe_code)]` at the top of `main.rs` or `lib.rs`. Any `unsafe` block, including one an assistant adds later, then fails to compile:

```rust title=forbid.rs
#![forbid(unsafe_code)]

fn main() {
    let x = 5;
    let p = &x as *const i32;
    let y = unsafe { *p };
    println!("{y}");
}
```

```text
error: usage of an `unsafe` block
 --> forbid.rs:6:13
  |
6 |     let y = unsafe { *p };
  |             ^^^^^^^^^^^^^
  |
note: the lint level is defined here
 --> forbid.rs:1:11
  |
1 | #![forbid(unsafe_code)]
  |           ^^^^^^^^^^^

error: aborting due to 1 previous error
```

5. **Look at `Cargo.toml`.** Every new dependency is code you now run and trust. Check that the crate exists under that name and is the one you mean, that the version is current, and what it pulls in (`cargo tree`). Run `cargo audit` (Tools > Cargo: Security Audit) to check known vulnerabilities; it needs `cargo-audit` installed.
6. **Read the logic.** Compare edge cases with the specification: empty input, large input, errors. Be careful with anything that touches the file system, the network, passwords or SQL; do not accept it until you can say why it is safe.

## Common mistakes
- Trusting code because it compiles. It may compile and still delete the wrong file.
- Assuming an API exists because the assistant used it. Models can invent functions and crate features; the compiler will then complain, but a nonexistent crate name is also a way to pull in an unintended package.
- Skipping the review of tests the assistant wrote along with the code. They share the same misunderstanding.
