Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::path_buf_push_overwrite: calling `push` with '/' or '\' (file system root) will overwrite the previous path definition
What it meansChecks for push calls on PathBuf that can cause overwrites. It is in the nursery group and off by default.
What you see
warning: calling `push` with '/' or '\' (file system root) will overwrite the previous path definition --> main.rs:6:8 | 6 | x.push("/bar"); | ^^^^^^ help: try: `"bar"` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#path_buf_push_overwrite = note: `-W clippy::path-buf-push-overwrite` implied by `-W clippy::nursery`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Calling push with a root path at the start can overwrite the previous defined path.
New lints still being finished. Off by default and may have false positives.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
use std::path::PathBuf;
let mut x = PathBuf::from("/foo");
x.push("/bar");
assert_eq!(x, PathBuf::from("/bar"));
Write this instead
use std::path::PathBuf;
let mut x = PathBuf::from("/foo");
x.push("bar");
assert_eq!(x, PathBuf::from("/foo/bar"));
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
path_buf_push_overwrite = "warn"In code
#![warn(clippy::path_buf_push_overwrite)]One run
cargo clippy -- -W clippy::path-buf-push-overwriteThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
- Checks for push
calls on
PathBufthat can cause overwrites.
Why is this bad?
Calling push with a root path at the start can overwrite the
previous defined path.
Example
use std::path::PathBuf;
let mut x = PathBuf::from("/foo");
x.push("/bar");
assert_eq!(x, PathBuf::from("/bar"));
Could be written:
use std::path::PathBuf;
let mut x = PathBuf::from("/foo");
x.push("bar");
assert_eq!(x, PathBuf::from("/foo/bar"));
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- non_send_fields_in_send_tyThis lint warns about a Send implementation for a type that contains fields that are not safe to be sent across threads. It tries to detect fields that can cause a soundness issue when sent to another thread (e.g., Rc) while allowing !Send fields that are expected to exist in a Send type, such as raw pointers
- option_if_let_elseLints usage of if let Some(v) =
- or_fun_callChecks for calls to .or(foo(..)), .unwrap_or(foo(..)), .or_insert(foo(..)) etc., and suggests to use .or_else(|| foo(..)), .unwrap_or_else(|| foo(..)), .unwrap_or_default() or .or_default() etc
- read_zero_byte_vecreading zero byte data to `Vec`
- redundant_pub_cratepub(crate) function inside private module
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →Did this answer your question?