From 70c8fe5539f6a7888e631f2b1a0e59be7260389f Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Fri, 25 Aug 2017 17:30:21 +1000 Subject: [PATCH 1/2] fix PathParameters usage --- clippy_lints/src/lifetimes.rs | 40 ++++++++++------------ clippy_lints/src/methods.rs | 8 ++--- clippy_lints/src/needless_pass_by_value.rs | 2 +- clippy_lints/src/transmute.rs | 4 +-- clippy_lints/src/types.rs | 14 ++++---- clippy_lints/src/use_self.rs | 18 +++++----- clippy_lints/src/utils/hir_utils.rs | 15 ++++---- 7 files changed, 48 insertions(+), 53 deletions(-) diff --git a/clippy_lints/src/lifetimes.rs b/clippy_lints/src/lifetimes.rs index 47cc41c47..4cb386471 100644 --- a/clippy_lints/src/lifetimes.rs +++ b/clippy_lints/src/lifetimes.rs @@ -104,14 +104,14 @@ fn check_fn_inner<'a, 'tcx>( for typ in &generics.ty_params { for bound in &typ.bounds { if let TraitTyParamBound(ref trait_ref, _) = *bound { - let bounds = trait_ref + let bounds = &trait_ref .trait_ref .path .segments .last() .expect("a path must have at least one segment") .parameters - .lifetimes(); + .lifetimes; for bound in bounds { if bound.name != "'static" && !bound.is_elided() { return; @@ -282,25 +282,23 @@ impl<'v, 't> RefVisitor<'v, 't> { fn collect_anonymous_lifetimes(&mut self, qpath: &QPath, ty: &Ty) { let last_path_segment = &last_path_segment(qpath).parameters; - if let AngleBracketedParameters(ref params) = *last_path_segment { - if params.lifetimes.is_empty() { - let hir_id = self.cx.tcx.hir.node_to_hir_id(ty.id); - match self.cx.tables.qpath_def(qpath, hir_id) { - Def::TyAlias(def_id) | - Def::Struct(def_id) => { - let generics = self.cx.tcx.generics_of(def_id); - for _ in generics.regions.as_slice() { - self.record(&None); - } - }, - Def::Trait(def_id) => { - let trait_def = self.cx.tcx.trait_def(def_id); - for _ in &self.cx.tcx.generics_of(trait_def.def_id).regions { - self.record(&None); - } - }, - _ => (), - } + if !last_path_segment.parenthesized && last_path_segment.lifetimes.is_empty() { + let hir_id = self.cx.tcx.hir.node_to_hir_id(ty.id); + match self.cx.tables.qpath_def(qpath, hir_id) { + Def::TyAlias(def_id) | + Def::Struct(def_id) => { + let generics = self.cx.tcx.generics_of(def_id); + for _ in generics.regions.as_slice() { + self.record(&None); + } + }, + Def::Trait(def_id) => { + let trait_def = self.cx.tcx.trait_def(def_id); + for _ in &self.cx.tcx.generics_of(trait_def.def_id).regions { + self.record(&None); + } + }, + _ => (), } } } diff --git a/clippy_lints/src/methods.rs b/clippy_lints/src/methods.rs index a50d77f55..bb3ab9204 100644 --- a/clippy_lints/src/methods.rs +++ b/clippy_lints/src/methods.rs @@ -1464,11 +1464,11 @@ fn is_as_ref_or_mut_trait(ty: &hir::Ty, self_ty: &hir::Ty, generics: &hir::Gener let path = &ptr.trait_ref.path; match_path_old(path, name) && path.segments.last().map_or(false, |s| { - if let hir::PathParameters::AngleBracketedParameters(ref data) = s.parameters { - data.types.len() == 1 && - (is_self_ty(&data.types[0]) || is_ty(&*data.types[0], self_ty)) - } else { + if s.parameters.parenthesized { false + } else { + s.parameters.types.len() == 1 && + (is_self_ty(&s.parameters.types[0]) || is_ty(&*s.parameters.types[0], self_ty)) } }) } else { diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index b7de586ed..15ebe6485 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -147,7 +147,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { let TyPath(QPath::Resolved(_, ref path)) = input.node, let Some(elem_ty) = path.segments.iter() .find(|seg| seg.name == "Vec") - .map(|ps| ps.parameters.types()[0]), + .map(|ps| &ps.parameters.types[0]), ], { let slice_ty = format!("&[{}]", snippet(cx, elem_ty.span, "_")); db.span_suggestion(input.span, diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs index bcb03c792..83e212c36 100644 --- a/clippy_lints/src/transmute.rs +++ b/clippy_lints/src/transmute.rs @@ -208,8 +208,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute { fn get_type_snippet(cx: &LateContext, path: &QPath, to_rty: Ty) -> String { let seg = last_path_segment(path); if_let_chain!{[ - let PathParameters::AngleBracketedParameters(ref ang) = seg.parameters, - let Some(to_ty) = ang.types.get(1), + !seg.parameters.parenthesized, + let Some(to_ty) = seg.parameters.types.get(1), let TyRptr(_, ref to_ty) = to_ty.node, ], { return snippet(cx, to_ty.ty.span, &to_rty.to_string()).to_string(); diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index e48f6a904..7c68eab80 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -155,8 +155,8 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) { if Some(def_id) == cx.tcx.lang_items.owned_box() { let last = last_path_segment(qpath); if_let_chain! {[ - let PathParameters::AngleBracketedParameters(ref ag) = last.parameters, - let Some(vec) = ag.types.get(0), + !last.parameters.parenthesized, + let Some(vec) = last.parameters.types.get(0), let TyPath(ref qpath) = vec.node, let Some(did) = opt_def_id(cx.tables.qpath_def(qpath, cx.tcx.hir.node_to_hir_id(vec.id))), match_def_path(cx.tcx, did, &paths::VEC), @@ -182,18 +182,18 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) { match *qpath { QPath::Resolved(Some(ref ty), ref p) => { check_ty(cx, ty, is_local); - for ty in p.segments.iter().flat_map(|seg| seg.parameters.types()) { + for ty in p.segments.iter().flat_map(|seg| seg.parameters.types.iter()) { check_ty(cx, ty, is_local); } }, QPath::Resolved(None, ref p) => { - for ty in p.segments.iter().flat_map(|seg| seg.parameters.types()) { + for ty in p.segments.iter().flat_map(|seg| seg.parameters.types.iter()) { check_ty(cx, ty, is_local); } }, QPath::TypeRelative(ref ty, ref seg) => { check_ty(cx, ty, is_local); - for ty in seg.parameters.types() { + for ty in seg.parameters.types.iter() { check_ty(cx, ty, is_local); } }, @@ -209,8 +209,8 @@ fn check_ty(cx: &LateContext, ast_ty: &hir::Ty, is_local: bool) { Some(def_id) == cx.tcx.lang_items.owned_box(), let QPath::Resolved(None, ref path) = *qpath, let [ref bx] = *path.segments, - let PathParameters::AngleBracketedParameters(ref ab_data) = bx.parameters, - let [ref inner] = *ab_data.types + !bx.parameters.parenthesized, + let [ref inner] = *bx.parameters.types ], { if is_any_trait(inner) { // Ignore `Box` types, see #1884 for details. diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index b8c970d37..fffeb3bb6 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -56,16 +56,16 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf { if_let_chain!([ let ItemImpl(.., ref item_type, ref refs) = item.node, let Ty_::TyPath(QPath::Resolved(_, ref item_path)) = item_type.node, - let PathParameters::AngleBracketedParameters(ref param_data) - = item_path.segments.last().expect(SEGMENTS_MSG).parameters, - param_data.lifetimes.len() == 0, ], { - let visitor = &mut UseSelfVisitor { - item_path: item_path, - cx: cx, - }; - for impl_item_ref in refs { - visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id)); + let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).parameters; + if !parameters.parenthesized && parameters.lifetimes.len() == 0 { + let visitor = &mut UseSelfVisitor { + item_path: item_path, + cx: cx, + }; + for impl_item_ref in refs { + visitor.visit_impl_item(cx.tcx.hir.impl_item(impl_item_ref.id)); + } } }) } diff --git a/clippy_lints/src/utils/hir_utils.rs b/clippy_lints/src/utils/hir_utils.rs index b4caad084..4890bb81d 100644 --- a/clippy_lints/src/utils/hir_utils.rs +++ b/clippy_lints/src/utils/hir_utils.rs @@ -195,18 +195,15 @@ impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> { } fn eq_path_parameters(&self, left: &PathParameters, right: &PathParameters) -> bool { - match (left, right) { - (&AngleBracketedParameters(ref left), &AngleBracketedParameters(ref right)) => { + if !(left.parenthesized || right.parenthesized) { over(&left.lifetimes, &right.lifetimes, |l, r| self.eq_lifetime(l, r)) && over(&left.types, &right.types, |l, r| self.eq_ty(l, r)) && over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r)) - }, - (&ParenthesizedParameters(ref left), &ParenthesizedParameters(ref right)) => { - over(&left.inputs, &right.inputs, |l, r| self.eq_ty(l, r)) && - both(&left.output, &right.output, |l, r| self.eq_ty(l, r)) - }, - (&AngleBracketedParameters(_), &ParenthesizedParameters(_)) | - (&ParenthesizedParameters(_), &AngleBracketedParameters(_)) => false, + } else if left.parenthesized && right.parenthesized { + over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r)) && + both(&Some(&left.bindings[0].ty), &Some(&right.bindings[0].ty), |l, r| self.eq_ty(l, r)) + } else { + false } } From bec2c68ebcb66a800d0015b2babc4f935ee96fa9 Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 25 Aug 2017 12:01:57 +0200 Subject: [PATCH 2/2] Bump the version to 0.0.154 --- CHANGELOG.md | 3 ++- Cargo.toml | 4 ++-- clippy_lints/Cargo.toml | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b72a6c1b..58ded1b33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ # Change Log All notable changes to this project will be documented in this file. -* New lint: [`naive_bytecount`] ## 0.0.154 +* Update to *rustc 1.21.0-nightly (2c0558f63 2017-08-24)* * Fix [`use_self`] triggering inside derives * Add support for linting an entire workspace with `cargo clippy --all` +* New lint: [`naive_bytecount`] ## 0.0.153 * Update to *rustc 1.21.0-nightly (8c303ed87 2017-08-20)* diff --git a/Cargo.toml b/Cargo.toml index 6db6ea54f..914d453eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clippy" -version = "0.0.153" +version = "0.0.154" authors = [ "Manish Goregaokar ", "Andre Bogus ", @@ -31,7 +31,7 @@ path = "src/main.rs" [dependencies] # begin automatic update -clippy_lints = { version = "0.0.153", path = "clippy_lints" } +clippy_lints = { version = "0.0.154", path = "clippy_lints" } # end automatic update cargo_metadata = "0.2" diff --git a/clippy_lints/Cargo.toml b/clippy_lints/Cargo.toml index 94094bf49..8cdbbaaec 100644 --- a/clippy_lints/Cargo.toml +++ b/clippy_lints/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "clippy_lints" # begin automatic update -version = "0.0.153" +version = "0.0.154" # end automatic update authors = [ "Manish Goregaokar ",