Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix breakage due to rust-lang/rust#61968 #4223

Merged
merged 2 commits into from Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,6 +93,18 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
}

fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
fn negate(bin_op_kind: BinOpKind) -> Option<BinOpKind> {
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 {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/double_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
13 changes: 6 additions & 7 deletions clippy_lints/src/inherent_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ declare_clippy_lint! {
#[allow(clippy::module_name_repetitions)]
#[derive(Default)]
pub struct MultipleInherentImpl {
impls: FxHashMap<def_id::DefId, (Span, Generics)>,
impls: FxHashMap<def_id::DefId, Span>,
}

impl_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]);
Expand All @@ -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);
}
}
}

Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl QuestionMark {
}
}

fn return_expression(block: &Block) -> Option<P<Expr>> {
fn return_expression(block: &Block) -> Option<&P<Expr>> {
// Check if last expression is a return statement. Then, return the expression
if_chain! {
if block.stmts.len() == 1;
Expand All @@ -139,7 +139,7 @@ impl QuestionMark {
if let &Some(ref ret_expr) = ret_expr;

then {
return Some(ret_expr.clone());
return Some(ret_expr);
}
}

Expand All @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,14 @@ pub fn implements_trait<'a, 'tcx>(
/// }
/// }
/// ```
pub fn trait_ref_of_method(cx: &LateContext<'_, '_>, hir_id: HirId) -> Option<TraitRef> {
pub fn trait_ref_of_method<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, hir_id: HirId) -> Option<&'a TraitRef> {
flip1995 marked this conversation as resolved.
Show resolved Hide resolved
// 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
}
Expand Down