From faea5bbc650d93e28e4a89a62aee0dc1409b08c7 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Sun, 28 Oct 2018 00:13:18 -0700 Subject: [PATCH] single-use-lifetime lint: don't false-positive on the anonymous lifetime --- src/librustc/middle/resolve_lifetime.rs | 5 +++++ .../one-use-in-inherent-impl-header.rs | 11 +++++++---- .../one-use-in-inherent-impl-header.stderr | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 67a564299b0..31e34895052 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -1593,6 +1593,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { _ => None, } { debug!("id = {:?} span = {:?} name = {:?}", node_id, span, name); + + if name == keywords::UnderscoreLifetime.ident() { + continue; + } + let mut err = self.tcx.struct_span_lint_node( lint::builtin::SINGLE_USE_LIFETIMES, id, diff --git a/src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.rs b/src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.rs index a862bbbe30c..b392f7a5174 100644 --- a/src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.rs +++ b/src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.rs @@ -12,10 +12,8 @@ #![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.) +// Test that we DO warn for a lifetime used only once in an impl, and that we +// don't warn for the anonymous lifetime. struct Foo<'f> { data: &'f u32 @@ -26,4 +24,9 @@ impl<'f> Foo<'f> { //~ ERROR `'f` only used once } } +impl Foo<'_> { + fn inherent_b(&self) {} +} + + fn main() { } diff --git a/src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.stderr b/src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.stderr index 2509366f969..40dfa677d0a 100644 --- a/src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.stderr +++ b/src/test/ui/single-use-lifetime/one-use-in-inherent-impl-header.stderr @@ -1,5 +1,5 @@ error: lifetime parameter `'f` only used once - --> $DIR/one-use-in-inherent-impl-header.rs:24:6 + --> $DIR/one-use-in-inherent-impl-header.rs:22:6 | LL | impl<'f> Foo<'f> { //~ ERROR `'f` only used once | ^^ -- ...is used only here