Skip to content

Commit 4e5a155

Browse files
committed
Auto merge of rust-lang#50440 - nikomatsakis:single-use-lifetimes, r=cramertj
Improve single-use and zero-use lifetime lints The code now correctly identifies *when* to lint -- or more correctly, anyhow -- but it doesn't yet offer suggestions for how to fix. (I just remembered when writing this I had meant to go back over some of these cases around e.g. impl Trait and double check that everything is right...) cc rust-lang#44752 r? @cramertj
2 parents a006328 + 6f98ee9 commit 4e5a155

33 files changed

+728
-169
lines changed

src/librustc/lint/builtin.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ declare_lint! {
233233
declare_lint! {
234234
pub SINGLE_USE_LIFETIME,
235235
Allow,
236-
"detects single use lifetimes"
236+
"detects lifetime parameters that are only used once"
237+
}
238+
239+
declare_lint! {
240+
pub UNUSED_LIFETIME,
241+
Allow,
242+
"detects lifetime parameters that are never used"
237243
}
238244

239245
declare_lint! {
@@ -318,6 +324,7 @@ impl LintPass for HardwiredLints {
318324
UNUSED_UNSAFE,
319325
UNUSED_MUT,
320326
SINGLE_USE_LIFETIME,
327+
UNUSED_LIFETIME,
321328
TYVAR_BEHIND_RAW_POINTER,
322329
ELIDED_LIFETIME_IN_PATH,
323330
BARE_TRAIT_OBJECT,

src/librustc/middle/resolve_lifetime.rs

Lines changed: 200 additions & 60 deletions
Large diffs are not rendered by default.

src/test/ui/in-band-lifetimes/single_use_lifetimes-2.stderr

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/test/ui/in-band-lifetimes/single_use_lifetimes-3.stderr

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/test/ui/in-band-lifetimes/single_use_lifetimes-4.stderr

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/test/ui/in-band-lifetimes/single_use_lifetimes-5.stderr

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/test/ui/in-band-lifetimes/single_use_lifetimes.stderr

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(single_use_lifetime)]
12+
#![allow(dead_code)]
13+
#![allow(unused_variables)]
14+
15+
// Test that we DO warn when lifetime name is used only
16+
// once in a fn argument.
17+
18+
struct Foo {
19+
a: for<'a> fn(&'a u32), //~ ERROR `'a` only used once
20+
b: for<'a> fn(&'a u32, &'a u32), // OK, used twice.
21+
c: for<'a> fn(&'a u32) -> &'a u32, // OK, used twice.
22+
d: for<'a> fn() -> &'a u32, // OK, used only in return type.
23+
//~^ ERROR return type references lifetime `'a`, which is not constrained by the fn input types
24+
}
25+
26+
fn main() { }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: lifetime parameter `'a` only used once
2+
--> $DIR/fn-types.rs:19:10
3+
|
4+
LL | a: for<'a> fn(&'a u32), //~ ERROR `'a` only used once
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/fn-types.rs:11:9
9+
|
10+
LL | #![deny(single_use_lifetime)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0581]: return type references lifetime `'a`, which is not constrained by the fn input types
14+
--> $DIR/fn-types.rs:22:22
15+
|
16+
LL | d: for<'a> fn() -> &'a u32, // OK, used only in return type.
17+
| ^^^^^^^
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0581`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(in_band_lifetimes)]
12+
#![deny(single_use_lifetime)]
13+
#![allow(dead_code)]
14+
#![allow(unused_variables)]
15+
16+
// Test that we DO warn when lifetime name is used only
17+
// once in a fn argument, even with in band lifetimes.
18+
19+
fn a(x: &'a u32, y: &'b u32) {
20+
//~^ ERROR `'a` only used once
21+
//~| ERROR `'b` only used once
22+
}
23+
24+
fn main() { }

0 commit comments

Comments
 (0)