Skip to content

Commit 8b097b5

Browse files
tamirdojeda
authored andcommitted
scripts: rust: replace length checks with match
Use a match expression with slice patterns instead of length checks and indexing. The result is more idiomatic, which is a better example for future Rust code authors. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Link: https://lore.kernel.org/r/20250529-idiomatic-match-slice-v2-1-4925ca2f1550@gmail.com [ Reworded title. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 275ad5e commit 8b097b5

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

scripts/rustdoc_test_gen.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,23 @@ fn find_real_path<'a>(srctree: &Path, valid_paths: &'a mut Vec<PathBuf>, file: &
8585
}
8686
}
8787

88-
assert!(
89-
valid_paths.len() > 0,
90-
"No path candidates found for `{file}`. This is likely a bug in the build system, or some \
91-
files went away while compiling."
92-
);
93-
94-
if valid_paths.len() > 1 {
95-
eprintln!("Several path candidates found:");
96-
for path in valid_paths {
97-
eprintln!(" {path:?}");
88+
match valid_paths.as_slice() {
89+
[] => panic!(
90+
"No path candidates found for `{file}`. This is likely a bug in the build system, or \
91+
some files went away while compiling."
92+
),
93+
[valid_path] => valid_path.to_str().unwrap(),
94+
valid_paths => {
95+
eprintln!("Several path candidates found:");
96+
for path in valid_paths {
97+
eprintln!(" {path:?}");
98+
}
99+
panic!(
100+
"Several path candidates found for `{file}`, please resolve the ambiguity by \
101+
renaming a file or folder."
102+
);
98103
}
99-
panic!(
100-
"Several path candidates found for `{file}`, please resolve the ambiguity by renaming \
101-
a file or folder."
102-
);
103104
}
104-
105-
valid_paths[0].to_str().unwrap()
106105
}
107106

108107
fn main() {

0 commit comments

Comments
 (0)