Skip to content

Commit 8d2912e

Browse files
authored
Rollup merge of rust-lang#4509 - sinkuu:redundant_clone_fix, r=llogiq
Fix false-positive of redundant_clone and move to clippy::perf This PR introduces dataflow analysis to `redundant_clone` lint to filter out borrowed variables, which had been incorrectly detected. Depends on rust-lang/rust#64207. changelog: Moved `redundant_clone` lint to `perf` group # What this lint catches ## `clone`/`to_owned` ```rust let s = String::new(); let t = s.clone(); ``` ```rust // MIR _1 = String::new(); _2 = &_1; _3 = clone(_2); // (*) ``` We can turn this `clone` call into a move if 1. `_2` is the sole borrow of `_1` at the statement `(*)` 2. `_1` is not used hereafter ## `Deref` + type-specific `to_owned` method ```rust let s = std::path::PathBuf::new(); let t = s.to_path_buf(); ``` ```rust // MIR _1 = PathBuf::new(); _2 = &1; _3 = call deref(_2); _4 = _3; // Copies borrow StorageDead(_2); _5 = Path::to_path_buf(_4); // (*) ``` We can turn this `to_path_buf` call into a move if 1. `_3` `_4` are the sole borrow of `_1` at `(*)` 2. `_1` is not used hereafter # What this PR introduces 1. `MaybeStorageLive` that determines whether a local lives at a particular location 2. `PossibleBorrowerVisitor` that constructs [`TransitiveRelation`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/transitive_relation/struct.TransitiveRelation.html) of possible borrows, e.g. visiting `_2 = &1; _3 = &_2:` will result in `_3 -> _2 -> _1` relation. Then `_3` and `_2` will be counted as possible borrowers of `_1` in the sole-borrow analysis above.
2 parents b824f02 + 4cded6d commit 8d2912e

21 files changed

+717
-205
lines changed

clippy_dev/src/fmt.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ pub fn run(check: bool, verbose: bool) {
100100
}
101101

102102
fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
103-
let arg_display: Vec<_> = args
104-
.iter()
105-
.map(|a| escape(a.as_ref().to_string_lossy()).to_owned())
106-
.collect();
103+
let arg_display: Vec<_> = args.iter().map(|a| escape(a.as_ref().to_string_lossy())).collect();
107104

108105
format!(
109106
"cd {} && {} {}",

clippy_lints/src/booleans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
343343

344344
let stats = terminal_stats(&expr);
345345
let mut simplified = expr.simplify();
346-
for simple in Bool::Not(Box::new(expr.clone())).simplify() {
346+
for simple in Bool::Not(Box::new(expr)).simplify() {
347347
match simple {
348348
Bool::Not(_) | Bool::True | Bool::False => {},
349349
_ => simplified.push(Bool::Not(Box::new(simple.clone()))),

clippy_lints/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ extern crate rustc_driver;
2727
#[allow(unused_extern_crates)]
2828
extern crate rustc_errors;
2929
#[allow(unused_extern_crates)]
30+
extern crate rustc_index;
31+
#[allow(unused_extern_crates)]
3032
extern crate rustc_mir;
3133
#[allow(unused_extern_crates)]
3234
extern crate rustc_target;
@@ -864,6 +866,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
864866
ranges::RANGE_MINUS_ONE,
865867
ranges::RANGE_PLUS_ONE,
866868
ranges::RANGE_ZIP_WITH_LEN,
869+
redundant_clone::REDUNDANT_CLONE,
867870
redundant_field_names::REDUNDANT_FIELD_NAMES,
868871
redundant_pattern_matching::REDUNDANT_PATTERN_MATCHING,
869872
redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
@@ -1169,6 +1172,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
11691172
methods::SINGLE_CHAR_PATTERN,
11701173
misc::CMP_OWNED,
11711174
mutex_atomic::MUTEX_ATOMIC,
1175+
redundant_clone::REDUNDANT_CLONE,
11721176
slow_vector_initialization::SLOW_VECTOR_INITIALIZATION,
11731177
trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF,
11741178
types::BOX_VEC,
@@ -1188,7 +1192,6 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
11881192
mutex_atomic::MUTEX_INTEGER,
11891193
needless_borrow::NEEDLESS_BORROW,
11901194
path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE,
1191-
redundant_clone::REDUNDANT_CLONE,
11921195
]);
11931196
}
11941197

0 commit comments

Comments
 (0)