[pathbuf_init_then_push]: Checks for calls to push immediately after creating a new PathBuf

Co-authored-by: Fridtjof Stoldt <xFrednet@gmail.com>
This commit is contained in:
lengyijun 2023-10-23 22:11:21 +08:00
parent b31bce4f5f
commit cb77f12600
14 changed files with 328 additions and 44 deletions

View file

@ -1,6 +1,7 @@
use std::path::PathBuf;
#[warn(clippy::all, clippy::path_buf_push_overwrite)]
#[warn(clippy::path_buf_push_overwrite)]
#[allow(clippy::pathbuf_init_then_push)]
fn main() {
let mut x = PathBuf::from("/foo");
x.push("bar");

View file

@ -1,6 +1,7 @@
use std::path::PathBuf;
#[warn(clippy::all, clippy::path_buf_push_overwrite)]
#[warn(clippy::path_buf_push_overwrite)]
#[allow(clippy::pathbuf_init_then_push)]
fn main() {
let mut x = PathBuf::from("/foo");
x.push("/bar");

View file

@ -1,5 +1,5 @@
error: calling `push` with '/' or '\' (file system root) will overwrite the previous path definition
--> tests/ui/path_buf_push_overwrite.rs:6:12
--> tests/ui/path_buf_push_overwrite.rs:7:12
|
LL | x.push("/bar");
| ^^^^^^ help: try: `"bar"`

View file

@ -0,0 +1,22 @@
#![warn(clippy::pathbuf_init_then_push)]
use std::path::PathBuf;
fn main() {
let mut path_buf = PathBuf::from("foo");
path_buf = PathBuf::from("foo").join("bar");
let bar = "bar";
path_buf = PathBuf::from("foo").join(bar);
let mut path_buf = PathBuf::from("foo").join("bar").join("buz");
let mut x = PathBuf::new();
println!("{}", x.display());
x.push("Duck");
let mut path_buf = PathBuf::new();
#[cfg(cats)]
path_buf.push("foo");
}

View file

@ -0,0 +1,26 @@
#![warn(clippy::pathbuf_init_then_push)]
use std::path::PathBuf;
fn main() {
let mut path_buf = PathBuf::new(); //~ ERROR: calls to `push` immediately after creation
path_buf.push("foo");
path_buf = PathBuf::from("foo"); //~ ERROR: calls to `push` immediately after creation
path_buf.push("bar");
let bar = "bar";
path_buf = PathBuf::from("foo"); //~ ERROR: calls to `push` immediately after creation
path_buf.push(bar);
let mut path_buf = PathBuf::from("foo").join("bar"); //~ ERROR: calls to `push` immediately after creation
path_buf.push("buz");
let mut x = PathBuf::new();
println!("{}", x.display());
x.push("Duck");
let mut path_buf = PathBuf::new();
#[cfg(cats)]
path_buf.push("foo");
}

View file

@ -0,0 +1,33 @@
error: calls to `push` immediately after creation
--> tests/ui/pathbuf_init_then_push.rs:6:5
|
LL | / let mut path_buf = PathBuf::new();
LL | | path_buf.push("foo");
| |_________________________^ help: consider using the `.join()`: `let mut path_buf = PathBuf::from("foo");`
|
= note: `-D clippy::pathbuf-init-then-push` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::pathbuf_init_then_push)]`
error: calls to `push` immediately after creation
--> tests/ui/pathbuf_init_then_push.rs:9:5
|
LL | / path_buf = PathBuf::from("foo");
LL | | path_buf.push("bar");
| |_________________________^ help: consider using the `.join()`: `path_buf = PathBuf::from("foo").join("bar");`
error: calls to `push` immediately after creation
--> tests/ui/pathbuf_init_then_push.rs:13:5
|
LL | / path_buf = PathBuf::from("foo");
LL | | path_buf.push(bar);
| |_______________________^ help: consider using the `.join()`: `path_buf = PathBuf::from("foo").join(bar);`
error: calls to `push` immediately after creation
--> tests/ui/pathbuf_init_then_push.rs:16:5
|
LL | / let mut path_buf = PathBuf::from("foo").join("bar");
LL | | path_buf.push("buz");
| |_________________________^ help: consider using the `.join()`: `let mut path_buf = PathBuf::from("foo").join("bar").join("buz");`
error: aborting due to 4 previous errors

View file

@ -3,6 +3,7 @@
#![allow(
clippy::drop_non_drop,
clippy::implicit_clone,
clippy::pathbuf_init_then_push,
clippy::uninlined_format_args,
clippy::unnecessary_literal_unwrap
)]

View file

@ -3,6 +3,7 @@
#![allow(
clippy::drop_non_drop,
clippy::implicit_clone,
clippy::pathbuf_init_then_push,
clippy::uninlined_format_args,
clippy::unnecessary_literal_unwrap
)]

View file

@ -1,11 +1,11 @@
error: redundant clone
--> tests/ui/redundant_clone.rs:14:42
--> tests/ui/redundant_clone.rs:15:42
|
LL | let _s = ["lorem", "ipsum"].join(" ").to_string();
| ^^^^^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:14:14
--> tests/ui/redundant_clone.rs:15:14
|
LL | let _s = ["lorem", "ipsum"].join(" ").to_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -13,169 +13,169 @@ LL | let _s = ["lorem", "ipsum"].join(" ").to_string();
= help: to override `-D warnings` add `#[allow(clippy::redundant_clone)]`
error: redundant clone
--> tests/ui/redundant_clone.rs:17:15
--> tests/ui/redundant_clone.rs:18:15
|
LL | let _s = s.clone();
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:17:14
--> tests/ui/redundant_clone.rs:18:14
|
LL | let _s = s.clone();
| ^
error: redundant clone
--> tests/ui/redundant_clone.rs:20:15
--> tests/ui/redundant_clone.rs:21:15
|
LL | let _s = s.to_string();
| ^^^^^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:20:14
--> tests/ui/redundant_clone.rs:21:14
|
LL | let _s = s.to_string();
| ^
error: redundant clone
--> tests/ui/redundant_clone.rs:23:15
--> tests/ui/redundant_clone.rs:24:15
|
LL | let _s = s.to_owned();
| ^^^^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:23:14
--> tests/ui/redundant_clone.rs:24:14
|
LL | let _s = s.to_owned();
| ^
error: redundant clone
--> tests/ui/redundant_clone.rs:25:42
--> tests/ui/redundant_clone.rs:26:42
|
LL | let _s = Path::new("/a/b/").join("c").to_owned();
| ^^^^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:25:14
--> tests/ui/redundant_clone.rs:26:14
|
LL | let _s = Path::new("/a/b/").join("c").to_owned();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant clone
--> tests/ui/redundant_clone.rs:27:42
--> tests/ui/redundant_clone.rs:28:42
|
LL | let _s = Path::new("/a/b/").join("c").to_path_buf();
| ^^^^^^^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:27:14
--> tests/ui/redundant_clone.rs:28:14
|
LL | let _s = Path::new("/a/b/").join("c").to_path_buf();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant clone
--> tests/ui/redundant_clone.rs:29:29
--> tests/ui/redundant_clone.rs:30:29
|
LL | let _s = OsString::new().to_owned();
| ^^^^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:29:14
--> tests/ui/redundant_clone.rs:30:14
|
LL | let _s = OsString::new().to_owned();
| ^^^^^^^^^^^^^^^
error: redundant clone
--> tests/ui/redundant_clone.rs:31:29
--> tests/ui/redundant_clone.rs:32:29
|
LL | let _s = OsString::new().to_os_string();
| ^^^^^^^^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:31:14
--> tests/ui/redundant_clone.rs:32:14
|
LL | let _s = OsString::new().to_os_string();
| ^^^^^^^^^^^^^^^
error: redundant clone
--> tests/ui/redundant_clone.rs:42:19
--> tests/ui/redundant_clone.rs:43:19
|
LL | let _t = tup.0.clone();
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:42:14
--> tests/ui/redundant_clone.rs:43:14
|
LL | let _t = tup.0.clone();
| ^^^^^
error: redundant clone
--> tests/ui/redundant_clone.rs:74:25
--> tests/ui/redundant_clone.rs:75:25
|
LL | if b { (a.clone(), a.clone()) } else { (Alpha, a) }
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:74:24
--> tests/ui/redundant_clone.rs:75:24
|
LL | if b { (a.clone(), a.clone()) } else { (Alpha, a) }
| ^
error: redundant clone
--> tests/ui/redundant_clone.rs:131:15
|
LL | let _s = s.clone();
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:131:14
|
LL | let _s = s.clone();
| ^
error: redundant clone
--> tests/ui/redundant_clone.rs:132:15
|
LL | let _t = t.clone();
LL | let _s = s.clone();
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:132:14
|
LL | let _s = s.clone();
| ^
error: redundant clone
--> tests/ui/redundant_clone.rs:133:15
|
LL | let _t = t.clone();
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:133:14
|
LL | let _t = t.clone();
| ^
error: redundant clone
--> tests/ui/redundant_clone.rs:142:19
--> tests/ui/redundant_clone.rs:143:19
|
LL | let _f = f.clone();
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:142:18
--> tests/ui/redundant_clone.rs:143:18
|
LL | let _f = f.clone();
| ^
error: redundant clone
--> tests/ui/redundant_clone.rs:154:14
--> tests/ui/redundant_clone.rs:155:14
|
LL | let y = x.clone().join("matthias");
| ^^^^^^^^ help: remove this
|
note: cloned value is neither consumed nor mutated
--> tests/ui/redundant_clone.rs:154:13
--> tests/ui/redundant_clone.rs:155:13
|
LL | let y = x.clone().join("matthias");
| ^^^^^^^^^
error: redundant clone
--> tests/ui/redundant_clone.rs:208:11
--> tests/ui/redundant_clone.rs:209:11
|
LL | foo(&x.clone(), move || {
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> tests/ui/redundant_clone.rs:208:10
--> tests/ui/redundant_clone.rs:209:10
|
LL | foo(&x.clone(), move || {
| ^