Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
33 changed files
with
728 additions
and
169 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
Large diffs are not rendered by default.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
src/test/ui/in-band-lifetimes/single_use_lifetimes-2.stderr
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
src/test/ui/in-band-lifetimes/single_use_lifetimes-3.stderr
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
src/test/ui/in-band-lifetimes/single_use_lifetimes-4.stderr
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
src/test/ui/in-band-lifetimes/single_use_lifetimes-5.stderr
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,26 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![deny(single_use_lifetime)] | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
// Test that we DO warn when lifetime name is used only | ||
// once in a fn argument. | ||
|
||
struct Foo { | ||
a: for<'a> fn(&'a u32), //~ ERROR `'a` only used once | ||
b: for<'a> fn(&'a u32, &'a u32), // OK, used twice. | ||
c: for<'a> fn(&'a u32) -> &'a u32, // OK, used twice. | ||
d: for<'a> fn() -> &'a u32, // OK, used only in return type. | ||
//~^ ERROR return type references lifetime `'a`, which is not constrained by the fn input types | ||
} | ||
|
||
fn main() { } |
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,21 @@ | ||
error: lifetime parameter `'a` only used once | ||
--> $DIR/fn-types.rs:19:10 | ||
| | ||
LL | a: for<'a> fn(&'a u32), //~ ERROR `'a` only used once | ||
| ^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/fn-types.rs:11:9 | ||
| | ||
LL | #![deny(single_use_lifetime)] | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
error[E0581]: return type references lifetime `'a`, which is not constrained by the fn input types | ||
--> $DIR/fn-types.rs:22:22 | ||
| | ||
LL | d: for<'a> fn() -> &'a u32, // OK, used only in return type. | ||
| ^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0581`. |
24 changes: 24 additions & 0 deletions
24
src/test/ui/single-use-lifetime/one-use-in-fn-argument-in-band.rs
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,24 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(in_band_lifetimes)] | ||
#![deny(single_use_lifetime)] | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
// Test that we DO warn when lifetime name is used only | ||
// once in a fn argument, even with in band lifetimes. | ||
|
||
fn a(x: &'a u32, y: &'b u32) { | ||
//~^ ERROR `'a` only used once | ||
//~| ERROR `'b` only used once | ||
} | ||
|
||
fn main() { } |
20 changes: 20 additions & 0 deletions
20
src/test/ui/single-use-lifetime/one-use-in-fn-argument-in-band.stderr
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,20 @@ | ||
error: lifetime parameter `'b` only used once | ||
--> $DIR/one-use-in-fn-argument-in-band.rs:19:22 | ||
| | ||
LL | fn a(x: &'a u32, y: &'b u32) { | ||
| ^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/one-use-in-fn-argument-in-band.rs:12:9 | ||
| | ||
LL | #![deny(single_use_lifetime)] | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: lifetime parameter `'a` only used once | ||
--> $DIR/one-use-in-fn-argument-in-band.rs:19:10 | ||
| | ||
LL | fn a(x: &'a u32, y: &'b u32) { | ||
| ^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
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
14 changes: 14 additions & 0 deletions
14
src/test/ui/single-use-lifetime/one-use-in-fn-argument.stderr
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,14 @@ | ||
error: lifetime parameter `'a` only used once | ||
--> $DIR/one-use-in-fn-argument.rs:18:6 | ||
| | ||
LL | fn a<'a>(x: &'a u32) { //~ ERROR `'a` only used once | ||
| ^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/one-use-in-fn-argument.rs:11:9 | ||
| | ||
LL | #![deny(single_use_lifetime)] | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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,30 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// compile-pass | ||
|
||
#![deny(single_use_lifetime)] | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
// Test that we DO NOT warn when lifetime name is used only | ||
// once in a fn return type -- using `'_` is not legal there, | ||
// as it must refer back to an argument. | ||
// | ||
// (Normally, using `'static` would be preferred, but there are | ||
// times when that is not what you want.) | ||
// | ||
// run-pass | ||
|
||
fn b<'a>() -> &'a u32 { // OK: used only in return type | ||
&22 | ||
} | ||
|
||
fn main() { } |
29 changes: 29 additions & 0 deletions
29
src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.rs
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,29 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![deny(single_use_lifetime)] | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
// Test that we DO warn for a lifetime used only once in an impl. | ||
// | ||
// (Actually, until #15872 is fixed, you can't use `'_` here, but | ||
// hopefully that will come soon.) | ||
|
||
struct Foo<'f> { | ||
data: &'f u32 | ||
} | ||
|
||
impl<'f> Foo<'f> { //~ ERROR `'f` only used once | ||
fn inherent_a(&self) { | ||
} | ||
} | ||
|
||
fn main() { } |
14 changes: 14 additions & 0 deletions
14
src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.stderr
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,14 @@ | ||
error: lifetime parameter `'f` only used once | ||
--> $DIR/one-use-in-inherent-impl-header.rs:24:6 | ||
| | ||
LL | impl<'f> Foo<'f> { //~ ERROR `'f` only used once | ||
| ^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/one-use-in-inherent-impl-header.rs:11:9 | ||
| | ||
LL | #![deny(single_use_lifetime)] | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
26 changes: 26 additions & 0 deletions
26
src/test/ui/single-use-lifetime/one-use-in-inherent-method-argument.rs
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,26 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![deny(single_use_lifetime)] | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
// Test that we DO warn for a lifetime used only once in an inherent method. | ||
|
||
struct Foo<'f> { | ||
data: &'f u32 | ||
} | ||
|
||
impl<'f> Foo<'f> { //~ ERROR `'f` only used once | ||
fn inherent_a<'a>(&self, data: &'a u32) { //~ ERROR `'a` only used once | ||
} | ||
} | ||
|
||
fn main() { } |
20 changes: 20 additions & 0 deletions
20
src/test/ui/single-use-lifetime/one-use-in-inherent-method-argument.stderr
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,20 @@ | ||
error: lifetime parameter `'a` only used once | ||
--> $DIR/one-use-in-inherent-method-argument.rs:22:19 | ||
| | ||
LL | fn inherent_a<'a>(&self, data: &'a u32) { //~ ERROR `'a` only used once | ||
| ^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/one-use-in-inherent-method-argument.rs:11:9 | ||
| | ||
LL | #![deny(single_use_lifetime)] | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: lifetime parameter `'f` only used once | ||
--> $DIR/one-use-in-inherent-method-argument.rs:21:6 | ||
| | ||
LL | impl<'f> Foo<'f> { //~ ERROR `'f` only used once | ||
| ^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
28 changes: 28 additions & 0 deletions
28
src/test/ui/single-use-lifetime/one-use-in-inherent-method-return.rs
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,28 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![deny(single_use_lifetime)] | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
|
||
// Test that we DO NOT warn for a lifetime used just once in a return type, | ||
// where that return type is in an inherent method. | ||
|
||
struct Foo<'f> { | ||
data: &'f u32 | ||
} | ||
|
||
impl<'f> Foo<'f> { //~ ERROR `'f` only used once | ||
fn inherent_a<'a>(&self) -> &'a u32 { // OK for 'a | ||
&22 | ||
} | ||
} | ||
|
||
fn main() { } |
Oops, something went wrong.