Skip to content

Commit 0a9dcaf

Browse files
committed
add warnings for unused lifetime parameters
1 parent aef29a0 commit 0a9dcaf

File tree

6 files changed

+95
-2
lines changed

6 files changed

+95
-2
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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,25 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13581358
Some(LifetimeUseSet::Many) => {
13591359
debug!("Not one use lifetime");
13601360
}
1361-
None => {}
1361+
None => {
1362+
let node_id = self.tcx.hir.as_local_node_id(*def_id).unwrap();
1363+
if let hir::map::NodeLifetime(hir_lifetime) = self.tcx.hir.get(node_id) {
1364+
let span = hir_lifetime.span;
1365+
let id = hir_lifetime.id;
1366+
1367+
self.tcx
1368+
.struct_span_lint_node(
1369+
lint::builtin::UNUSED_LIFETIME,
1370+
id,
1371+
span,
1372+
&format!(
1373+
"lifetime parameter `{}` never used",
1374+
hir_lifetime.name.name()
1375+
),
1376+
)
1377+
.emit();
1378+
}
1379+
}
13621380
}
13631381
}
13641382
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
// Test that we DO warn when lifetime name is not used at all.
12+
13+
#![deny(unused_lifetime)]
14+
#![allow(dead_code)]
15+
#![allow(unused_variables)]
16+
17+
fn d<'a>() { } //~ ERROR `'a` never used
18+
19+
fn main() { }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: lifetime parameter `'a` never used
2+
--> $DIR/zero-uses-in-fn.rs:17:6
3+
|
4+
LL | fn d<'a>() { } //~ ERROR `'a` never used
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/zero-uses-in-fn.rs:13:9
9+
|
10+
LL | #![deny(unused_lifetime)]
11+
| ^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// Test that we DO warn when lifetime name is not used at all.
12+
13+
#![deny(unused_lifetime)]
14+
#![allow(dead_code)]
15+
#![allow(unused_variables)]
16+
17+
struct Foo { }
18+
19+
impl<'a> Foo { } //~ ERROR `'a` never used
20+
21+
fn main() { }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: lifetime parameter `'a` never used
2+
--> $DIR/zero-uses-in-impl.rs:19:6
3+
|
4+
LL | impl<'a> Foo { } //~ ERROR `'a` never used
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/zero-uses-in-impl.rs:13:9
9+
|
10+
LL | #![deny(unused_lifetime)]
11+
| ^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)