forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#7346 - lengyijun:redundant_clone_5707, r=oli-obk
fix 5707 changelog: ``[`redundant_clone`]``, fix rust-lang#5707 # Root problem of rust-lang#5707 : ``` &2:&mut HashMap = &mut _4; &3:&str = & _5; _1 = HashMap::insert(move _2,move _3, _); ``` generate PossibleBorrower(_2,_1) and PossibleBorrower(_3,_1). However, it misses PossibleBorrower(_3,_2). # My solution to rust-lang#5707 : When meet a function call, we should: 1. build PossibleBorrower between borrow parameters and return value (currently) 2. build PossibleBorrower between immutable borrow parameters and mutable borrow parameters (*add*) 3. build PossibleBorrower inside mutable borrow parameters (*add*) For example: ``` _2: &mut _22; _3: &mut _; _4: & _; _5: & _; _1 = call(move _2, move _3, move _4, move _5); ``` we need to build 1. return value with parameter(current implementataion) PossibleBorrower(_2,_1) PossibleBorrower(_3,_1) PossibleBorrower(_4,_1) PossibleBorrower(_5,_1) 2. between mutable borrow and immutable borrow PossibleBorrower(_4,_2) PossibleBorrower(_5,_2) PossibleBorrower(_4,_3) PossibleBorrower(_5,_3) 3. between mutable borrow and mutable borrow PossibleBorrower(_3,_2) PossibleBorrower(_2,_3) But that's not enough. Modification to _2 actually apply to _22. So I write a `PossibleBorrowed` visitor, which tracks (borrower => possible borrowed) relation. For example (_2 => _22). However, a lot of problems exist here. ## Known Problems: 1. not sure all `&mut`'s origin are collected. I'm not sure how to deal with `&mut` when meet a function call, so I didn't do it currently. Also, my implement is not flow sensitive, so it's not accurate. ``` foo(_2:&mut _, _3: &_) ``` This pr doesn't count _3 as origin of _2. 2. introduce false negative `foo(_2, _3)` will emit PossibleBorrower(_3,_2) in this pr, but _3 and _2 may not have relation. Clippy may feel that _3 is still in use because of _2, but actually, _3 is on longer needed and can be moved. ## Insight The key problem is determine where every `&mut` come from accurately. I think Polonius is an elegant solution to it. Polonius is flow sensitive and accurate. But I'm uncertain about whether we can import Polonius in rust-clippy currently. This pr actually is part of Polonius' functionality, I think. # TODO 1. `cargo test` can't pass yet due to similar variable name
- Loading branch information
Showing
4 changed files
with
168 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters