Skip to content

Commit 4e5a155

Browse files
committed
Auto merge of #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 #44752 r? @cramertj
2 parents a006328 + 6f98ee9 commit 4e5a155

33 files changed

+728
-169
lines changed

src/librustc/lint/builtin.rs

+8-1
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

+200-60
Large diffs are not rendered by default.

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

-14
This file was deleted.

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

-20
This file was deleted.

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

-20
This file was deleted.

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

-14
This file was deleted.

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

-14
This file was deleted.
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() { }
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`.
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() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: lifetime parameter `'b` only used once
2+
--> $DIR/one-use-in-fn-argument-in-band.rs:19:22
3+
|
4+
LL | fn a(x: &'a u32, y: &'b u32) {
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/one-use-in-fn-argument-in-band.rs:12:9
9+
|
10+
LL | #![deny(single_use_lifetime)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: lifetime parameter `'a` only used once
14+
--> $DIR/one-use-in-fn-argument-in-band.rs:19:10
15+
|
16+
LL | fn a(x: &'a u32, y: &'b u32) {
17+
| ^^
18+
19+
error: aborting due to 2 previous errors
20+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -7,10 +7,15 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10+
1011
#![deny(single_use_lifetime)]
11-
// FIXME(#44752) -- this scenario should not be warned
12-
fn deref<'x>() -> &'x u32 { //~ ERROR lifetime name `'x` only used once
13-
22
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+
fn a<'a>(x: &'a u32) { //~ ERROR `'a` only used once
1419
}
1520

1621
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: lifetime parameter `'a` only used once
2+
--> $DIR/one-use-in-fn-argument.rs:18:6
3+
|
4+
LL | fn a<'a>(x: &'a u32) { //~ ERROR `'a` only used once
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/one-use-in-fn-argument.rs:11:9
9+
|
10+
LL | #![deny(single_use_lifetime)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
// compile-pass
12+
13+
#![deny(single_use_lifetime)]
14+
#![allow(dead_code)]
15+
#![allow(unused_variables)]
16+
17+
// Test that we DO NOT warn when lifetime name is used only
18+
// once in a fn return type -- using `'_` is not legal there,
19+
// as it must refer back to an argument.
20+
//
21+
// (Normally, using `'static` would be preferred, but there are
22+
// times when that is not what you want.)
23+
//
24+
// run-pass
25+
26+
fn b<'a>() -> &'a u32 { // OK: used only in return type
27+
&22
28+
}
29+
30+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 for a lifetime used only once in an impl.
16+
//
17+
// (Actually, until #15872 is fixed, you can't use `'_` here, but
18+
// hopefully that will come soon.)
19+
20+
struct Foo<'f> {
21+
data: &'f u32
22+
}
23+
24+
impl<'f> Foo<'f> { //~ ERROR `'f` only used once
25+
fn inherent_a(&self) {
26+
}
27+
}
28+
29+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: lifetime parameter `'f` only used once
2+
--> $DIR/one-use-in-inherent-impl-header.rs:24:6
3+
|
4+
LL | impl<'f> Foo<'f> { //~ ERROR `'f` only used once
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/one-use-in-inherent-impl-header.rs:11:9
9+
|
10+
LL | #![deny(single_use_lifetime)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+
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 for a lifetime used only once in an inherent method.
16+
17+
struct Foo<'f> {
18+
data: &'f u32
19+
}
20+
21+
impl<'f> Foo<'f> { //~ ERROR `'f` only used once
22+
fn inherent_a<'a>(&self, data: &'a u32) { //~ ERROR `'a` only used once
23+
}
24+
}
25+
26+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: lifetime parameter `'a` only used once
2+
--> $DIR/one-use-in-inherent-method-argument.rs:22:19
3+
|
4+
LL | fn inherent_a<'a>(&self, data: &'a u32) { //~ ERROR `'a` only used once
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/one-use-in-inherent-method-argument.rs:11:9
9+
|
10+
LL | #![deny(single_use_lifetime)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: lifetime parameter `'f` only used once
14+
--> $DIR/one-use-in-inherent-method-argument.rs:21:6
15+
|
16+
LL | impl<'f> Foo<'f> { //~ ERROR `'f` only used once
17+
| ^^
18+
19+
error: aborting due to 2 previous errors
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 NOT warn for a lifetime used just once in a return type,
16+
// where that return type is in an inherent method.
17+
18+
struct Foo<'f> {
19+
data: &'f u32
20+
}
21+
22+
impl<'f> Foo<'f> { //~ ERROR `'f` only used once
23+
fn inherent_a<'a>(&self) -> &'a u32 { // OK for 'a
24+
&22
25+
}
26+
}
27+
28+
fn main() { }

0 commit comments

Comments
 (0)