From 837b5208f78bada92bcd017e4b3763ba791193e5 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Fri, 21 Jun 2019 08:14:07 +0200 Subject: [PATCH 1/2] Fix breakage due to rust-lang/rust#61968 --- clippy_lints/src/booleans.rs | 53 +++++++++++++-------------- clippy_lints/src/double_comparison.rs | 2 +- clippy_lints/src/inherent_impl.rs | 13 +++---- clippy_lints/src/question_mark.rs | 6 +-- clippy_lints/src/utils/mod.rs | 4 +- 5 files changed, 37 insertions(+), 41 deletions(-) diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index 0acf620cd17e..caf418c9b5b4 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -2,14 +2,14 @@ use crate::utils::{ get_trait_def_id, implements_trait, in_macro, in_macro_or_desugar, match_type, paths, snippet_opt, span_lint_and_then, SpanlessEq, }; +use if_chain::if_chain; use rustc::hir::intravisit::*; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; -use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::Applicability; use syntax::ast::LitKind; -use syntax::source_map::{dummy_spanned, Span, DUMMY_SP}; +use syntax::source_map::Span; declare_clippy_lint! { /// **What it does:** Checks for boolean expressions that can be written more @@ -93,6 +93,18 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> { } fn run(&mut self, e: &'v Expr) -> Result { + fn negate(bin_op_kind: BinOpKind) -> Option { + match bin_op_kind { + BinOpKind::Eq => Some(BinOpKind::Ne), + BinOpKind::Ne => Some(BinOpKind::Eq), + BinOpKind::Gt => Some(BinOpKind::Le), + BinOpKind::Ge => Some(BinOpKind::Lt), + BinOpKind::Lt => Some(BinOpKind::Ge), + BinOpKind::Le => Some(BinOpKind::Gt), + _ => None, + } + } + // prevent folding of `cfg!` macros and the like if !in_macro_or_desugar(e.span) { match &e.node { @@ -115,33 +127,18 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> { #[allow(clippy::cast_possible_truncation)] return Ok(Bool::Term(n as u8)); } - let negated = match &e.node { - ExprKind::Binary(binop, lhs, rhs) => { - if !implements_ord(self.cx, lhs) { - continue; - } - let mk_expr = |op| Expr { - hir_id: DUMMY_HIR_ID, - span: DUMMY_SP, - attrs: ThinVec::new(), - node: ExprKind::Binary(dummy_spanned(op), lhs.clone(), rhs.clone()), - }; - match binop.node { - BinOpKind::Eq => mk_expr(BinOpKind::Ne), - BinOpKind::Ne => mk_expr(BinOpKind::Eq), - BinOpKind::Gt => mk_expr(BinOpKind::Le), - BinOpKind::Ge => mk_expr(BinOpKind::Lt), - BinOpKind::Lt => mk_expr(BinOpKind::Ge), - BinOpKind::Le => mk_expr(BinOpKind::Gt), - _ => continue, - } - }, - _ => continue, - }; - if SpanlessEq::new(self.cx).ignore_fn().eq_expr(&negated, expr) { - #[allow(clippy::cast_possible_truncation)] - return Ok(Bool::Not(Box::new(Bool::Term(n as u8)))); + if_chain! { + if let ExprKind::Binary(e_binop, e_lhs, e_rhs) = &e.node; + if implements_ord(self.cx, e_lhs); + if let ExprKind::Binary(expr_binop, expr_lhs, expr_rhs) = &expr.node; + if negate(e_binop.node) == Some(expr_binop.node); + if SpanlessEq::new(self.cx).ignore_fn().eq_expr(e_lhs, expr_lhs); + if SpanlessEq::new(self.cx).ignore_fn().eq_expr(e_rhs, expr_rhs); + then { + #[allow(clippy::cast_possible_truncation)] + return Ok(Bool::Not(Box::new(Bool::Term(n as u8)))); + } } } let n = self.terminals.len(); diff --git a/clippy_lints/src/double_comparison.rs b/clippy_lints/src/double_comparison.rs index 0f5a88d3dde8..228d53ff1130 100644 --- a/clippy_lints/src/double_comparison.rs +++ b/clippy_lints/src/double_comparison.rs @@ -36,7 +36,7 @@ declare_lint_pass!(DoubleComparisons => [DOUBLE_COMPARISONS]); impl<'a, 'tcx> DoubleComparisons { #[allow(clippy::similar_names)] fn check_binop(self, cx: &LateContext<'a, 'tcx>, op: BinOpKind, lhs: &'tcx Expr, rhs: &'tcx Expr, span: Span) { - let (lkind, llhs, lrhs, rkind, rlhs, rrhs) = match (lhs.node.clone(), rhs.node.clone()) { + let (lkind, llhs, lrhs, rkind, rlhs, rrhs) = match (&lhs.node, &rhs.node) { (ExprKind::Binary(lb, llhs, lrhs), ExprKind::Binary(rb, rlhs, rrhs)) => { (lb.node, llhs, lrhs, rb.node, rlhs, rrhs) }, diff --git a/clippy_lints/src/inherent_impl.rs b/clippy_lints/src/inherent_impl.rs index bde784f81de8..27170817def1 100644 --- a/clippy_lints/src/inherent_impl.rs +++ b/clippy_lints/src/inherent_impl.rs @@ -42,7 +42,7 @@ declare_clippy_lint! { #[allow(clippy::module_name_repetitions)] #[derive(Default)] pub struct MultipleInherentImpl { - impls: FxHashMap, + impls: FxHashMap, } impl_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]); @@ -51,8 +51,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MultipleInherentImpl { fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item) { if let ItemKind::Impl(_, _, _, ref generics, None, _, _) = item.node { // Remember for each inherent implementation encoutered its span and generics - self.impls - .insert(item.hir_id.owner_def_id(), (item.span, generics.clone())); + // but filter out implementations that have generic params (type or lifetime) + if generics.params.len() == 0 { + self.impls.insert(item.hir_id.owner_def_id(), item.span); + } } } @@ -66,10 +68,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MultipleInherentImpl { .values() { // Filter out implementations that have generic params (type or lifetime) - let mut impl_spans = impls - .iter() - .filter_map(|impl_def| self.impls.get(impl_def)) - .filter_map(|(span, generics)| if generics.params.len() == 0 { Some(span) } else { None }); + let mut impl_spans = impls.iter().filter_map(|impl_def| self.impls.get(impl_def)); if let Some(initial_span) = impl_spans.nth(0) { impl_spans.for_each(|additional_span| { span_lint_and_then( diff --git a/clippy_lints/src/question_mark.rs b/clippy_lints/src/question_mark.rs index da038800c2d8..9e0f7d0f3c2e 100644 --- a/clippy_lints/src/question_mark.rs +++ b/clippy_lints/src/question_mark.rs @@ -129,7 +129,7 @@ impl QuestionMark { } } - fn return_expression(block: &Block) -> Option> { + fn return_expression(block: &Block) -> Option<&P> { // Check if last expression is a return statement. Then, return the expression if_chain! { if block.stmts.len() == 1; @@ -139,7 +139,7 @@ impl QuestionMark { if let &Some(ref ret_expr) = ret_expr; then { - return Some(ret_expr.clone()); + return Some(ret_expr); } } @@ -148,7 +148,7 @@ impl QuestionMark { if block.stmts.len() == 0; if let Some(ExprKind::Ret(Some(ret_expr))) = block.expr.as_ref().map(|e| &e.node); then { - return Some(ret_expr.clone()); + return Some(ret_expr); } } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index bee44d092ee8..b7bc4900c5d3 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -315,14 +315,14 @@ pub fn implements_trait<'a, 'tcx>( /// } /// } /// ``` -pub fn trait_ref_of_method(cx: &LateContext<'_, '_>, hir_id: HirId) -> Option { +pub fn trait_ref_of_method<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, hir_id: HirId) -> Option<&'a TraitRef> { // Get the implemented trait for the current function let parent_impl = cx.tcx.hir().get_parent_item(hir_id); if_chain! { if parent_impl != hir::CRATE_HIR_ID; if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl); if let hir::ItemKind::Impl(_, _, _, _, trait_ref, _, _) = &item.node; - then { return trait_ref.clone(); } + then { return trait_ref.as_ref(); } } None } From ca2ba973a7b101088e5b07d1e77ed3b6ef1c1fd2 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 21 Jun 2019 12:32:39 +0200 Subject: [PATCH 2/2] Remove unnecssary lifetime from trait_ref_of_method --- clippy_lints/src/utils/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index b7bc4900c5d3..004a54346a6d 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -315,7 +315,7 @@ pub fn implements_trait<'a, 'tcx>( /// } /// } /// ``` -pub fn trait_ref_of_method<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, hir_id: HirId) -> Option<&'a TraitRef> { +pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'_, 'tcx>, hir_id: HirId) -> Option<&'tcx TraitRef> { // Get the implemented trait for the current function let parent_impl = cx.tcx.hir().get_parent_item(hir_id); if_chain! {