Skip to content

Commit 7e09990

Browse files
committed
Update to handle GenericParam
introduced in rust-lang/rust#45930
1 parent ebcdf03 commit 7e09990

File tree

5 files changed

+31
-25
lines changed

5 files changed

+31
-25
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use reexport::*;
22
use rustc::lint::*;
33
use rustc::hir::def::Def;
44
use rustc::hir::*;
5-
use rustc::hir::intravisit::{walk_fn_decl, walk_generics, walk_ty, walk_ty_param_bound, NestedVisitorMap, Visitor};
5+
use rustc::hir::intravisit::*;
66
use std::collections::{HashMap, HashSet};
77
use syntax::codemap::Span;
88
use utils::{in_external_macro, last_path_segment, span_lint};
@@ -101,7 +101,7 @@ fn check_fn_inner<'a, 'tcx>(
101101
}
102102

103103
let mut bounds_lts = Vec::new();
104-
for typ in &generics.ty_params {
104+
for typ in generics.ty_params() {
105105
for bound in &typ.bounds {
106106
if let TraitTyParamBound(ref trait_ref, _) = *bound {
107107
let params = &trait_ref
@@ -122,7 +122,7 @@ fn check_fn_inner<'a, 'tcx>(
122122
}
123123
}
124124
}
125-
if could_use_elision(cx, decl, body, &generics.lifetimes, bounds_lts) {
125+
if could_use_elision(cx, decl, body, &generics.params, bounds_lts) {
126126
span_lint(
127127
cx,
128128
NEEDLESS_LIFETIMES,
@@ -137,7 +137,7 @@ fn could_use_elision<'a, 'tcx: 'a>(
137137
cx: &LateContext<'a, 'tcx>,
138138
func: &'tcx FnDecl,
139139
body: Option<BodyId>,
140-
named_lts: &'tcx [LifetimeDef],
140+
named_generics: &'tcx [GenericParam],
141141
bounds_lts: Vec<&'tcx Lifetime>,
142142
) -> bool {
143143
// There are two scenarios where elision works:
@@ -147,7 +147,7 @@ fn could_use_elision<'a, 'tcx: 'a>(
147147
// level of the current item.
148148

149149
// check named LTs
150-
let allowed_lts = allowed_lts_from(named_lts);
150+
let allowed_lts = allowed_lts_from(named_generics);
151151

152152
// these will collect all the lifetimes for references in arg/return types
153153
let mut input_visitor = RefVisitor::new(cx);
@@ -222,11 +222,13 @@ fn could_use_elision<'a, 'tcx: 'a>(
222222
}
223223
}
224224

225-
fn allowed_lts_from(named_lts: &[LifetimeDef]) -> HashSet<RefLt> {
225+
fn allowed_lts_from(named_generics: &[GenericParam]) -> HashSet<RefLt> {
226226
let mut allowed_lts = HashSet::new();
227-
for lt in named_lts {
228-
if lt.bounds.is_empty() {
229-
allowed_lts.insert(RefLt::Named(lt.lifetime.name.name()));
227+
for par in named_generics.iter() {
228+
if let GenericParam::Lifetime(ref lt) = *par {
229+
if lt.bounds.is_empty() {
230+
allowed_lts.insert(RefLt::Named(lt.lifetime.name.name()));
231+
}
230232
}
231233
}
232234
allowed_lts.insert(RefLt::Unnamed);
@@ -370,7 +372,7 @@ fn has_where_lifetimes<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, where_clause: &
370372
return true;
371373
}
372374
// if the bounds define new lifetimes, they are fine to occur
373-
let allowed_lts = allowed_lts_from(&pred.bound_lifetimes);
375+
let allowed_lts = allowed_lts_from(&pred.bound_generic_params);
374376
// now walk the bounds
375377
for bound in pred.bounds.iter() {
376378
walk_ty_param_bound(&mut visitor, bound);
@@ -408,12 +410,15 @@ impl<'tcx> Visitor<'tcx> for LifetimeChecker {
408410
self.map.remove(&lifetime.name.name());
409411
}
410412

411-
fn visit_lifetime_def(&mut self, _: &'tcx LifetimeDef) {
413+
fn visit_generic_param(&mut self, param: &'tcx GenericParam) {
412414
// don't actually visit `<'a>` or `<'a: 'b>`
413415
// we've already visited the `'a` declarations and
414416
// don't want to spuriously remove them
415417
// `'b` in `'a: 'b` is useless unless used elsewhere in
416418
// a non-lifetime bound
419+
if param.is_type_param() {
420+
walk_generic_param(self, param)
421+
}
417422
}
418423
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
419424
NestedVisitorMap::None
@@ -422,8 +427,7 @@ impl<'tcx> Visitor<'tcx> for LifetimeChecker {
422427

423428
fn report_extra_lifetimes<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, func: &'tcx FnDecl, generics: &'tcx Generics) {
424429
let hs = generics
425-
.lifetimes
426-
.iter()
430+
.lifetimes()
427431
.map(|lt| (lt.lifetime.name.name(), lt.lifetime.span))
428432
.collect();
429433
let mut checker = LifetimeChecker { map: hs };

clippy_lints/src/methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ impl SelfKind {
18491849

18501850
fn is_as_ref_or_mut_trait(ty: &hir::Ty, self_ty: &hir::Ty, generics: &hir::Generics, name: &[&str]) -> bool {
18511851
single_segment_ty(ty).map_or(false, |seg| {
1852-
generics.ty_params.iter().any(|param| {
1852+
generics.ty_params().any(|param| {
18531853
param.name == seg.name && param.bounds.iter().any(|bound| {
18541854
if let hir::TyParamBound::TraitTyParamBound(ref ptr, ..) = *bound {
18551855
let path = &ptr.trait_ref.path;

clippy_lints/src/misc_early.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,17 @@ impl LintPass for MiscEarly {
188188

189189
impl EarlyLintPass for MiscEarly {
190190
fn check_generics(&mut self, cx: &EarlyContext, gen: &Generics) {
191-
for ty in &gen.ty_params {
192-
let name = ty.ident.name.as_str();
193-
if constants::BUILTIN_TYPES.contains(&&*name) {
194-
span_lint(
195-
cx,
196-
BUILTIN_TYPE_SHADOW,
197-
ty.span,
198-
&format!("This generic shadows the built-in type `{}`", name),
199-
);
191+
for param in &gen.params {
192+
if let GenericParam::Type(ref ty) = *param {
193+
let name = ty.ident.name.as_str();
194+
if constants::BUILTIN_TYPES.contains(&&*name) {
195+
span_lint(
196+
cx,
197+
BUILTIN_TYPE_SHADOW,
198+
ty.span,
199+
&format!("This generic shadows the built-in type `{}`", name),
200+
);
201+
}
200202
}
201203
}
202204
}

clippy_lints/src/new_without_default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
103103
// can't be implemented by default
104104
return;
105105
}
106-
if !impl_item.generics.ty_params.is_empty() {
106+
if impl_item.generics.params.iter().any(|gen| gen.is_type_param()) {
107107
// when the result of `new()` depends on a type parameter we should not require
108108
// an
109109
// impl of `Default`

clippy_lints/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ impl<'tcx> Visitor<'tcx> for TypeComplexityVisitor {
957957
TyTraitObject(ref param_bounds, _) => {
958958
let has_lifetime_parameters = param_bounds
959959
.iter()
960-
.any(|bound| !bound.bound_lifetimes.is_empty());
960+
.any(|bound| bound.bound_generic_params.iter().any(|gen| gen.is_lifetime_param()));
961961
if has_lifetime_parameters {
962962
// complex trait bounds like A<'a, 'b>
963963
(50 * self.nest, 1)

0 commit comments

Comments
 (0)