-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide suggestion to dereference closure tail if appropriate
When encoutnering a case like ```rust //@ run-rustfix use std::collections::HashMap; fn main() { let vs = vec![0, 0, 1, 1, 3, 4, 5, 6, 3, 3, 3]; let mut counts = HashMap::new(); for num in vs { let count = counts.entry(num).or_insert(0); *count += 1; } let _ = counts.iter().max_by_key(|(_, v)| v); ``` produce the following suggestion ``` error: lifetime may not live long enough --> $DIR/return-value-lifetime-error.rs:13:47 | LL | let _ = counts.iter().max_by_key(|(_, v)| v); | ------- ^ returning this value requires that `'1` must outlive `'2` | | | | | return type of closure is &'2 &i32 | has type `&'1 (&i32, &i32)` | help: dereference the return value | LL | let _ = counts.iter().max_by_key(|(_, v)| **v); | ++ ``` Fix #50195.
- Loading branch information
Showing
6 changed files
with
239 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//@ run-rustfix | ||
use std::collections::HashMap; | ||
|
||
fn main() { | ||
let vs = vec![0, 0, 1, 1, 3, 4, 5, 6, 3, 3, 3]; | ||
|
||
let mut counts = HashMap::new(); | ||
for num in vs { | ||
let count = counts.entry(num).or_insert(0); | ||
*count += 1; | ||
} | ||
|
||
let _ = counts.iter().max_by_key(|(_, v)| **v); | ||
//~^ ERROR lifetime may not live long enough | ||
//~| HELP dereference the return value | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//@ run-rustfix | ||
use std::collections::HashMap; | ||
|
||
fn main() { | ||
let vs = vec![0, 0, 1, 1, 3, 4, 5, 6, 3, 3, 3]; | ||
|
||
let mut counts = HashMap::new(); | ||
for num in vs { | ||
let count = counts.entry(num).or_insert(0); | ||
*count += 1; | ||
} | ||
|
||
let _ = counts.iter().max_by_key(|(_, v)| v); | ||
//~^ ERROR lifetime may not live long enough | ||
//~| HELP dereference the return value | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/return-value-lifetime-error.rs:13:47 | ||
| | ||
LL | let _ = counts.iter().max_by_key(|(_, v)| v); | ||
| ------- ^ returning this value requires that `'1` must outlive `'2` | ||
| | | | ||
| | return type of closure is &'2 &i32 | ||
| has type `&'1 (&i32, &i32)` | ||
| | ||
help: dereference the return value | ||
| | ||
LL | let _ = counts.iter().max_by_key(|(_, v)| **v); | ||
| ++ | ||
|
||
error: aborting due to 1 previous error | ||
|