Skip to content

Commit d71e9c4

Browse files
committed
Auto merge of #4266 - uHOOCCOOHu:fix/async_fn_lifetime, r=flip1995
Ignore generated fresh lifetimes in elision check <!-- Thank you for making Clippy better! We're collecting our changelog from pull request descriptions. If your PR only updates to the latest nightly, you can leave the `changelog` entry as `none`. Otherwise, please write a short comment explaining your change. If your PR fixes an issue, you can add "fixes #issue_number" into this PR description. This way the issue will be automatically closed when your PR is merged. If you added a new lint, here's a checklist for things that will be checked during review or continuous integration. - [ ] Followed [lint naming conventions][lint_naming] - [ ] Added passing UI tests (including committed `.stderr` file) - [ ] `cargo test` passes locally - [ ] Executed `util/dev update_lints` - [ ] Added lint documentation - [ ] Run `cargo fmt` Note that you can skip the above if you are just opening a WIP PR in order to get feedback. Delete this line and everything above before opening your PR --> fixes #3988 changelog: Ignore generated fresh lifetimes in elision check. **HELP**: It seems `tests/ui` are compiled under edition 2015, and I don't know how to add tests for this properly. Here is the test input it had already passed: ```rust #![feature(async_await)] #![allow(dead_code)] async fn sink1<'a>(_: &'a str) {} // lint async fn sink1_elided(_: &str) {} // ok async fn one_to_one<'a>(s: &'a str) -> &'a str { s } // lint async fn one_to_one_elided(s: &str) -> &str { s } // ok async fn all_to_one<'a>(a: &'a str, _b: &'a str) -> &'a str { a } // ok // async fn unrelated(_: &str, _: &str) {} // Not allowed in async fn // #3988 struct Foo; impl Foo { pub async fn foo(&mut self) {} // ok } // rust-lang/rust#61115 async fn print(s: &str) { // ok println!("{}", s); } fn main() {} ```
2 parents 49ff0d9 + 5265ab8 commit d71e9c4

12 files changed

+86
-28
lines changed

clippy_lints/src/lifetimes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ impl<'v, 't> RefVisitor<'v, 't> {
283283
if let Some(ref lt) = *lifetime {
284284
if lt.name == LifetimeName::Static {
285285
self.lts.push(RefLt::Static);
286+
} else if let LifetimeName::Param(ParamName::Fresh(_)) = lt.name {
287+
// Fresh lifetimes generated should be ignored.
286288
} else if lt.is_elided() {
287289
self.lts.push(RefLt::Unnamed);
288290
} else {

rustfmt.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ max_width = 120
22
comment_width = 100
33
match_block_trailing_comma = true
44
wrap_comments = true
5-
5+
edition = "2018"
66
error_on_line_overflow = true

tests/ui/cognitive_complexity.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ fn mcarton_sees_all() {
313313
}
314314

315315
#[clippy::cognitive_complexity = "0"]
316-
fn try() -> Result<i32, &'static str> {
316+
fn try_() -> Result<i32, &'static str> {
317317
match 5 {
318318
5 => Ok(5),
319319
_ => return Err("bla"),
@@ -322,14 +322,14 @@ fn try() -> Result<i32, &'static str> {
322322

323323
#[clippy::cognitive_complexity = "0"]
324324
fn try_again() -> Result<i32, &'static str> {
325-
let _ = try!(Ok(42));
326-
let _ = try!(Ok(43));
327-
let _ = try!(Ok(44));
328-
let _ = try!(Ok(45));
329-
let _ = try!(Ok(46));
330-
let _ = try!(Ok(47));
331-
let _ = try!(Ok(48));
332-
let _ = try!(Ok(49));
325+
let _ = r#try!(Ok(42));
326+
let _ = r#try!(Ok(43));
327+
let _ = r#try!(Ok(44));
328+
let _ = r#try!(Ok(45));
329+
let _ = r#try!(Ok(46));
330+
let _ = r#try!(Ok(47));
331+
let _ = r#try!(Ok(48));
332+
let _ = r#try!(Ok(49));
333333
match 5 {
334334
5 => Ok(5),
335335
_ => return Err("bla"),

tests/ui/cognitive_complexity.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ LL | | }
216216
error: the function has a cognitive complexity of 1
217217
--> $DIR/cognitive_complexity.rs:316:1
218218
|
219-
LL | / fn try() -> Result<i32, &'static str> {
219+
LL | / fn try_() -> Result<i32, &'static str> {
220220
LL | | match 5 {
221221
LL | | 5 => Ok(5),
222222
LL | | _ => return Err("bla"),
@@ -230,9 +230,9 @@ error: the function has a cognitive complexity of 1
230230
--> $DIR/cognitive_complexity.rs:324:1
231231
|
232232
LL | / fn try_again() -> Result<i32, &'static str> {
233-
LL | | let _ = try!(Ok(42));
234-
LL | | let _ = try!(Ok(43));
235-
LL | | let _ = try!(Ok(44));
233+
LL | | let _ = r#try!(Ok(42));
234+
LL | | let _ = r#try!(Ok(43));
235+
LL | | let _ = r#try!(Ok(44));
236236
... |
237237
LL | | }
238238
LL | | }

tests/ui/dlist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use alloc::collections::linked_list::LinkedList;
77

88
trait Foo {
99
type Baz = LinkedList<u8>;
10-
fn foo(LinkedList<u8>);
10+
fn foo(_: LinkedList<u8>);
1111
const BAR: Option<LinkedList<u8>>;
1212
}
1313

tests/ui/dlist.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ LL | type Baz = LinkedList<u8>;
88
= help: a VecDeque might work
99

1010
error: I see you're using a LinkedList! Perhaps you meant some other data structure?
11-
--> $DIR/dlist.rs:10:12
11+
--> $DIR/dlist.rs:10:15
1212
|
13-
LL | fn foo(LinkedList<u8>);
14-
| ^^^^^^^^^^^^^^
13+
LL | fn foo(_: LinkedList<u8>);
14+
| ^^^^^^^^^^^^^^
1515
|
1616
= help: a VecDeque might work
1717

tests/ui/if_same_then_else.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ fn if_same_then_else() -> Result<&'static str, ()> {
215215
};
216216

217217
if true {
218-
try!(Ok("foo"));
218+
r#try!(Ok("foo"));
219219
} else {
220220
//~ ERROR same body as `if` block
221-
try!(Ok("foo"));
221+
r#try!(Ok("foo"));
222222
}
223223

224224
if true {

tests/ui/if_same_then_else.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ error: this `if` has identical blocks
197197
LL | } else {
198198
| ____________^
199199
LL | | //~ ERROR same body as `if` block
200-
LL | | try!(Ok("foo"));
200+
LL | | r#try!(Ok("foo"));
201201
LL | | }
202202
| |_____^
203203
|
@@ -206,7 +206,7 @@ note: same as this
206206
|
207207
LL | if true {
208208
| _____________^
209-
LL | | try!(Ok("foo"));
209+
LL | | r#try!(Ok("foo"));
210210
LL | | } else {
211211
| |_____^
212212

tests/ui/issue_4266.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// compile-flags: --edition 2018
2+
#![feature(async_await)]
3+
#![allow(dead_code)]
4+
5+
async fn sink1<'a>(_: &'a str) {} // lint
6+
async fn sink1_elided(_: &str) {} // ok
7+
8+
// lint
9+
async fn one_to_one<'a>(s: &'a str) -> &'a str {
10+
s
11+
}
12+
13+
// ok
14+
async fn one_to_one_elided(s: &str) -> &str {
15+
s
16+
}
17+
18+
// ok
19+
async fn all_to_one<'a>(a: &'a str, _b: &'a str) -> &'a str {
20+
a
21+
}
22+
23+
// async fn unrelated(_: &str, _: &str) {} // Not allowed in async fn
24+
25+
// #3988
26+
struct Foo;
27+
impl Foo {
28+
// ok
29+
pub async fn foo(&mut self) {}
30+
}
31+
32+
// rust-lang/rust#61115
33+
// ok
34+
async fn print(s: &str) {
35+
println!("{}", s);
36+
}
37+
38+
fn main() {}

tests/ui/issue_4266.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
2+
--> $DIR/issue_4266.rs:5:1
3+
|
4+
LL | async fn sink1<'a>(_: &'a str) {} // lint
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::needless-lifetimes` implied by `-D warnings`
8+
9+
error: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
10+
--> $DIR/issue_4266.rs:9:1
11+
|
12+
LL | / async fn one_to_one<'a>(s: &'a str) -> &'a str {
13+
LL | | s
14+
LL | | }
15+
| |_^
16+
17+
error: aborting due to 2 previous errors
18+

tests/ui/unused_io_amount.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
use std::io;
55

66
fn try_macro<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
7-
try!(s.write(b"test"));
7+
r#try!(s.write(b"test"));
88
let mut buf = [0u8; 4];
9-
try!(s.read(&mut buf));
9+
r#try!(s.read(&mut buf));
1010
Ok(())
1111
}
1212

tests/ui/unused_io_amount.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error: handle written amount returned or use `Write::write_all` instead
22
--> $DIR/unused_io_amount.rs:7:5
33
|
4-
LL | try!(s.write(b"test"));
5-
| ^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | r#try!(s.write(b"test"));
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::unused-io-amount` implied by `-D warnings`
88
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
99

1010
error: handle read amount returned or use `Read::read_exact` instead
1111
--> $DIR/unused_io_amount.rs:9:5
1212
|
13-
LL | try!(s.read(&mut buf));
14-
| ^^^^^^^^^^^^^^^^^^^^^^^
13+
LL | r#try!(s.read(&mut buf));
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
1616
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
1717

0 commit comments

Comments
 (0)