Fix the panic scan for a Rust config loader
Your task
An AI explained a Rust config loader as safe: "It returns a Result, so any error goes to the caller. It cannot crash the process." Your team checks a claim like that by scanning the function body for every call that panics, which stops the thread instead of returning an error.
An assistant wrote find_panic_sites(body_lines). It is in the editor. It has a bug. Find it and fix it.
Input and output:
body_lines: the lines of a Rust function body, in order.- Return a list of findings. Each finding is
"<line>:<name>", where<line>is the line number counting from 1.
Rules the tests check:
- On each line, ignore everything from the first
//onward. It is a comment. - These exact texts are panic sites, each with the name it reports:
.unwrap(reportsunwrap.expect(reportsexpectpanic!(reportspanicunreachable!(reportsunreachabletodo!(reportstodo
- Report every occurrence. A line with two panic sites gives two findings.
- Findings go in line order, and left to right within a line.
- A match inside a string literal still counts. Nothing else is a panic site, so
.unwrap_or(is not one.
Examples
find_panic_sites(body_lines: list[str]) -> list[str]Example 1
Input
find_panic_sites(["fn load_config(path: &str) -> Result<Config, ConfigError> {", " let text = std::fs::read_to_string(path).unwrap();", " let cfg: Config = serde_json::from_str(&text).expect(\"valid config\");", " Ok(cfg)", "}"])Output
["2:unwrap", "3:expect"]Example 2
Input
find_panic_sites(["fn load_config(path: &str) -> Result<Config, ConfigError> {", " let text = std::fs::read_to_string(path)?;", " let cfg: Config = serde_json::from_str(&text)?;", " Ok(cfg)", "}"])Output
[]