-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix tracking of which locals would need to be captured in a closure.
* Captures by sub closures are now considered * Copy types are correctly borrowed by reference when their value is used * Fields are no longer automatically borrowed by value * Bindings in `match` and `let` patterns are now checked to determine how a local is captured
- Loading branch information
Showing
4 changed files
with
176 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,50 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::manual_map)] | ||
#![allow(clippy::toplevel_ref_arg)] | ||
|
||
fn main() { | ||
// Lint. `y` is declared within the arm, so it isn't captured by the map closure | ||
let _ = Some(0).map(|x| { | ||
let y = (String::new(), String::new()); | ||
(x, y.0) | ||
}); | ||
|
||
// Don't lint. `s` is borrowed until partway through the arm, but needs to be captured by the map | ||
// closure | ||
let s = Some(String::new()); | ||
let _ = match &s { | ||
Some(x) => Some((x.clone(), s)), | ||
None => None, | ||
}; | ||
|
||
// Don't lint. `s` is borrowed until partway through the arm, but needs to be captured by the map | ||
// closure | ||
let s = Some(String::new()); | ||
let _ = match &s { | ||
Some(x) => Some({ | ||
let clone = x.clone(); | ||
let s = || s; | ||
(clone, s()) | ||
}), | ||
None => None, | ||
}; | ||
|
||
// Don't lint. `s` is borrowed until partway through the arm, but needs to be captured as a mutable | ||
// reference by the map closure | ||
let mut s = Some(String::new()); | ||
let _ = match &s { | ||
Some(x) => Some({ | ||
let clone = x.clone(); | ||
let ref mut s = s; | ||
(clone, s) | ||
}), | ||
None => None, | ||
}; | ||
|
||
// Lint. `s` is captured by reference, so no lifetime issues. | ||
let s = Some(String::new()); | ||
let _ = s.as_ref().map(|x| { | ||
if let Some(ref s) = s { (x.clone(), s) } else { panic!() } | ||
}); | ||
} |
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