Merge pull request #481 from fhartwig/lifetime-with-alias

Make unneeded_lifetimes lint work properly with type aliases
This commit is contained in:
Manish Goregaokar 2015-12-06 09:08:40 +05:30
commit c4e9982dd7
3 changed files with 27 additions and 16 deletions

View File

@ -3,7 +3,7 @@ use reexport::*;
use rustc::lint::*;
use syntax::codemap::Span;
use rustc_front::intravisit::{Visitor, walk_ty, walk_ty_param_bound};
use rustc::middle::def::Def::{DefTy, DefTrait};
use rustc::middle::def::Def::{DefTy, DefTrait, DefStruct};
use std::collections::HashSet;
use utils::{in_external_macro, span_lint};
@ -186,23 +186,22 @@ impl <'v, 't> RefVisitor<'v, 't> {
let last_path_segment = path.segments.last().map(|s| &s.parameters);
if let Some(&AngleBracketedParameters(ref params)) = last_path_segment {
if params.lifetimes.is_empty() {
let def = self.cx.tcx.def_map.borrow().get(&ty.id).map(|r| r.full_def());
match def {
Some(DefTy(def_id, _)) => {
if let Some(ty_def) = self.cx.tcx.adt_defs.borrow().get(&def_id) {
let scheme = ty_def.type_scheme(self.cx.tcx);
for _ in scheme.generics.regions.as_slice() {
if let Some(def) = self.cx.tcx.def_map.borrow().get(&ty.id).map(|r| r.full_def()) {
match def {
DefTy(def_id, _) | DefStruct(def_id) => {
let type_scheme = self.cx.tcx.lookup_item_type(def_id);
for _ in type_scheme.generics.regions.as_slice() {
self.record(&None);
}
}
},
DefTrait(def_id) => {
let trait_def = self.cx.tcx.trait_defs.borrow()[&def_id];
for _ in &trait_def.generics.regions {
self.record(&None);
}
},
_ => {}
}
Some(DefTrait(def_id)) => {
let trait_def = self.cx.tcx.trait_defs.borrow()[&def_id];
for _ in &trait_def.generics.regions {
self.record(&None);
}
}
_ => {}
}
}
}

View File

@ -259,7 +259,6 @@ pub fn get_parent_expr<'c>(cx: &'c LateContext, e: &Expr) -> Option<&'c Expr> {
if let NodeExpr(parent) = node { Some(parent) } else { None } )
}
#[allow(needless_lifetimes)] // workaround for https://github.com/Manishearth/rust-clippy/issues/417
pub fn get_enclosing_block<'c>(cx: &'c LateContext, node: NodeId) -> Option<&'c Block> {
let map = &cx.tcx.map;
let enclosing_node = map.get_enclosing_scope(node)

View File

@ -106,5 +106,18 @@ fn trait_obj_elided<'a>(_arg: &'a WithLifetime) -> &'a str { unimplemented!() }
// unambiguous if we elided the lifetime
fn trait_obj_elided2<'a>(_arg: &'a Drop) -> &'a str { unimplemented!() } //~ERROR explicit lifetimes given
type FooAlias<'a> = Foo<'a>;
fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str { unimplemented!() } //~ERROR explicit lifetimes given
// no warning, two input lifetimes (named on the reference, anonymous on Foo)
fn alias_with_lt2<'a>(_foo: &'a FooAlias) -> &'a str { unimplemented!() }
// no warning, two input lifetimes (anonymous on the reference, named on Foo)
fn alias_with_lt3<'a>(_foo: &FooAlias<'a> ) -> &'a str { unimplemented!() }
// no warning, two input lifetimes
fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b> ) -> &'a str { unimplemented!() }
fn main() {
}