]> git.lizzy.rs Git - rust.git/commitdiff
Consider lifetime in self paramter in unused_lifetime lint
authorFlorian Hartwig <florian.j.hartwig@gmail.com>
Thu, 14 Jan 2016 18:27:24 +0000 (19:27 +0100)
committerFlorian Hartwig <florian.j.hartwig@gmail.com>
Thu, 14 Jan 2016 18:27:24 +0000 (19:27 +0100)
src/lifetimes.rs
tests/compile-fail/lifetimes.rs
tests/compile-fail/unused_lt.rs

index 2de916cfebc4e7b1146ff33b62aafb41dfe32477..1edd75a45aa162aaf323f900d4c2a375b7219a0c 100644 (file)
@@ -77,7 +77,7 @@ fn check_fn_inner(cx: &LateContext, decl: &FnDecl, slf: Option<&ExplicitSelf>, g
                   span,
                   "explicit lifetimes given in parameter types where they could be elided");
     }
-    report_extra_lifetimes(cx, decl, &generics);
+    report_extra_lifetimes(cx, decl, &generics, slf);
 }
 
 fn could_use_elision(cx: &LateContext, func: &FnDecl, slf: Option<&ExplicitSelf>, named_lts: &[LifetimeDef]) -> bool {
@@ -303,14 +303,25 @@ fn visit_lifetime_def(&mut self, _: &'v LifetimeDef) {
     }
 }
 
-fn report_extra_lifetimes(cx: &LateContext, func: &FnDecl, generics: &Generics) {
+fn report_extra_lifetimes(cx: &LateContext, func: &FnDecl,
+                          generics: &Generics, slf: Option<&ExplicitSelf>) {
     let hs = generics.lifetimes
                      .iter()
                      .map(|lt| (lt.lifetime.name, lt.lifetime.span))
                      .collect();
     let mut checker = LifetimeChecker(hs);
+
     walk_generics(&mut checker, generics);
     walk_fn_decl(&mut checker, func);
+
+    if let Some(slf) = slf {
+        match slf.node {
+            SelfRegion(Some(ref lt), _, _) => checker.visit_lifetime(lt),
+            SelfExplicit(ref t, _) => walk_ty(&mut checker, t),
+            _ => {}
+        }
+    }
+
     for (_, v) in checker.0 {
         span_lint(cx, UNUSED_LIFETIMES, v, "this lifetime isn't used in the function definition");
     }
index 4d454a738d99908792b97232552531f1595af96f..99c16917426ed9f5ad41bb130bbd650b2f01bffb 100644 (file)
@@ -1,8 +1,8 @@
 #![feature(plugin)]
 #![plugin(clippy)]
 
-#![deny(needless_lifetimes)]
-#![allow(dead_code, unused_lifetimes)]
+#![deny(needless_lifetimes, unused_lifetimes)]
+#![allow(dead_code)]
 fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) { }
 //~^ERROR explicit lifetimes given
 
index 85667174509e2f96f5e0468df97edeb2331a80b6..a35718b5848ffd16399b7b69292326b949fc862a 100644 (file)
@@ -52,6 +52,13 @@ pub fn parse2<'a, I>(_it: &mut I) where I: Iterator<Item=&'a str>{
     unimplemented!()
 }
 
+struct X { x: u32 }
+
+impl X {
+    fn self_ref_with_lifetime<'a>(&'a self) {}
+    fn explicit_self_with_lifetime<'a>(self: &'a Self) {}
+}
+
 fn main() {
 
 }