about summary refs log tree commit diff
path: root/clippy_dev/src
diff options
context:
space:
mode:
authorPhil Hansch <dev@phansch.net>2019-10-04 08:08:58 +0200
committerGitHub <noreply@github.com>2019-10-04 08:08:58 +0200
commit8d2912ec00a42698e44db924495bcc30a090ee2b (patch)
treef5fd4950c2ae59975eff323e6191578a74564e12 /clippy_dev/src
parentb824f021d61ddee59695f20c7128c39d80e9e5b6 (diff)
parent4cded6d9012bbfc77d13c7ef2a413402199a2a03 (diff)
downloadrust-8d2912ec00a42698e44db924495bcc30a090ee2b.tar.gz
rust-8d2912ec00a42698e44db924495bcc30a090ee2b.zip
Rollup merge of #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 https://github.com/rust-lang/rust/pull/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.
Diffstat (limited to 'clippy_dev/src')
-rw-r--r--clippy_dev/src/fmt.rs5
1 files changed, 1 insertions, 4 deletions
diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs
index 1f4925db1be..3c90aa4b507 100644
--- a/clippy_dev/src/fmt.rs
+++ b/clippy_dev/src/fmt.rs
@@ -100,10 +100,7 @@ pub fn run(check: bool, verbose: bool) {
 }
 
 fn format_command(program: impl AsRef<OsStr>, dir: impl AsRef<Path>, args: &[impl AsRef<OsStr>]) -> String {
-    let arg_display: Vec<_> = args
-        .iter()
-        .map(|a| escape(a.as_ref().to_string_lossy()).to_owned())
-        .collect();
+    let arg_display: Vec<_> = args.iter().map(|a| escape(a.as_ref().to_string_lossy())).collect();
 
     format!(
         "cd {} && {} {}",