diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b3760d1ec33..d86414a87f4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -964,7 +964,7 @@ Released 2020-11-19 [#5907](https://github.com/rust-lang/rust-clippy/pull/5907) * [`suspicious_arithmetic_impl`]: extend to implementations of `BitAnd`, `BitOr`, `BitXor`, `Rem`, `Shl`, and `Shr` [#5884](https://github.com/rust-lang/rust-clippy/pull/5884) -* [`invalid_atomic_ordering`]: detect misuse of `compare_exchange`, `compare_exchange_weak`, and `fetch_update` +* `invalid_atomic_ordering`: detect misuse of `compare_exchange`, `compare_exchange_weak`, and `fetch_update` [#6025](https://github.com/rust-lang/rust-clippy/pull/6025) * Avoid [`redundant_pattern_matching`] triggering in macros [#6069](https://github.com/rust-lang/rust-clippy/pull/6069) @@ -1451,7 +1451,7 @@ Released 2020-03-12 * [`option_as_ref_deref`] [#4945](https://github.com/rust-lang/rust-clippy/pull/4945) * [`wildcard_in_or_patterns`] [#4960](https://github.com/rust-lang/rust-clippy/pull/4960) * [`iter_nth_zero`] [#4966](https://github.com/rust-lang/rust-clippy/pull/4966) -* [`invalid_atomic_ordering`] [#4999](https://github.com/rust-lang/rust-clippy/pull/4999) +* `invalid_atomic_ordering` [#4999](https://github.com/rust-lang/rust-clippy/pull/4999) * [`skip_while_next`] [#5067](https://github.com/rust-lang/rust-clippy/pull/5067) ### Moves and Deprecations @@ -2712,7 +2712,6 @@ Released 2018-09-13 [`integer_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#integer_arithmetic [`integer_division`]: https://rust-lang.github.io/rust-clippy/master/index.html#integer_division [`into_iter_on_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref -[`invalid_atomic_ordering`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_atomic_ordering [`invalid_null_ptr_usage`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_null_ptr_usage [`invalid_regex`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_regex [`invalid_upcast_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#invalid_upcast_comparisons diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index cb9347a923d8..d834a1d317a0 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -1,5 +1,6 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::higher; use clippy_utils::source::snippet_opt; use clippy_utils::{is_direct_expn_of, is_expn_of, match_panic_call}; use if_chain::if_chain; @@ -116,7 +117,7 @@ enum AssertKind { /// where `message` is any expression and `c` is a constant bool. fn match_assert_with_message<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option { if_chain! { - if let ExprKind::If(cond, then, _) = expr.kind; + if let Some(higher::If { cond, then, .. }) = higher::If::hir(expr); if let ExprKind::Unary(UnOp::Not, expr) = cond.kind; // bind the first argument of the `assert!` macro if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), expr); diff --git a/clippy_lints/src/atomic_ordering.rs b/clippy_lints/src/atomic_ordering.rs deleted file mode 100644 index cece28e8b3c3..000000000000 --- a/clippy_lints/src/atomic_ordering.rs +++ /dev/null @@ -1,230 +0,0 @@ -use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::match_def_path; -use if_chain::if_chain; -use rustc_hir::def_id::DefId; -use rustc_hir::{Expr, ExprKind}; -use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty; -use rustc_session::{declare_lint_pass, declare_tool_lint}; - -declare_clippy_lint! { - /// ### What it does - /// Checks for usage of invalid atomic - /// ordering in atomic loads/stores/exchanges/updates and - /// memory fences. - /// - /// ### Why is this bad? - /// Using an invalid atomic ordering - /// will cause a panic at run-time. - /// - /// ### Example - /// ```rust,no_run - /// # use std::sync::atomic::{self, AtomicU8, Ordering}; - /// - /// let x = AtomicU8::new(0); - /// - /// // Bad: `Release` and `AcqRel` cannot be used for `load`. - /// let _ = x.load(Ordering::Release); - /// let _ = x.load(Ordering::AcqRel); - /// - /// // Bad: `Acquire` and `AcqRel` cannot be used for `store`. - /// x.store(1, Ordering::Acquire); - /// x.store(2, Ordering::AcqRel); - /// - /// // Bad: `Relaxed` cannot be used as a fence's ordering. - /// atomic::fence(Ordering::Relaxed); - /// atomic::compiler_fence(Ordering::Relaxed); - /// - /// // Bad: `Release` and `AcqRel` are both always invalid - /// // for the failure ordering (the last arg). - /// let _ = x.compare_exchange(1, 2, Ordering::SeqCst, Ordering::Release); - /// let _ = x.compare_exchange_weak(2, 3, Ordering::AcqRel, Ordering::AcqRel); - /// - /// // Bad: The failure ordering is not allowed to be - /// // stronger than the success order, and `SeqCst` is - /// // stronger than `Relaxed`. - /// let _ = x.fetch_update(Ordering::Relaxed, Ordering::SeqCst, |val| Some(val + val)); - /// ``` - pub INVALID_ATOMIC_ORDERING, - correctness, - "usage of invalid atomic ordering in atomic operations and memory fences" -} - -declare_lint_pass!(AtomicOrdering => [INVALID_ATOMIC_ORDERING]); - -const ATOMIC_TYPES: [&str; 12] = [ - "AtomicBool", - "AtomicI8", - "AtomicI16", - "AtomicI32", - "AtomicI64", - "AtomicIsize", - "AtomicPtr", - "AtomicU8", - "AtomicU16", - "AtomicU32", - "AtomicU64", - "AtomicUsize", -]; - -fn type_is_atomic(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { - if let ty::Adt(&ty::AdtDef { did, .. }, _) = cx.typeck_results().expr_ty(expr).kind() { - ATOMIC_TYPES - .iter() - .any(|ty| match_def_path(cx, did, &["core", "sync", "atomic", ty])) - } else { - false - } -} - -fn match_ordering_def_path(cx: &LateContext<'_>, did: DefId, orderings: &[&str]) -> bool { - orderings - .iter() - .any(|ordering| match_def_path(cx, did, &["core", "sync", "atomic", "Ordering", ordering])) -} - -fn check_atomic_load_store(cx: &LateContext<'_>, expr: &Expr<'_>) { - if_chain! { - if let ExprKind::MethodCall(method_path, _, args, _) = &expr.kind; - let method = method_path.ident.name.as_str(); - if type_is_atomic(cx, &args[0]); - if method == "load" || method == "store"; - let ordering_arg = if method == "load" { &args[1] } else { &args[2] }; - if let ExprKind::Path(ref ordering_qpath) = ordering_arg.kind; - if let Some(ordering_def_id) = cx.qpath_res(ordering_qpath, ordering_arg.hir_id).opt_def_id(); - then { - if method == "load" && - match_ordering_def_path(cx, ordering_def_id, &["Release", "AcqRel"]) { - span_lint_and_help( - cx, - INVALID_ATOMIC_ORDERING, - ordering_arg.span, - "atomic loads cannot have `Release` and `AcqRel` ordering", - None, - "consider using ordering modes `Acquire`, `SeqCst` or `Relaxed`" - ); - } else if method == "store" && - match_ordering_def_path(cx, ordering_def_id, &["Acquire", "AcqRel"]) { - span_lint_and_help( - cx, - INVALID_ATOMIC_ORDERING, - ordering_arg.span, - "atomic stores cannot have `Acquire` and `AcqRel` ordering", - None, - "consider using ordering modes `Release`, `SeqCst` or `Relaxed`" - ); - } - } - } -} - -fn check_memory_fence(cx: &LateContext<'_>, expr: &Expr<'_>) { - if_chain! { - if let ExprKind::Call(func, args) = expr.kind; - if let ExprKind::Path(ref func_qpath) = func.kind; - if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id(); - if ["fence", "compiler_fence"] - .iter() - .any(|func| match_def_path(cx, def_id, &["core", "sync", "atomic", func])); - if let ExprKind::Path(ref ordering_qpath) = &args[0].kind; - if let Some(ordering_def_id) = cx.qpath_res(ordering_qpath, args[0].hir_id).opt_def_id(); - if match_ordering_def_path(cx, ordering_def_id, &["Relaxed"]); - then { - span_lint_and_help( - cx, - INVALID_ATOMIC_ORDERING, - args[0].span, - "memory fences cannot have `Relaxed` ordering", - None, - "consider using ordering modes `Acquire`, `Release`, `AcqRel` or `SeqCst`" - ); - } - } -} - -fn opt_ordering_defid(cx: &LateContext<'_>, ord_arg: &Expr<'_>) -> Option { - if let ExprKind::Path(ref ord_qpath) = ord_arg.kind { - cx.qpath_res(ord_qpath, ord_arg.hir_id).opt_def_id() - } else { - None - } -} - -fn check_atomic_compare_exchange(cx: &LateContext<'_>, expr: &Expr<'_>) { - if_chain! { - if let ExprKind::MethodCall(method_path, _, args, _) = &expr.kind; - let method = method_path.ident.name.as_str(); - if type_is_atomic(cx, &args[0]); - if method == "compare_exchange" || method == "compare_exchange_weak" || method == "fetch_update"; - let (success_order_arg, failure_order_arg) = if method == "fetch_update" { - (&args[1], &args[2]) - } else { - (&args[3], &args[4]) - }; - if let Some(fail_ordering_def_id) = opt_ordering_defid(cx, failure_order_arg); - then { - // Helper type holding on to some checking and error reporting data. Has - // - (success ordering name, - // - list of failure orderings forbidden by the success order, - // - suggestion message) - type OrdLintInfo = (&'static str, &'static [&'static str], &'static str); - let relaxed: OrdLintInfo = ("Relaxed", &["SeqCst", "Acquire"], "ordering mode `Relaxed`"); - let acquire: OrdLintInfo = ("Acquire", &["SeqCst"], "ordering modes `Acquire` or `Relaxed`"); - let seq_cst: OrdLintInfo = ("SeqCst", &[], "ordering modes `Acquire`, `SeqCst` or `Relaxed`"); - let release = ("Release", relaxed.1, relaxed.2); - let acqrel = ("AcqRel", acquire.1, acquire.2); - let search = [relaxed, acquire, seq_cst, release, acqrel]; - - let success_lint_info = opt_ordering_defid(cx, success_order_arg) - .and_then(|success_ord_def_id| -> Option { - search - .iter() - .find(|(ordering, ..)| { - match_def_path(cx, success_ord_def_id, - &["core", "sync", "atomic", "Ordering", ordering]) - }) - .copied() - }); - - if match_ordering_def_path(cx, fail_ordering_def_id, &["Release", "AcqRel"]) { - // If we don't know the success order is, use what we'd suggest - // if it were maximally permissive. - let suggested = success_lint_info.unwrap_or(seq_cst).2; - span_lint_and_help( - cx, - INVALID_ATOMIC_ORDERING, - failure_order_arg.span, - &format!( - "{}'s failure ordering may not be `Release` or `AcqRel`", - method, - ), - None, - &format!("consider using {} instead", suggested), - ); - } else if let Some((success_ord_name, bad_ords_given_success, suggested)) = success_lint_info { - if match_ordering_def_path(cx, fail_ordering_def_id, bad_ords_given_success) { - span_lint_and_help( - cx, - INVALID_ATOMIC_ORDERING, - failure_order_arg.span, - &format!( - "{}'s failure ordering may not be stronger than the success ordering of `{}`", - method, - success_ord_name, - ), - None, - &format!("consider using {} instead", suggested), - ); - } - } - } - } -} - -impl<'tcx> LateLintPass<'tcx> for AtomicOrdering { - fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - check_atomic_load_store(cx, expr); - check_memory_fence(cx, expr); - check_atomic_compare_exchange(cx, expr); - } -} diff --git a/clippy_lints/src/blocks_in_if_conditions.rs b/clippy_lints/src/blocks_in_if_conditions.rs index 9b2e4f8998e4..51d95cc6f0b1 100644 --- a/clippy_lints/src/blocks_in_if_conditions.rs +++ b/clippy_lints/src/blocks_in_if_conditions.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; +use clippy_utils::higher; use clippy_utils::source::snippet_block_with_applicability; use clippy_utils::ty::implements_trait; use clippy_utils::{differing_macro_contexts, get_parent_expr}; @@ -92,7 +93,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions { if in_external_macro(cx.sess(), expr.span) { return; } - if let ExprKind::If(cond, _, _) = &expr.kind { + if let Some(higher::If { cond, .. }) = higher::If::hir(expr) { if let ExprKind::Block(block, _) = &cond.kind { if block.rules == BlockCheckMode::DefaultBlock { if block.stmts.is_empty() { diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index 4a83d35a568c..6f12d34e66b6 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -116,7 +116,7 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> { // prevent folding of `cfg!` macros and the like if !e.span.from_expansion() { match &e.kind { - ExprKind::Unary(UnOp::Not, inner) => return Ok(Bool::Not(box self.run(inner)?)), + ExprKind::Unary(UnOp::Not, inner) => return Ok(Bool::Not(Box::new(self.run(inner)?))), ExprKind::Binary(binop, lhs, rhs) => match &binop.node { BinOpKind::Or => { return Ok(Bool::Or(self.extract(BinOpKind::Or, &[lhs, rhs], Vec::new())?)); diff --git a/clippy_lints/src/collapsible_match.rs b/clippy_lints/src/collapsible_match.rs index bd8f9cc7343b..a4693fa213bc 100644 --- a/clippy_lints/src/collapsible_match.rs +++ b/clippy_lints/src/collapsible_match.rs @@ -1,6 +1,7 @@ use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::higher::IfLetOrMatch; use clippy_utils::visitors::is_local_used; -use clippy_utils::{is_lang_ctor, path_to_local, peel_ref_operators, SpanlessEq}; +use clippy_utils::{is_lang_ctor, is_unit_expr, path_to_local, peel_ref_operators, SpanlessEq}; use if_chain::if_chain; use rustc_hir::LangItem::OptionNone; use rustc_hir::{Arm, Expr, ExprKind, Guard, HirId, Pat, PatKind, StmtKind}; @@ -49,56 +50,86 @@ declare_lint_pass!(CollapsibleMatch => [COLLAPSIBLE_MATCH]); impl<'tcx> LateLintPass<'tcx> for CollapsibleMatch { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { - if let ExprKind::Match(_expr, arms, _source) = expr.kind { - if let Some(wild_arm) = arms.iter().rfind(|arm| arm_is_wild_like(cx, arm)) { - for arm in arms { - check_arm(arm, wild_arm, cx); + match IfLetOrMatch::parse(cx, expr) { + Some(IfLetOrMatch::Match(_, arms, _)) => { + if let Some(els_arm) = arms.iter().rfind(|arm| arm_is_wild_like(cx, arm)) { + for arm in arms { + check_arm(cx, true, arm.pat, arm.body, arm.guard.as_ref(), Some(els_arm.body)); + } } - } + }, + Some(IfLetOrMatch::IfLet(_, pat, body, els)) => { + check_arm(cx, false, pat, body, None, els); + }, + None => {}, } } } -fn check_arm<'tcx>(arm: &Arm<'tcx>, wild_outer_arm: &Arm<'tcx>, cx: &LateContext<'tcx>) { - let expr = strip_singleton_blocks(arm.body); +fn check_arm<'tcx>( + cx: &LateContext<'tcx>, + outer_is_match: bool, + outer_pat: &'tcx Pat<'tcx>, + outer_then_body: &'tcx Expr<'tcx>, + outer_guard: Option<&'tcx Guard<'tcx>>, + outer_else_body: Option<&'tcx Expr<'tcx>>, +) { + let inner_expr = strip_singleton_blocks(outer_then_body); if_chain! { - if let ExprKind::Match(expr_in, arms_inner, _) = expr.kind; - // the outer arm pattern and the inner match - if expr_in.span.ctxt() == arm.pat.span.ctxt(); - // there must be no more than two arms in the inner match for this lint - if arms_inner.len() == 2; - // no if guards on the inner match - if arms_inner.iter().all(|arm| arm.guard.is_none()); + if let Some(inner) = IfLetOrMatch::parse(cx, inner_expr); + if let Some((inner_scrutinee, inner_then_pat, inner_else_body)) = match inner { + IfLetOrMatch::IfLet(scrutinee, pat, _, els) => Some((scrutinee, pat, els)), + IfLetOrMatch::Match(scrutinee, arms, ..) => if_chain! { + // if there are more than two arms, collapsing would be non-trivial + if arms.len() == 2 && arms.iter().all(|a| a.guard.is_none()); + // one of the arms must be "wild-like" + if let Some(wild_idx) = arms.iter().rposition(|a| arm_is_wild_like(cx, a)); + then { + let (then, els) = (&arms[1 - wild_idx], &arms[wild_idx]); + Some((scrutinee, then.pat, Some(els.body))) + } else { + None + } + }, + }; + if outer_pat.span.ctxt() == inner_scrutinee.span.ctxt(); // match expression must be a local binding // match { .. } - if let Some(binding_id) = path_to_local(peel_ref_operators(cx, expr_in)); - // one of the branches must be "wild-like" - if let Some(wild_inner_arm_idx) = arms_inner.iter().rposition(|arm_inner| arm_is_wild_like(cx, arm_inner)); - let (wild_inner_arm, non_wild_inner_arm) = - (&arms_inner[wild_inner_arm_idx], &arms_inner[1 - wild_inner_arm_idx]); - if !pat_contains_or(non_wild_inner_arm.pat); + if let Some(binding_id) = path_to_local(peel_ref_operators(cx, inner_scrutinee)); + if !pat_contains_or(inner_then_pat); // the binding must come from the pattern of the containing match arm // .... => match { .. } - if let Some(binding_span) = find_pat_binding(arm.pat, binding_id); - // the "wild-like" branches must be equal - if SpanlessEq::new(cx).eq_expr(wild_inner_arm.body, wild_outer_arm.body); + if let Some(binding_span) = find_pat_binding(outer_pat, binding_id); + // the "else" branches must be equal + if match (outer_else_body, inner_else_body) { + (None, None) => true, + (None, Some(e)) | (Some(e), None) => is_unit_expr(e), + (Some(a), Some(b)) => SpanlessEq::new(cx).eq_expr(a, b), + }; // the binding must not be used in the if guard - if match arm.guard { - None => true, - Some(Guard::If(expr) | Guard::IfLet(_, expr)) => !is_local_used(cx, expr, binding_id), + if outer_guard.map_or(true, |(Guard::If(e) | Guard::IfLet(_, e))| !is_local_used(cx, *e, binding_id)); + // ...or anywhere in the inner expression + if match inner { + IfLetOrMatch::IfLet(_, _, body, els) => { + !is_local_used(cx, body, binding_id) && els.map_or(true, |e| !is_local_used(cx, e, binding_id)) + }, + IfLetOrMatch::Match(_, arms, ..) => !arms.iter().any(|arm| is_local_used(cx, arm, binding_id)), }; - // ...or anywhere in the inner match - if !arms_inner.iter().any(|arm| is_local_used(cx, arm, binding_id)); then { + let msg = format!( + "this `{}` can be collapsed into the outer `{}`", + if matches!(inner, IfLetOrMatch::Match(..)) { "match" } else { "if let" }, + if outer_is_match { "match" } else { "if let" }, + ); span_lint_and_then( cx, COLLAPSIBLE_MATCH, - expr.span, - "unnecessary nested match", + inner_expr.span, + &msg, |diag| { - let mut help_span = MultiSpan::from_spans(vec![binding_span, non_wild_inner_arm.pat.span]); + let mut help_span = MultiSpan::from_spans(vec![binding_span, inner_then_pat.span]); help_span.push_span_label(binding_span, "replace this binding".into()); - help_span.push_span_label(non_wild_inner_arm.pat.span, "with this pattern".into()); + help_span.push_span_label(inner_then_pat.span, "with this pattern".into()); diag.span_help(help_span, "the outer pattern can be modified to include the inner pattern"); }, ); @@ -120,9 +151,8 @@ fn strip_singleton_blocks<'hir>(mut expr: &'hir Expr<'hir>) -> &'hir Expr<'hir> expr } -/// A "wild-like" pattern is wild ("_") or `None`. -/// For this lint to apply, both the outer and inner match expressions -/// must have "wild-like" branches that can be combined. +/// A "wild-like" arm has a wild (`_`) or `None` pattern and no guard. Such arms can be "collapsed" +/// into a single wild arm without any significant loss in semantics or readability. fn arm_is_wild_like(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool { if arm.guard.is_some() { return false; diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index 29f3c911a306..d58e49491203 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -316,9 +316,10 @@ fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> Option< let mut start_eq = usize::MAX; let mut end_eq = usize::MAX; let mut expr_eq = true; - for win in blocks.windows(2) { - let l_stmts = win[0].stmts; - let r_stmts = win[1].stmts; + let mut iter = blocks.windows(2); + while let Some(&[win0, win1]) = iter.next() { + let l_stmts = win0.stmts; + let r_stmts = win1.stmts; // `SpanlessEq` now keeps track of the locals and is therefore context sensitive clippy#6752. // The comparison therefore needs to be done in a way that builds the correct context. @@ -335,22 +336,22 @@ fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> Option< it1.zip(it2) .fold(0, |acc, (l, r)| if evaluator.eq_stmt(l, r) { acc + 1 } else { 0 }) }; - let block_expr_eq = both(&win[0].expr, &win[1].expr, |l, r| evaluator.eq_expr(l, r)); + let block_expr_eq = both(&win0.expr, &win1.expr, |l, r| evaluator.eq_expr(l, r)); // IF_SAME_THEN_ELSE if_chain! { if block_expr_eq; if l_stmts.len() == r_stmts.len(); if l_stmts.len() == current_start_eq; - if !is_lint_allowed(cx, IF_SAME_THEN_ELSE, win[0].hir_id); - if !is_lint_allowed(cx, IF_SAME_THEN_ELSE, win[1].hir_id); + if !is_lint_allowed(cx, IF_SAME_THEN_ELSE, win0.hir_id); + if !is_lint_allowed(cx, IF_SAME_THEN_ELSE, win1.hir_id); then { span_lint_and_note( cx, IF_SAME_THEN_ELSE, - win[0].span, + win0.span, "this `if` has identical blocks", - Some(win[1].span), + Some(win1.span), "same as this", ); diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index ded7001ad8c8..7825e5f6ed52 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -232,6 +232,7 @@ fn is_linted_explicit_deref_position(parent: Option>, child_id: HirId, | ExprKind::If(..) | ExprKind::Loop(..) | ExprKind::Match(..) + | ExprKind::Let(..) | ExprKind::Closure(..) | ExprKind::Block(..) | ExprKind::Assign(..) diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index 75561cfde369..cb2b7f5be70a 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -578,8 +578,8 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) { let filename = FileName::anon_source_code(&code); let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); - let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false); - let handler = Handler::with_emitter(false, None, box emitter); + let emitter = EmitterWriter::new(Box::new(io::sink()), None, false, false, false, None, false); + let handler = Handler::with_emitter(false, None, Box::new(emitter)); let sess = ParseSess::with_span_handler(handler, sm); let mut parser = match maybe_new_parser_from_source_str(&sess, filename, code) { diff --git a/clippy_lints/src/entry.rs b/clippy_lints/src/entry.rs index 7fb8e4276600..ac6824672f66 100644 --- a/clippy_lints/src/entry.rs +++ b/clippy_lints/src/entry.rs @@ -1,3 +1,4 @@ +use clippy_utils::higher; use clippy_utils::{ can_move_expr_to_closure_no_visit, diagnostics::span_lint_and_sugg, @@ -5,6 +6,7 @@ use clippy_utils::{ source::{reindent_multiline, snippet_indent, snippet_with_applicability, snippet_with_context}, SpanlessEq, }; +use core::fmt::Write; use rustc_errors::Applicability; use rustc_hir::{ hir_id::HirIdSet, @@ -14,7 +16,6 @@ use rustc_hir::{ use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{Span, SyntaxContext, DUMMY_SP}; -use std::fmt::Write; declare_clippy_lint! { /// ### What it does @@ -63,10 +64,11 @@ declare_lint_pass!(HashMapPass => [MAP_ENTRY]); impl<'tcx> LateLintPass<'tcx> for HashMapPass { #[allow(clippy::too_many_lines)] fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - let (cond_expr, then_expr, else_expr) = match expr.kind { - ExprKind::If(c, t, e) => (c, t, e), + let (cond_expr, then_expr, else_expr) = match higher::If::hir(expr) { + Some(higher::If { cond, then, r#else }) => (cond, then, r#else), _ => return, }; + let (map_ty, contains_expr) = match try_parse_contains(cx, cond_expr) { Some(x) => x, None => return, diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index 8b0e9e6bc9b9..ae66edc2a9e1 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -53,7 +53,7 @@ fn is_non_trait_box(ty: Ty<'_>) -> bool { struct EscapeDelegate<'a, 'tcx> { cx: &'a LateContext<'tcx>, set: HirIdSet, - trait_self_ty: Option>, + trait_self_ty: Option>, too_large_for_stack: u64, } @@ -171,7 +171,8 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> { // skip if there is a `self` parameter binding to a type // that contains `Self` (i.e.: `self: Box`), see #4804 if let Some(trait_self_ty) = self.trait_self_ty { - if map.name(cmt.hir_id) == kw::SelfLower && contains_ty(cmt.place.ty(), trait_self_ty) { + if map.name(cmt.hir_id) == kw::SelfLower && contains_ty(self.cx.tcx, cmt.place.ty(), trait_self_ty) + { return; } } diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 192b69e18f90..f6a64a8ca603 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -100,7 +100,7 @@ fn check_closure<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if ex.span.ctxt() != expr.span.ctxt() { if decl.inputs.is_empty() { - if let Some(VecArgs::Vec(&[])) = higher::vec_macro(cx, ex) { + if let Some(VecArgs::Vec(&[])) = higher::VecArgs::hir(cx, ex) { // replace `|| vec![]` with `Vec::new` span_lint_and_sugg( cx, diff --git a/clippy_lints/src/floating_point_arithmetic.rs b/clippy_lints/src/floating_point_arithmetic.rs index b01c0cdd8462..a643b3eb8f1b 100644 --- a/clippy_lints/src/floating_point_arithmetic.rs +++ b/clippy_lints/src/floating_point_arithmetic.rs @@ -3,6 +3,7 @@ use clippy_utils::consts::{ Constant::{Int, F32, F64}, }; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::higher; use clippy_utils::{eq_expr_value, get_parent_expr, numeric_literal, sugg}; use if_chain::if_chain; use rustc_errors::Applicability; @@ -331,8 +332,6 @@ fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) { ), Applicability::MachineApplicable, ); - - return; } } } @@ -545,11 +544,11 @@ fn are_negated<'a>(cx: &LateContext<'_>, expr1: &'a Expr<'a>, expr2: &'a Expr<'a fn check_custom_abs(cx: &LateContext<'_>, expr: &Expr<'_>) { if_chain! { - if let ExprKind::If(cond, body, else_body) = expr.kind; - if let ExprKind::Block(block, _) = body.kind; + if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr); + if let ExprKind::Block(block, _) = then.kind; if block.stmts.is_empty(); if let Some(if_body_expr) = block.expr; - if let Some(ExprKind::Block(else_block, _)) = else_body.map(|el| &el.kind); + if let Some(ExprKind::Block(else_block, _)) = r#else.map(|el| &el.kind); if else_block.stmts.is_empty(); if let Some(else_body_expr) = else_block.expr; if let Some((if_expr_positive, body)) = are_negated(cx, if_body_expr, else_body_expr); diff --git a/clippy_lints/src/formatting.rs b/clippy_lints/src/formatting.rs index b4cf1971d78d..4dd0ffe77ea4 100644 --- a/clippy_lints/src/formatting.rs +++ b/clippy_lints/src/formatting.rs @@ -164,7 +164,7 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) { if !differing_macro_contexts(lhs.span, rhs.span) && !lhs.span.from_expansion(); // span between BinOp LHS and RHS let binop_span = lhs.span.between(rhs.span); - // if RHS is a UnOp + // if RHS is an UnOp if let ExprKind::Unary(op, ref un_rhs) = rhs.kind; // from UnOp operator to UnOp operand let unop_operand_span = rhs.span.until(un_rhs.span); diff --git a/clippy_lints/src/functions/must_use.rs b/clippy_lints/src/functions/must_use.rs index ea6193acbe84..77d08081c07f 100644 --- a/clippy_lints/src/functions/must_use.rs +++ b/clippy_lints/src/functions/must_use.rs @@ -26,7 +26,6 @@ pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { let fn_header_span = item.span.with_hi(sig.decl.output.span().hi()); if let Some(attr) = attr { check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr); - return; } else if is_public && !is_proc_macro(cx.sess(), attrs) && !attrs.iter().any(|a| a.has_name(sym::no_mangle)) { check_must_use_candidate( cx, diff --git a/clippy_lints/src/future_not_send.rs b/clippy_lints/src/future_not_send.rs index 0be03969bcbe..3e35ada7b2a1 100644 --- a/clippy_lints/src/future_not_send.rs +++ b/clippy_lints/src/future_not_send.rs @@ -93,7 +93,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { cx.tcx.infer_ctxt().enter(|infcx| { for FulfillmentError { obligation, .. } in send_errors { infcx.maybe_note_obligation_cause_for_async_await(db, &obligation); - if let Trait(trait_pred, _) = obligation.predicate.kind().skip_binder() { + if let Trait(trait_pred) = obligation.predicate.kind().skip_binder() { db.note(&format!( "`{}` doesn't implement `{}`", trait_pred.self_ty(), diff --git a/clippy_lints/src/if_let_mutex.rs b/clippy_lints/src/if_let_mutex.rs index d3ddeda9fd1b..7dad1c31150e 100644 --- a/clippy_lints/src/if_let_mutex.rs +++ b/clippy_lints/src/if_let_mutex.rs @@ -1,9 +1,10 @@ use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::higher; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::SpanlessEq; use if_chain::if_chain; use rustc_hir::intravisit::{self as visit, NestedVisitorMap, Visitor}; -use rustc_hir::{Expr, ExprKind, MatchSource}; +use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::map::Map; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -42,7 +43,7 @@ declare_clippy_lint! { declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]); impl<'tcx> LateLintPass<'tcx> for IfLetMutex { - fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) { + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { let mut arm_visit = ArmVisitor { mutex_lock_called: false, found_mutex: None, @@ -53,25 +54,23 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex { found_mutex: None, cx, }; - if let ExprKind::Match( - op, - arms, - MatchSource::IfLetDesugar { - contains_else_clause: true, - }, - ) = ex.kind + if let Some(higher::IfLet { + let_expr, + if_then, + if_else: Some(if_else), + .. + }) = higher::IfLet::hir(cx, expr) { - op_visit.visit_expr(op); + op_visit.visit_expr(let_expr); if op_visit.mutex_lock_called { - for arm in arms { - arm_visit.visit_arm(arm); - } + arm_visit.visit_expr(if_then); + arm_visit.visit_expr(if_else); if arm_visit.mutex_lock_called && arm_visit.same_mutex(cx, op_visit.found_mutex.unwrap()) { span_lint_and_help( cx, IF_LET_MUTEX, - ex.span, + expr.span, "calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock", None, "move the lock call outside of the `if let ...` expression", diff --git a/clippy_lints/src/if_let_some_result.rs b/clippy_lints/src/if_let_some_result.rs index 587307811a11..33eba02dae06 100644 --- a/clippy_lints/src/if_let_some_result.rs +++ b/clippy_lints/src/if_let_some_result.rs @@ -1,10 +1,11 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::higher; use clippy_utils::method_chain_args; use clippy_utils::source::snippet_with_applicability; use clippy_utils::ty::is_type_diagnostic_item; use if_chain::if_chain; use rustc_errors::Applicability; -use rustc_hir::{Expr, ExprKind, MatchSource, PatKind, QPath}; +use rustc_hir::{Expr, ExprKind, PatKind, QPath}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::sym; @@ -44,17 +45,17 @@ declare_lint_pass!(OkIfLet => [IF_LET_SOME_RESULT]); impl<'tcx> LateLintPass<'tcx> for OkIfLet { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if_chain! { //begin checking variables - if let ExprKind::Match(op, body, MatchSource::IfLetDesugar { .. }) = expr.kind; //test if expr is if let - if let ExprKind::MethodCall(_, ok_span, result_types, _) = op.kind; //check is expr.ok() has type Result.ok(, _) - if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = body[0].pat.kind; //get operation - if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized; + if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr); + if let ExprKind::MethodCall(_, ok_span, result_types, _) = let_expr.kind; //check is expr.ok() has type Result.ok(, _) + if let PatKind::TupleStruct(QPath::Resolved(_, x), y, _) = let_pat.kind; //get operation + if method_chain_args(let_expr, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized; if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&result_types[0]), sym::result_type); if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some"; then { let mut applicability = Applicability::MachineApplicable; let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability); - let trimmed_ok = snippet_with_applicability(cx, op.span.until(ok_span), "", &mut applicability); + let trimmed_ok = snippet_with_applicability(cx, let_expr.span.until(ok_span), "", &mut applicability); let sugg = format!( "if let Ok({}) = {}", some_expr_string, @@ -63,7 +64,7 @@ impl<'tcx> LateLintPass<'tcx> for OkIfLet { span_lint_and_sugg( cx, IF_LET_SOME_RESULT, - expr.span.with_hi(op.span.hi()), + expr.span.with_hi(let_expr.span.hi()), "matching on `Some` with `ok()` is redundant", &format!("consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string), sugg, diff --git a/clippy_lints/src/if_then_some_else_none.rs b/clippy_lints/src/if_then_some_else_none.rs index 17b9a2f888e0..a2dac57454f2 100644 --- a/clippy_lints/src/if_then_some_else_none.rs +++ b/clippy_lints/src/if_then_some_else_none.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::source::snippet_with_macro_callsite; -use clippy_utils::{is_else_clause, is_lang_ctor, meets_msrv, msrvs}; +use clippy_utils::{higher, is_else_clause, is_lang_ctor, meets_msrv, msrvs}; use if_chain::if_chain; use rustc_hir::LangItem::{OptionNone, OptionSome}; use rustc_hir::{Expr, ExprKind}; @@ -70,7 +70,7 @@ impl LateLintPass<'_> for IfThenSomeElseNone { } if_chain! { - if let ExprKind::If(cond, then, Some(els)) = expr.kind; + if let Some(higher::If { cond, then, r#else: Some(els) }) = higher::If::hir(expr); if let ExprKind::Block(then_block, _) = then.kind; if let Some(then_expr) = then_block.expr; if let ExprKind::Call(then_call, [then_arg]) = then_expr.kind; diff --git a/clippy_lints/src/implicit_saturating_sub.rs b/clippy_lints/src/implicit_saturating_sub.rs index 0a7d31dce2fd..79d4d7ddcbce 100644 --- a/clippy_lints/src/implicit_saturating_sub.rs +++ b/clippy_lints/src/implicit_saturating_sub.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::higher; use clippy_utils::{in_macro, SpanlessEq}; use if_chain::if_chain; use rustc_ast::ast::LitKind; @@ -42,7 +43,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub { return; } if_chain! { - if let ExprKind::If(cond, then, None) = &expr.kind; + if let Some(higher::If { cond, then, .. }) = higher::If::hir(expr); // Check if the conditional expression is a binary operation if let ExprKind::Binary(ref cond_op, cond_left, cond_right) = cond.kind; diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index 8c1f10733095..f52f090d3872 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if let ExprKind::Index(array, index) = &expr.kind { let ty = cx.typeck_results().expr_ty(array).peel_refs(); - if let Some(range) = higher::range(index) { + if let Some(range) = higher::Range::hir(index) { // Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..] if let ty::Array(_, s) = ty.kind() { let size: u128 = if let Some(size) = s.try_eval_usize(cx.tcx, cx.param_env) { diff --git a/clippy_lints/src/infinite_iter.rs b/clippy_lints/src/infinite_iter.rs index 2411a3175b91..58646385def5 100644 --- a/clippy_lints/src/infinite_iter.rs +++ b/clippy_lints/src/infinite_iter.rs @@ -172,7 +172,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness { Finite } }, - ExprKind::Struct(..) => higher::range(expr).map_or(false, |r| r.end.is_none()).into(), + ExprKind::Struct(..) => higher::Range::hir(expr).map_or(false, |r| r.end.is_none()).into(), _ => Finite, } } diff --git a/clippy_lints/src/let_if_seq.rs b/clippy_lints/src/let_if_seq.rs index 834440e912df..7f2c7b707f0b 100644 --- a/clippy_lints/src/let_if_seq.rs +++ b/clippy_lints/src/let_if_seq.rs @@ -64,8 +64,8 @@ impl<'tcx> LateLintPass<'tcx> for LetIfSeq { if let hir::StmtKind::Local(local) = stmt.kind; if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.kind; if let hir::StmtKind::Expr(if_) = expr.kind; - if let hir::ExprKind::If(cond, then, ref else_) = if_.kind; - if !is_local_used(cx, cond, canonical_id); + if let hir::ExprKind::If(hir::Expr { kind: hir::ExprKind::DropTemps(cond), ..}, then, else_) = if_.kind; + if !is_local_used(cx, *cond, canonical_id); if let hir::ExprKind::Block(then, _) = then.kind; if let Some(value) = check_assign(cx, canonical_id, &*then); if !is_local_used(cx, value, canonical_id); @@ -78,7 +78,7 @@ impl<'tcx> LateLintPass<'tcx> for LetIfSeq { ); if has_interior_mutability { return; } - let (default_multi_stmts, default) = if let Some(else_) = *else_ { + let (default_multi_stmts, default) = if let Some(else_) = else_ { if let hir::ExprKind::Block(else_, _) = else_.kind { if let Some(default) = check_assign(cx, canonical_id, else_) { (else_.stmts.len() > 1, default) diff --git a/clippy_lints/src/let_underscore.rs b/clippy_lints/src/let_underscore.rs index 8992d25932ca..89146b4dd2c9 100644 --- a/clippy_lints/src/let_underscore.rs +++ b/clippy_lints/src/let_underscore.rs @@ -119,7 +119,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore { if let Some(init) = local.init; then { let init_ty = cx.typeck_results().expr_ty(init); - let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() { + let contains_sync_guard = init_ty.walk(cx.tcx).any(|inner| match inner.unpack() { GenericArgKind::Type(inner_ty) => { SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path)) }, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 5987f9e5b0cb..2b76eacb7d61 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -1,7 +1,6 @@ // error-pattern:cargo-clippy #![feature(box_patterns)] -#![feature(box_syntax)] #![feature(drain_filter)] #![feature(in_band_lifetimes)] #![feature(iter_zip)] @@ -165,7 +164,6 @@ mod asm_syntax; mod assertions_on_constants; mod assign_ops; mod async_yields_async; -mod atomic_ordering; mod attrs; mod await_holding_invalid; mod bit_mask; @@ -396,9 +394,9 @@ use crate::utils::conf::TryConf; /// Used in `./src/driver.rs`. pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore) { // NOTE: Do not add any more pre-expansion passes. These should be removed eventually. - store.register_pre_expansion_pass(|| box write::Write::default()); - store.register_pre_expansion_pass(|| box attrs::EarlyAttributes); - store.register_pre_expansion_pass(|| box dbg_macro::DbgMacro); + store.register_pre_expansion_pass(|| Box::new(write::Write::default())); + store.register_pre_expansion_pass(|| Box::new(attrs::EarlyAttributes)); + store.register_pre_expansion_pass(|| Box::new(dbg_macro::DbgMacro)); } #[doc(hidden)] @@ -539,7 +537,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: assign_ops::ASSIGN_OP_PATTERN, assign_ops::MISREFACTORED_ASSIGN_OP, async_yields_async::ASYNC_YIELDS_ASYNC, - atomic_ordering::INVALID_ATOMIC_ORDERING, attrs::BLANKET_CLIPPY_RESTRICTION_LINTS, attrs::DEPRECATED_CFG_ATTR, attrs::DEPRECATED_SEMVER, @@ -1183,7 +1180,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(assign_ops::ASSIGN_OP_PATTERN), LintId::of(assign_ops::MISREFACTORED_ASSIGN_OP), LintId::of(async_yields_async::ASYNC_YIELDS_ASYNC), - LintId::of(atomic_ordering::INVALID_ATOMIC_ORDERING), LintId::of(attrs::BLANKET_CLIPPY_RESTRICTION_LINTS), LintId::of(attrs::DEPRECATED_CFG_ATTR), LintId::of(attrs::DEPRECATED_SEMVER), @@ -1681,7 +1677,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(absurd_extreme_comparisons::ABSURD_EXTREME_COMPARISONS), LintId::of(approx_const::APPROX_CONSTANT), LintId::of(async_yields_async::ASYNC_YIELDS_ASYNC), - LintId::of(atomic_ordering::INVALID_ATOMIC_ORDERING), LintId::of(attrs::DEPRECATED_SEMVER), LintId::of(attrs::MISMATCHED_TARGET_OS), LintId::of(attrs::USELESS_ATTRIBUTE), @@ -1826,7 +1821,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: #[cfg(feature = "metadata-collector-lint")] { if std::env::var("ENABLE_METADATA_COLLECTION").eq(&Ok("1".to_string())) { - store.register_late_pass(|| box utils::internal_lints::metadata_collector::MetadataCollector::new()); + store.register_late_pass(|| Box::new(utils::internal_lints::metadata_collector::MetadataCollector::new())); return; } } @@ -1834,62 +1829,62 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: // all the internal lints #[cfg(feature = "internal-lints")] { - store.register_early_pass(|| box utils::internal_lints::ClippyLintsInternal); - store.register_early_pass(|| box utils::internal_lints::ProduceIce); - store.register_late_pass(|| box utils::inspector::DeepCodeInspector); - store.register_late_pass(|| box utils::internal_lints::CollapsibleCalls); - store.register_late_pass(|| box utils::internal_lints::CompilerLintFunctions::new()); - store.register_late_pass(|| box utils::internal_lints::IfChainStyle); - store.register_late_pass(|| box utils::internal_lints::InvalidPaths); - store.register_late_pass(|| box utils::internal_lints::InterningDefinedSymbol::default()); - store.register_late_pass(|| box utils::internal_lints::LintWithoutLintPass::default()); - store.register_late_pass(|| box utils::internal_lints::MatchTypeOnDiagItem); - store.register_late_pass(|| box utils::internal_lints::OuterExpnDataPass); + store.register_early_pass(|| Box::new(utils::internal_lints::ClippyLintsInternal)); + store.register_early_pass(|| Box::new(utils::internal_lints::ProduceIce)); + store.register_late_pass(|| Box::new(utils::inspector::DeepCodeInspector)); + store.register_late_pass(|| Box::new(utils::internal_lints::CollapsibleCalls)); + store.register_late_pass(|| Box::new(utils::internal_lints::CompilerLintFunctions::new())); + store.register_late_pass(|| Box::new(utils::internal_lints::IfChainStyle)); + store.register_late_pass(|| Box::new(utils::internal_lints::InvalidPaths)); + store.register_late_pass(|| Box::new(utils::internal_lints::InterningDefinedSymbol::default())); + store.register_late_pass(|| Box::new(utils::internal_lints::LintWithoutLintPass::default())); + store.register_late_pass(|| Box::new(utils::internal_lints::MatchTypeOnDiagItem)); + store.register_late_pass(|| Box::new(utils::internal_lints::OuterExpnDataPass)); } - store.register_late_pass(|| box utils::author::Author); - store.register_late_pass(|| box await_holding_invalid::AwaitHolding); - store.register_late_pass(|| box serde_api::SerdeApi); + store.register_late_pass(|| Box::new(utils::author::Author)); + store.register_late_pass(|| Box::new(await_holding_invalid::AwaitHolding)); + store.register_late_pass(|| Box::new(serde_api::SerdeApi)); let vec_box_size_threshold = conf.vec_box_size_threshold; let type_complexity_threshold = conf.type_complexity_threshold; let avoid_breaking_exported_api = conf.avoid_breaking_exported_api; - store.register_late_pass(move || box types::Types::new( + store.register_late_pass(move || Box::new(types::Types::new( vec_box_size_threshold, type_complexity_threshold, avoid_breaking_exported_api, - )); - store.register_late_pass(|| box booleans::NonminimalBool); - store.register_late_pass(|| box needless_bitwise_bool::NeedlessBitwiseBool); - store.register_late_pass(|| box eq_op::EqOp); - store.register_late_pass(|| box enum_clike::UnportableVariant); - store.register_late_pass(|| box float_literal::FloatLiteral); + ))); + store.register_late_pass(|| Box::new(booleans::NonminimalBool)); + store.register_late_pass(|| Box::new(needless_bitwise_bool::NeedlessBitwiseBool)); + store.register_late_pass(|| Box::new(eq_op::EqOp)); + store.register_late_pass(|| Box::new(enum_clike::UnportableVariant)); + store.register_late_pass(|| Box::new(float_literal::FloatLiteral)); let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold; - store.register_late_pass(move || box bit_mask::BitMask::new(verbose_bit_mask_threshold)); - store.register_late_pass(|| box ptr::Ptr); - store.register_late_pass(|| box ptr_eq::PtrEq); - store.register_late_pass(|| box needless_bool::NeedlessBool); - store.register_late_pass(|| box needless_bool::BoolComparison); - store.register_late_pass(|| box needless_for_each::NeedlessForEach); - store.register_late_pass(|| box approx_const::ApproxConstant); - store.register_late_pass(|| box misc::MiscLints); - store.register_late_pass(|| box eta_reduction::EtaReduction); - store.register_late_pass(|| box identity_op::IdentityOp); - store.register_late_pass(|| box erasing_op::ErasingOp); - store.register_late_pass(|| box mut_mut::MutMut); - store.register_late_pass(|| box mut_reference::UnnecessaryMutPassed); - store.register_late_pass(|| box len_zero::LenZero); - store.register_late_pass(|| box attrs::Attributes); - store.register_late_pass(|| box blocks_in_if_conditions::BlocksInIfConditions); - store.register_late_pass(|| box collapsible_match::CollapsibleMatch); - store.register_late_pass(|| box unicode::Unicode); - store.register_late_pass(|| box unit_return_expecting_ord::UnitReturnExpectingOrd); - store.register_late_pass(|| box strings::StringAdd); - store.register_late_pass(|| box implicit_return::ImplicitReturn); - store.register_late_pass(|| box implicit_saturating_sub::ImplicitSaturatingSub); - store.register_late_pass(|| box default_numeric_fallback::DefaultNumericFallback); - store.register_late_pass(|| box inconsistent_struct_constructor::InconsistentStructConstructor); - store.register_late_pass(|| box non_octal_unix_permissions::NonOctalUnixPermissions); - store.register_early_pass(|| box unnecessary_self_imports::UnnecessarySelfImports); + store.register_late_pass(move || Box::new(bit_mask::BitMask::new(verbose_bit_mask_threshold))); + store.register_late_pass(|| Box::new(ptr::Ptr)); + store.register_late_pass(|| Box::new(ptr_eq::PtrEq)); + store.register_late_pass(|| Box::new(needless_bool::NeedlessBool)); + store.register_late_pass(|| Box::new(needless_bool::BoolComparison)); + store.register_late_pass(|| Box::new(needless_for_each::NeedlessForEach)); + store.register_late_pass(|| Box::new(approx_const::ApproxConstant)); + store.register_late_pass(|| Box::new(misc::MiscLints)); + store.register_late_pass(|| Box::new(eta_reduction::EtaReduction)); + store.register_late_pass(|| Box::new(identity_op::IdentityOp)); + store.register_late_pass(|| Box::new(erasing_op::ErasingOp)); + store.register_late_pass(|| Box::new(mut_mut::MutMut)); + store.register_late_pass(|| Box::new(mut_reference::UnnecessaryMutPassed)); + store.register_late_pass(|| Box::new(len_zero::LenZero)); + store.register_late_pass(|| Box::new(attrs::Attributes)); + store.register_late_pass(|| Box::new(blocks_in_if_conditions::BlocksInIfConditions)); + store.register_late_pass(|| Box::new(collapsible_match::CollapsibleMatch)); + store.register_late_pass(|| Box::new(unicode::Unicode)); + store.register_late_pass(|| Box::new(unit_return_expecting_ord::UnitReturnExpectingOrd)); + store.register_late_pass(|| Box::new(strings::StringAdd)); + store.register_late_pass(|| Box::new(implicit_return::ImplicitReturn)); + store.register_late_pass(|| Box::new(implicit_saturating_sub::ImplicitSaturatingSub)); + store.register_late_pass(|| Box::new(default_numeric_fallback::DefaultNumericFallback)); + store.register_late_pass(|| Box::new(inconsistent_struct_constructor::InconsistentStructConstructor)); + store.register_late_pass(|| Box::new(non_octal_unix_permissions::NonOctalUnixPermissions)); + store.register_early_pass(|| Box::new(unnecessary_self_imports::UnnecessarySelfImports)); let msrv = conf.msrv.as_ref().and_then(|s| { parse_msrv(s, None, None).or_else(|| { @@ -1899,234 +1894,233 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: }); let avoid_breaking_exported_api = conf.avoid_breaking_exported_api; - store.register_late_pass(move || box methods::Methods::new(avoid_breaking_exported_api, msrv)); - store.register_late_pass(move || box matches::Matches::new(msrv)); - store.register_early_pass(move || box manual_non_exhaustive::ManualNonExhaustive::new(msrv)); - store.register_late_pass(move || box manual_strip::ManualStrip::new(msrv)); - store.register_early_pass(move || box redundant_static_lifetimes::RedundantStaticLifetimes::new(msrv)); - store.register_early_pass(move || box redundant_field_names::RedundantFieldNames::new(msrv)); - store.register_late_pass(move || box checked_conversions::CheckedConversions::new(msrv)); - store.register_late_pass(move || box mem_replace::MemReplace::new(msrv)); - store.register_late_pass(move || box ranges::Ranges::new(msrv)); - store.register_late_pass(move || box from_over_into::FromOverInto::new(msrv)); - store.register_late_pass(move || box use_self::UseSelf::new(msrv)); - store.register_late_pass(move || box missing_const_for_fn::MissingConstForFn::new(msrv)); - store.register_late_pass(move || box needless_question_mark::NeedlessQuestionMark); - store.register_late_pass(move || box casts::Casts::new(msrv)); - store.register_early_pass(move || box unnested_or_patterns::UnnestedOrPatterns::new(msrv)); + store.register_late_pass(move || Box::new(methods::Methods::new(avoid_breaking_exported_api, msrv))); + store.register_late_pass(move || Box::new(matches::Matches::new(msrv))); + store.register_early_pass(move || Box::new(manual_non_exhaustive::ManualNonExhaustive::new(msrv))); + store.register_late_pass(move || Box::new(manual_strip::ManualStrip::new(msrv))); + store.register_early_pass(move || Box::new(redundant_static_lifetimes::RedundantStaticLifetimes::new(msrv))); + store.register_early_pass(move || Box::new(redundant_field_names::RedundantFieldNames::new(msrv))); + store.register_late_pass(move || Box::new(checked_conversions::CheckedConversions::new(msrv))); + store.register_late_pass(move || Box::new(mem_replace::MemReplace::new(msrv))); + store.register_late_pass(move || Box::new(ranges::Ranges::new(msrv))); + store.register_late_pass(move || Box::new(from_over_into::FromOverInto::new(msrv))); + store.register_late_pass(move || Box::new(use_self::UseSelf::new(msrv))); + store.register_late_pass(move || Box::new(missing_const_for_fn::MissingConstForFn::new(msrv))); + store.register_late_pass(move || Box::new(needless_question_mark::NeedlessQuestionMark)); + store.register_late_pass(move || Box::new(casts::Casts::new(msrv))); + store.register_early_pass(move || Box::new(unnested_or_patterns::UnnestedOrPatterns::new(msrv))); - store.register_late_pass(|| box size_of_in_element_count::SizeOfInElementCount); - store.register_late_pass(|| box map_clone::MapClone); - store.register_late_pass(|| box map_err_ignore::MapErrIgnore); - store.register_late_pass(|| box shadow::Shadow); - store.register_late_pass(|| box unit_types::UnitTypes); - store.register_late_pass(|| box loops::Loops); - store.register_late_pass(|| box main_recursion::MainRecursion::default()); - store.register_late_pass(|| box lifetimes::Lifetimes); - store.register_late_pass(|| box entry::HashMapPass); - store.register_late_pass(|| box minmax::MinMaxPass); - store.register_late_pass(|| box open_options::OpenOptions); - store.register_late_pass(|| box zero_div_zero::ZeroDiv); - store.register_late_pass(|| box mutex_atomic::Mutex); - store.register_late_pass(|| box needless_update::NeedlessUpdate); - store.register_late_pass(|| box needless_borrow::NeedlessBorrow::default()); - store.register_late_pass(|| box needless_borrowed_ref::NeedlessBorrowedRef); - store.register_late_pass(|| box no_effect::NoEffect); - store.register_late_pass(|| box temporary_assignment::TemporaryAssignment); - store.register_late_pass(|| box transmute::Transmute); + store.register_late_pass(|| Box::new(size_of_in_element_count::SizeOfInElementCount)); + store.register_late_pass(|| Box::new(map_clone::MapClone)); + store.register_late_pass(|| Box::new(map_err_ignore::MapErrIgnore)); + store.register_late_pass(|| Box::new(shadow::Shadow)); + store.register_late_pass(|| Box::new(unit_types::UnitTypes)); + store.register_late_pass(|| Box::new(loops::Loops)); + store.register_late_pass(|| Box::new(main_recursion::MainRecursion::default())); + store.register_late_pass(|| Box::new(lifetimes::Lifetimes)); + store.register_late_pass(|| Box::new(entry::HashMapPass)); + store.register_late_pass(|| Box::new(minmax::MinMaxPass)); + store.register_late_pass(|| Box::new(open_options::OpenOptions)); + store.register_late_pass(|| Box::new(zero_div_zero::ZeroDiv)); + store.register_late_pass(|| Box::new(mutex_atomic::Mutex)); + store.register_late_pass(|| Box::new(needless_update::NeedlessUpdate)); + store.register_late_pass(|| Box::new(needless_borrow::NeedlessBorrow::default())); + store.register_late_pass(|| Box::new(needless_borrowed_ref::NeedlessBorrowedRef)); + store.register_late_pass(|| Box::new(no_effect::NoEffect)); + store.register_late_pass(|| Box::new(temporary_assignment::TemporaryAssignment)); + store.register_late_pass(|| Box::new(transmute::Transmute)); let cognitive_complexity_threshold = conf.cognitive_complexity_threshold; - store.register_late_pass(move || box cognitive_complexity::CognitiveComplexity::new(cognitive_complexity_threshold)); + store.register_late_pass(move || Box::new(cognitive_complexity::CognitiveComplexity::new(cognitive_complexity_threshold))); let too_large_for_stack = conf.too_large_for_stack; - store.register_late_pass(move || box escape::BoxedLocal{too_large_for_stack}); - store.register_late_pass(move || box vec::UselessVec{too_large_for_stack}); - store.register_late_pass(|| box panic_unimplemented::PanicUnimplemented); - store.register_late_pass(|| box strings::StringLitAsBytes); - store.register_late_pass(|| box derive::Derive); - store.register_late_pass(|| box get_last_with_len::GetLastWithLen); - store.register_late_pass(|| box drop_forget_ref::DropForgetRef); - store.register_late_pass(|| box empty_enum::EmptyEnum); - store.register_late_pass(|| box absurd_extreme_comparisons::AbsurdExtremeComparisons); - store.register_late_pass(|| box invalid_upcast_comparisons::InvalidUpcastComparisons); - store.register_late_pass(|| box regex::Regex::default()); - store.register_late_pass(|| box copies::CopyAndPaste); - store.register_late_pass(|| box copy_iterator::CopyIterator); - store.register_late_pass(|| box format::UselessFormat); - store.register_late_pass(|| box swap::Swap); - store.register_late_pass(|| box overflow_check_conditional::OverflowCheckConditional); - store.register_late_pass(|| box new_without_default::NewWithoutDefault::default()); + store.register_late_pass(move || Box::new(escape::BoxedLocal{too_large_for_stack})); + store.register_late_pass(move || Box::new(vec::UselessVec{too_large_for_stack})); + store.register_late_pass(|| Box::new(panic_unimplemented::PanicUnimplemented)); + store.register_late_pass(|| Box::new(strings::StringLitAsBytes)); + store.register_late_pass(|| Box::new(derive::Derive)); + store.register_late_pass(|| Box::new(get_last_with_len::GetLastWithLen)); + store.register_late_pass(|| Box::new(drop_forget_ref::DropForgetRef)); + store.register_late_pass(|| Box::new(empty_enum::EmptyEnum)); + store.register_late_pass(|| Box::new(absurd_extreme_comparisons::AbsurdExtremeComparisons)); + store.register_late_pass(|| Box::new(invalid_upcast_comparisons::InvalidUpcastComparisons)); + store.register_late_pass(|| Box::new(regex::Regex::default())); + store.register_late_pass(|| Box::new(copies::CopyAndPaste)); + store.register_late_pass(|| Box::new(copy_iterator::CopyIterator)); + store.register_late_pass(|| Box::new(format::UselessFormat)); + store.register_late_pass(|| Box::new(swap::Swap)); + store.register_late_pass(|| Box::new(overflow_check_conditional::OverflowCheckConditional)); + store.register_late_pass(|| Box::new(new_without_default::NewWithoutDefault::default())); let blacklisted_names = conf.blacklisted_names.iter().cloned().collect::>(); - store.register_late_pass(move || box blacklisted_name::BlacklistedName::new(blacklisted_names.clone())); + store.register_late_pass(move || Box::new(blacklisted_name::BlacklistedName::new(blacklisted_names.clone()))); let too_many_arguments_threshold = conf.too_many_arguments_threshold; let too_many_lines_threshold = conf.too_many_lines_threshold; - store.register_late_pass(move || box functions::Functions::new(too_many_arguments_threshold, too_many_lines_threshold)); + store.register_late_pass(move || Box::new(functions::Functions::new(too_many_arguments_threshold, too_many_lines_threshold))); let doc_valid_idents = conf.doc_valid_idents.iter().cloned().collect::>(); - store.register_late_pass(move || box doc::DocMarkdown::new(doc_valid_idents.clone())); - store.register_late_pass(|| box neg_multiply::NegMultiply); - store.register_late_pass(|| box mem_discriminant::MemDiscriminant); - store.register_late_pass(|| box mem_forget::MemForget); - store.register_late_pass(|| box arithmetic::Arithmetic::default()); - store.register_late_pass(|| box assign_ops::AssignOps); - store.register_late_pass(|| box let_if_seq::LetIfSeq); - store.register_late_pass(|| box eval_order_dependence::EvalOrderDependence); - store.register_late_pass(|| box missing_doc::MissingDoc::new()); - store.register_late_pass(|| box missing_inline::MissingInline); - store.register_late_pass(move || box exhaustive_items::ExhaustiveItems); - store.register_late_pass(|| box if_let_some_result::OkIfLet); - store.register_late_pass(|| box partialeq_ne_impl::PartialEqNeImpl); - store.register_late_pass(|| box unused_io_amount::UnusedIoAmount); + store.register_late_pass(move || Box::new(doc::DocMarkdown::new(doc_valid_idents.clone()))); + store.register_late_pass(|| Box::new(neg_multiply::NegMultiply)); + store.register_late_pass(|| Box::new(mem_discriminant::MemDiscriminant)); + store.register_late_pass(|| Box::new(mem_forget::MemForget)); + store.register_late_pass(|| Box::new(arithmetic::Arithmetic::default())); + store.register_late_pass(|| Box::new(assign_ops::AssignOps)); + store.register_late_pass(|| Box::new(let_if_seq::LetIfSeq)); + store.register_late_pass(|| Box::new(eval_order_dependence::EvalOrderDependence)); + store.register_late_pass(|| Box::new(missing_doc::MissingDoc::new())); + store.register_late_pass(|| Box::new(missing_inline::MissingInline)); + store.register_late_pass(move || Box::new(exhaustive_items::ExhaustiveItems)); + store.register_late_pass(|| Box::new(if_let_some_result::OkIfLet)); + store.register_late_pass(|| Box::new(partialeq_ne_impl::PartialEqNeImpl)); + store.register_late_pass(|| Box::new(unused_io_amount::UnusedIoAmount)); let enum_variant_size_threshold = conf.enum_variant_size_threshold; - store.register_late_pass(move || box large_enum_variant::LargeEnumVariant::new(enum_variant_size_threshold)); - store.register_late_pass(|| box explicit_write::ExplicitWrite); - store.register_late_pass(|| box needless_pass_by_value::NeedlessPassByValue); + store.register_late_pass(move || Box::new(large_enum_variant::LargeEnumVariant::new(enum_variant_size_threshold))); + store.register_late_pass(|| Box::new(explicit_write::ExplicitWrite)); + store.register_late_pass(|| Box::new(needless_pass_by_value::NeedlessPassByValue)); let pass_by_ref_or_value = pass_by_ref_or_value::PassByRefOrValue::new( conf.trivial_copy_size_limit, conf.pass_by_value_size_limit, conf.avoid_breaking_exported_api, &sess.target, ); - store.register_late_pass(move || box pass_by_ref_or_value); - store.register_late_pass(|| box ref_option_ref::RefOptionRef); - store.register_late_pass(|| box try_err::TryErr); - store.register_late_pass(|| box bytecount::ByteCount); - store.register_late_pass(|| box infinite_iter::InfiniteIter); - store.register_late_pass(|| box inline_fn_without_body::InlineFnWithoutBody); - store.register_late_pass(|| box useless_conversion::UselessConversion::default()); - store.register_late_pass(|| box implicit_hasher::ImplicitHasher); - store.register_late_pass(|| box fallible_impl_from::FallibleImplFrom); - store.register_late_pass(|| box double_comparison::DoubleComparisons); - store.register_late_pass(|| box question_mark::QuestionMark); - store.register_early_pass(|| box suspicious_operation_groupings::SuspiciousOperationGroupings); - store.register_late_pass(|| box suspicious_trait_impl::SuspiciousImpl); - store.register_late_pass(|| box map_unit_fn::MapUnit); - store.register_late_pass(|| box inherent_impl::MultipleInherentImpl); - store.register_late_pass(|| box neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd); - store.register_late_pass(|| box unwrap::Unwrap); - store.register_late_pass(|| box duration_subsec::DurationSubsec); - store.register_late_pass(|| box indexing_slicing::IndexingSlicing); - store.register_late_pass(|| box non_copy_const::NonCopyConst); - store.register_late_pass(|| box ptr_offset_with_cast::PtrOffsetWithCast); - store.register_late_pass(|| box redundant_clone::RedundantClone); - store.register_late_pass(|| box slow_vector_initialization::SlowVectorInit); - store.register_late_pass(|| box unnecessary_sort_by::UnnecessarySortBy); - store.register_late_pass(move || box unnecessary_wraps::UnnecessaryWraps::new(avoid_breaking_exported_api)); - store.register_late_pass(|| box assertions_on_constants::AssertionsOnConstants); - store.register_late_pass(|| box transmuting_null::TransmutingNull); - store.register_late_pass(|| box path_buf_push_overwrite::PathBufPushOverwrite); - store.register_late_pass(|| box integer_division::IntegerDivision); - store.register_late_pass(|| box inherent_to_string::InherentToString); + store.register_late_pass(move || Box::new(pass_by_ref_or_value)); + store.register_late_pass(|| Box::new(ref_option_ref::RefOptionRef)); + store.register_late_pass(|| Box::new(try_err::TryErr)); + store.register_late_pass(|| Box::new(bytecount::ByteCount)); + store.register_late_pass(|| Box::new(infinite_iter::InfiniteIter)); + store.register_late_pass(|| Box::new(inline_fn_without_body::InlineFnWithoutBody)); + store.register_late_pass(|| Box::new(useless_conversion::UselessConversion::default())); + store.register_late_pass(|| Box::new(implicit_hasher::ImplicitHasher)); + store.register_late_pass(|| Box::new(fallible_impl_from::FallibleImplFrom)); + store.register_late_pass(|| Box::new(double_comparison::DoubleComparisons)); + store.register_late_pass(|| Box::new(question_mark::QuestionMark)); + store.register_early_pass(|| Box::new(suspicious_operation_groupings::SuspiciousOperationGroupings)); + store.register_late_pass(|| Box::new(suspicious_trait_impl::SuspiciousImpl)); + store.register_late_pass(|| Box::new(map_unit_fn::MapUnit)); + store.register_late_pass(|| Box::new(inherent_impl::MultipleInherentImpl)); + store.register_late_pass(|| Box::new(neg_cmp_op_on_partial_ord::NoNegCompOpForPartialOrd)); + store.register_late_pass(|| Box::new(unwrap::Unwrap)); + store.register_late_pass(|| Box::new(duration_subsec::DurationSubsec)); + store.register_late_pass(|| Box::new(indexing_slicing::IndexingSlicing)); + store.register_late_pass(|| Box::new(non_copy_const::NonCopyConst)); + store.register_late_pass(|| Box::new(ptr_offset_with_cast::PtrOffsetWithCast)); + store.register_late_pass(|| Box::new(redundant_clone::RedundantClone)); + store.register_late_pass(|| Box::new(slow_vector_initialization::SlowVectorInit)); + store.register_late_pass(|| Box::new(unnecessary_sort_by::UnnecessarySortBy)); + store.register_late_pass(move || Box::new(unnecessary_wraps::UnnecessaryWraps::new(avoid_breaking_exported_api))); + store.register_late_pass(|| Box::new(assertions_on_constants::AssertionsOnConstants)); + store.register_late_pass(|| Box::new(transmuting_null::TransmutingNull)); + store.register_late_pass(|| Box::new(path_buf_push_overwrite::PathBufPushOverwrite)); + store.register_late_pass(|| Box::new(integer_division::IntegerDivision)); + store.register_late_pass(|| Box::new(inherent_to_string::InherentToString)); let max_trait_bounds = conf.max_trait_bounds; - store.register_late_pass(move || box trait_bounds::TraitBounds::new(max_trait_bounds)); - store.register_late_pass(|| box comparison_chain::ComparisonChain); - store.register_late_pass(|| box mut_key::MutableKeyType); - store.register_late_pass(|| box modulo_arithmetic::ModuloArithmetic); - store.register_early_pass(|| box reference::DerefAddrOf); - store.register_early_pass(|| box reference::RefInDeref); - store.register_early_pass(|| box double_parens::DoubleParens); - store.register_late_pass(|| box to_string_in_display::ToStringInDisplay::new()); - store.register_early_pass(|| box unsafe_removed_from_name::UnsafeNameRemoval); - store.register_early_pass(|| box if_not_else::IfNotElse); - store.register_early_pass(|| box else_if_without_else::ElseIfWithoutElse); - store.register_early_pass(|| box int_plus_one::IntPlusOne); - store.register_early_pass(|| box formatting::Formatting); - store.register_early_pass(|| box misc_early::MiscEarlyLints); - store.register_early_pass(|| box redundant_closure_call::RedundantClosureCall); - store.register_late_pass(|| box redundant_closure_call::RedundantClosureCall); - store.register_early_pass(|| box unused_unit::UnusedUnit); - store.register_late_pass(|| box returns::Return); - store.register_early_pass(|| box collapsible_if::CollapsibleIf); - store.register_early_pass(|| box items_after_statements::ItemsAfterStatements); - store.register_early_pass(|| box precedence::Precedence); - store.register_early_pass(|| box needless_continue::NeedlessContinue); - store.register_early_pass(|| box redundant_else::RedundantElse); - store.register_late_pass(|| box create_dir::CreateDir); - store.register_early_pass(|| box needless_arbitrary_self_type::NeedlessArbitrarySelfType); + store.register_late_pass(move || Box::new(trait_bounds::TraitBounds::new(max_trait_bounds))); + store.register_late_pass(|| Box::new(comparison_chain::ComparisonChain)); + store.register_late_pass(|| Box::new(mut_key::MutableKeyType)); + store.register_late_pass(|| Box::new(modulo_arithmetic::ModuloArithmetic)); + store.register_early_pass(|| Box::new(reference::DerefAddrOf)); + store.register_early_pass(|| Box::new(reference::RefInDeref)); + store.register_early_pass(|| Box::new(double_parens::DoubleParens)); + store.register_late_pass(|| Box::new(to_string_in_display::ToStringInDisplay::new())); + store.register_early_pass(|| Box::new(unsafe_removed_from_name::UnsafeNameRemoval)); + store.register_early_pass(|| Box::new(if_not_else::IfNotElse)); + store.register_early_pass(|| Box::new(else_if_without_else::ElseIfWithoutElse)); + store.register_early_pass(|| Box::new(int_plus_one::IntPlusOne)); + store.register_early_pass(|| Box::new(formatting::Formatting)); + store.register_early_pass(|| Box::new(misc_early::MiscEarlyLints)); + store.register_early_pass(|| Box::new(redundant_closure_call::RedundantClosureCall)); + store.register_late_pass(|| Box::new(redundant_closure_call::RedundantClosureCall)); + store.register_early_pass(|| Box::new(unused_unit::UnusedUnit)); + store.register_late_pass(|| Box::new(returns::Return)); + store.register_early_pass(|| Box::new(collapsible_if::CollapsibleIf)); + store.register_early_pass(|| Box::new(items_after_statements::ItemsAfterStatements)); + store.register_early_pass(|| Box::new(precedence::Precedence)); + store.register_early_pass(|| Box::new(needless_continue::NeedlessContinue)); + store.register_early_pass(|| Box::new(redundant_else::RedundantElse)); + store.register_late_pass(|| Box::new(create_dir::CreateDir)); + store.register_early_pass(|| Box::new(needless_arbitrary_self_type::NeedlessArbitrarySelfType)); let cargo_ignore_publish = conf.cargo_ignore_publish; - store.register_late_pass(move || box cargo_common_metadata::CargoCommonMetadata::new(cargo_ignore_publish)); - store.register_late_pass(|| box multiple_crate_versions::MultipleCrateVersions); - store.register_late_pass(|| box wildcard_dependencies::WildcardDependencies); + store.register_late_pass(move || Box::new(cargo_common_metadata::CargoCommonMetadata::new(cargo_ignore_publish))); + store.register_late_pass(|| Box::new(multiple_crate_versions::MultipleCrateVersions)); + store.register_late_pass(|| Box::new(wildcard_dependencies::WildcardDependencies)); let literal_representation_lint_fraction_readability = conf.unreadable_literal_lint_fractions; - store.register_early_pass(move || box literal_representation::LiteralDigitGrouping::new(literal_representation_lint_fraction_readability)); + store.register_early_pass(move || Box::new(literal_representation::LiteralDigitGrouping::new(literal_representation_lint_fraction_readability))); let literal_representation_threshold = conf.literal_representation_threshold; - store.register_early_pass(move || box literal_representation::DecimalLiteralRepresentation::new(literal_representation_threshold)); + store.register_early_pass(move || Box::new(literal_representation::DecimalLiteralRepresentation::new(literal_representation_threshold))); let enum_variant_name_threshold = conf.enum_variant_name_threshold; - store.register_late_pass(move || box enum_variants::EnumVariantNames::new(enum_variant_name_threshold, avoid_breaking_exported_api)); - store.register_early_pass(|| box tabs_in_doc_comments::TabsInDocComments); + store.register_late_pass(move || Box::new(enum_variants::EnumVariantNames::new(enum_variant_name_threshold, avoid_breaking_exported_api))); + store.register_early_pass(|| Box::new(tabs_in_doc_comments::TabsInDocComments)); let upper_case_acronyms_aggressive = conf.upper_case_acronyms_aggressive; - store.register_late_pass(move || box upper_case_acronyms::UpperCaseAcronyms::new(avoid_breaking_exported_api, upper_case_acronyms_aggressive)); - store.register_late_pass(|| box default::Default::default()); - store.register_late_pass(|| box unused_self::UnusedSelf); - store.register_late_pass(|| box mutable_debug_assertion::DebugAssertWithMutCall); - store.register_late_pass(|| box exit::Exit); - store.register_late_pass(|| box to_digit_is_some::ToDigitIsSome); + store.register_late_pass(move || Box::new(upper_case_acronyms::UpperCaseAcronyms::new(avoid_breaking_exported_api, upper_case_acronyms_aggressive))); + store.register_late_pass(|| Box::new(default::Default::default())); + store.register_late_pass(|| Box::new(unused_self::UnusedSelf)); + store.register_late_pass(|| Box::new(mutable_debug_assertion::DebugAssertWithMutCall)); + store.register_late_pass(|| Box::new(exit::Exit)); + store.register_late_pass(|| Box::new(to_digit_is_some::ToDigitIsSome)); let array_size_threshold = conf.array_size_threshold; - store.register_late_pass(move || box large_stack_arrays::LargeStackArrays::new(array_size_threshold)); - store.register_late_pass(move || box large_const_arrays::LargeConstArrays::new(array_size_threshold)); - store.register_late_pass(|| box floating_point_arithmetic::FloatingPointArithmetic); - store.register_early_pass(|| box as_conversions::AsConversions); - store.register_late_pass(|| box let_underscore::LetUnderscore); - store.register_late_pass(|| box atomic_ordering::AtomicOrdering); - store.register_early_pass(|| box single_component_path_imports::SingleComponentPathImports); + store.register_late_pass(move || Box::new(large_stack_arrays::LargeStackArrays::new(array_size_threshold))); + store.register_late_pass(move || Box::new(large_const_arrays::LargeConstArrays::new(array_size_threshold))); + store.register_late_pass(|| Box::new(floating_point_arithmetic::FloatingPointArithmetic)); + store.register_early_pass(|| Box::new(as_conversions::AsConversions)); + store.register_late_pass(|| Box::new(let_underscore::LetUnderscore)); + store.register_early_pass(|| Box::new(single_component_path_imports::SingleComponentPathImports)); let max_fn_params_bools = conf.max_fn_params_bools; let max_struct_bools = conf.max_struct_bools; - store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools)); - store.register_early_pass(|| box option_env_unwrap::OptionEnvUnwrap); + store.register_early_pass(move || Box::new(excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools))); + store.register_early_pass(|| Box::new(option_env_unwrap::OptionEnvUnwrap)); let warn_on_all_wildcard_imports = conf.warn_on_all_wildcard_imports; - store.register_late_pass(move || box wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports)); - store.register_late_pass(|| box verbose_file_reads::VerboseFileReads); - store.register_late_pass(|| box redundant_pub_crate::RedundantPubCrate::default()); - store.register_late_pass(|| box unnamed_address::UnnamedAddress); - store.register_late_pass(|| box dereference::Dereferencing::default()); - store.register_late_pass(|| box option_if_let_else::OptionIfLetElse); - store.register_late_pass(|| box future_not_send::FutureNotSend); - store.register_late_pass(|| box if_let_mutex::IfLetMutex); - store.register_late_pass(|| box mut_mutex_lock::MutMutexLock); - store.register_late_pass(|| box match_on_vec_items::MatchOnVecItems); - store.register_late_pass(|| box manual_async_fn::ManualAsyncFn); - store.register_late_pass(|| box vec_resize_to_zero::VecResizeToZero); - store.register_late_pass(|| box panic_in_result_fn::PanicInResultFn); + store.register_late_pass(move || Box::new(wildcard_imports::WildcardImports::new(warn_on_all_wildcard_imports))); + store.register_late_pass(|| Box::new(verbose_file_reads::VerboseFileReads)); + store.register_late_pass(|| Box::new(redundant_pub_crate::RedundantPubCrate::default())); + store.register_late_pass(|| Box::new(unnamed_address::UnnamedAddress)); + store.register_late_pass(|| Box::new(dereference::Dereferencing::default())); + store.register_late_pass(|| Box::new(option_if_let_else::OptionIfLetElse)); + store.register_late_pass(|| Box::new(future_not_send::FutureNotSend)); + store.register_late_pass(|| Box::new(if_let_mutex::IfLetMutex)); + store.register_late_pass(|| Box::new(mut_mutex_lock::MutMutexLock)); + store.register_late_pass(|| Box::new(match_on_vec_items::MatchOnVecItems)); + store.register_late_pass(|| Box::new(manual_async_fn::ManualAsyncFn)); + store.register_late_pass(|| Box::new(vec_resize_to_zero::VecResizeToZero)); + store.register_late_pass(|| Box::new(panic_in_result_fn::PanicInResultFn)); let single_char_binding_names_threshold = conf.single_char_binding_names_threshold; - store.register_early_pass(move || box non_expressive_names::NonExpressiveNames { + store.register_early_pass(move || Box::new(non_expressive_names::NonExpressiveNames { single_char_binding_names_threshold, - }); + })); let macro_matcher = conf.standard_macro_braces.iter().cloned().collect::>(); - store.register_early_pass(move || box nonstandard_macro_braces::MacroBraces::new(¯o_matcher)); - store.register_late_pass(|| box macro_use::MacroUseImports::default()); - store.register_late_pass(|| box pattern_type_mismatch::PatternTypeMismatch); - store.register_late_pass(|| box stable_sort_primitive::StableSortPrimitive); - store.register_late_pass(|| box repeat_once::RepeatOnce); - store.register_late_pass(|| box unwrap_in_result::UnwrapInResult); - store.register_late_pass(|| box self_assignment::SelfAssignment); - store.register_late_pass(|| box manual_unwrap_or::ManualUnwrapOr); - store.register_late_pass(|| box manual_ok_or::ManualOkOr); - store.register_late_pass(|| box float_equality_without_abs::FloatEqualityWithoutAbs); - store.register_late_pass(|| box semicolon_if_nothing_returned::SemicolonIfNothingReturned); - store.register_late_pass(|| box async_yields_async::AsyncYieldsAsync); + store.register_early_pass(move || Box::new(nonstandard_macro_braces::MacroBraces::new(¯o_matcher))); + store.register_late_pass(|| Box::new(macro_use::MacroUseImports::default())); + store.register_late_pass(|| Box::new(pattern_type_mismatch::PatternTypeMismatch)); + store.register_late_pass(|| Box::new(stable_sort_primitive::StableSortPrimitive)); + store.register_late_pass(|| Box::new(repeat_once::RepeatOnce)); + store.register_late_pass(|| Box::new(unwrap_in_result::UnwrapInResult)); + store.register_late_pass(|| Box::new(self_assignment::SelfAssignment)); + store.register_late_pass(|| Box::new(manual_unwrap_or::ManualUnwrapOr)); + store.register_late_pass(|| Box::new(manual_ok_or::ManualOkOr)); + store.register_late_pass(|| Box::new(float_equality_without_abs::FloatEqualityWithoutAbs)); + store.register_late_pass(|| Box::new(semicolon_if_nothing_returned::SemicolonIfNothingReturned)); + store.register_late_pass(|| Box::new(async_yields_async::AsyncYieldsAsync)); let disallowed_methods = conf.disallowed_methods.iter().cloned().collect::>(); - store.register_late_pass(move || box disallowed_method::DisallowedMethod::new(&disallowed_methods)); - store.register_early_pass(|| box asm_syntax::InlineAsmX86AttSyntax); - store.register_early_pass(|| box asm_syntax::InlineAsmX86IntelSyntax); - store.register_late_pass(|| box undropped_manually_drops::UndroppedManuallyDrops); - store.register_late_pass(|| box strings::StrToString); - store.register_late_pass(|| box strings::StringToString); - store.register_late_pass(|| box zero_sized_map_values::ZeroSizedMapValues); - store.register_late_pass(|| box vec_init_then_push::VecInitThenPush::default()); - store.register_late_pass(|| box case_sensitive_file_extension_comparisons::CaseSensitiveFileExtensionComparisons); - store.register_late_pass(|| box redundant_slicing::RedundantSlicing); - store.register_late_pass(|| box from_str_radix_10::FromStrRadix10); - store.register_late_pass(|| box manual_map::ManualMap); - store.register_late_pass(move || box if_then_some_else_none::IfThenSomeElseNone::new(msrv)); - store.register_late_pass(|| box bool_assert_comparison::BoolAssertComparison); - store.register_early_pass(move || box module_style::ModStyle); - store.register_late_pass(|| box unused_async::UnusedAsync); + store.register_late_pass(move || Box::new(disallowed_method::DisallowedMethod::new(&disallowed_methods))); + store.register_early_pass(|| Box::new(asm_syntax::InlineAsmX86AttSyntax)); + store.register_early_pass(|| Box::new(asm_syntax::InlineAsmX86IntelSyntax)); + store.register_late_pass(|| Box::new(undropped_manually_drops::UndroppedManuallyDrops)); + store.register_late_pass(|| Box::new(strings::StrToString)); + store.register_late_pass(|| Box::new(strings::StringToString)); + store.register_late_pass(|| Box::new(zero_sized_map_values::ZeroSizedMapValues)); + store.register_late_pass(|| Box::new(vec_init_then_push::VecInitThenPush::default())); + store.register_late_pass(|| Box::new(case_sensitive_file_extension_comparisons::CaseSensitiveFileExtensionComparisons)); + store.register_late_pass(|| Box::new(redundant_slicing::RedundantSlicing)); + store.register_late_pass(|| Box::new(from_str_radix_10::FromStrRadix10)); + store.register_late_pass(|| Box::new(manual_map::ManualMap)); + store.register_late_pass(move || Box::new(if_then_some_else_none::IfThenSomeElseNone::new(msrv))); + store.register_late_pass(|| Box::new(bool_assert_comparison::BoolAssertComparison)); + store.register_early_pass(move || Box::new(module_style::ModStyle)); + store.register_late_pass(|| Box::new(unused_async::UnusedAsync)); let disallowed_types = conf.disallowed_types.iter().cloned().collect::>(); - store.register_late_pass(move || box disallowed_type::DisallowedType::new(&disallowed_types)); + store.register_late_pass(move || Box::new(disallowed_type::DisallowedType::new(&disallowed_types))); let import_renames = conf.enforced_import_renames.clone(); - store.register_late_pass(move || box missing_enforced_import_rename::ImportRename::new(import_renames.clone())); + store.register_late_pass(move || Box::new(missing_enforced_import_rename::ImportRename::new(import_renames.clone()))); let scripts = conf.allowed_scripts.clone(); - store.register_early_pass(move || box disallowed_script_idents::DisallowedScriptIdents::new(&scripts)); - store.register_late_pass(|| box strlen_on_c_strings::StrlenOnCStrings); - store.register_late_pass(move || box self_named_constructors::SelfNamedConstructors); - store.register_late_pass(move || box feature_name::FeatureName); + store.register_early_pass(move || Box::new(disallowed_script_idents::DisallowedScriptIdents::new(&scripts))); + store.register_late_pass(|| Box::new(strlen_on_c_strings::StrlenOnCStrings)); + store.register_late_pass(move || Box::new(self_named_constructors::SelfNamedConstructors)); + store.register_late_pass(move || Box::new(feature_name::FeatureName)); } #[rustfmt::skip] @@ -2205,6 +2199,7 @@ pub fn register_renamed(ls: &mut rustc_lint::LintStore) { ls.register_renamed("clippy::temporary_cstring_as_ptr", "temporary_cstring_as_ptr"); ls.register_renamed("clippy::panic_params", "non_fmt_panics"); ls.register_renamed("clippy::unknown_clippy_lints", "unknown_lints"); + ls.register_renamed("clippy::invalid_atomic_ordering", "invalid_atomic_ordering"); } // only exists to let the dogfood integration test works. diff --git a/clippy_lints/src/loops/manual_flatten.rs b/clippy_lints/src/loops/manual_flatten.rs index 2af6cfe35b54..5b6e27085d58 100644 --- a/clippy_lints/src/loops/manual_flatten.rs +++ b/clippy_lints/src/loops/manual_flatten.rs @@ -1,12 +1,13 @@ use super::utils::make_iterator_snippet; use super::MANUAL_FLATTEN; use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::higher; use clippy_utils::visitors::is_local_used; use clippy_utils::{is_lang_ctor, path_to_local_id}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::LangItem::{OptionSome, ResultOk}; -use rustc_hir::{Expr, ExprKind, MatchSource, Pat, PatKind, StmtKind}; +use rustc_hir::{Expr, ExprKind, Pat, PatKind, StmtKind}; use rustc_lint::LateContext; use rustc_middle::ty; use rustc_span::source_map::Span; @@ -37,19 +38,18 @@ pub(super) fn check<'tcx>( if_chain! { if let Some(inner_expr) = inner_expr; - if let ExprKind::Match( - match_expr, [true_arm, _else_arm], MatchSource::IfLetDesugar{ contains_else_clause: false } - ) = inner_expr.kind; + if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else: None }) + = higher::IfLet::hir(cx, inner_expr); // Ensure match_expr in `if let` statement is the same as the pat from the for-loop if let PatKind::Binding(_, pat_hir_id, _, _) = pat.kind; - if path_to_local_id(match_expr, pat_hir_id); + if path_to_local_id(let_expr, pat_hir_id); // Ensure the `if let` statement is for the `Some` variant of `Option` or the `Ok` variant of `Result` - if let PatKind::TupleStruct(ref qpath, _, _) = true_arm.pat.kind; + if let PatKind::TupleStruct(ref qpath, _, _) = let_pat.kind; let some_ctor = is_lang_ctor(cx, qpath, OptionSome); let ok_ctor = is_lang_ctor(cx, qpath, ResultOk); if some_ctor || ok_ctor; // Ensure epxr in `if let` is not used afterwards - if !is_local_used(cx, true_arm, pat_hir_id); + if !is_local_used(cx, if_then, pat_hir_id); then { let if_let_type = if some_ctor { "Some" } else { "Ok" }; // Prepare the error message @@ -58,7 +58,7 @@ pub(super) fn check<'tcx>( // Prepare the help message let mut applicability = Applicability::MaybeIncorrect; let arg_snippet = make_iterator_snippet(cx, arg, &mut applicability); - let copied = match cx.typeck_results().expr_ty(match_expr).kind() { + let copied = match cx.typeck_results().expr_ty(let_expr).kind() { ty::Ref(_, inner, _) => match inner.kind() { ty::Ref(..) => ".copied()", _ => "" diff --git a/clippy_lints/src/loops/manual_memcpy.rs b/clippy_lints/src/loops/manual_memcpy.rs index 7d5ed3ab0a75..df848e68802c 100644 --- a/clippy_lints/src/loops/manual_memcpy.rs +++ b/clippy_lints/src/loops/manual_memcpy.rs @@ -27,7 +27,7 @@ pub(super) fn check<'tcx>( start: Some(start), end: Some(end), limits, - }) = higher::range(arg) + }) = higher::Range::hir(arg) { // the var must be a single name if let PatKind::Binding(_, canonical_id, _, _) = pat.kind { @@ -265,7 +265,7 @@ impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> { } } -/// a wrapper around `MinifyingSugg`, which carries a operator like currying +/// a wrapper around `MinifyingSugg`, which carries an operator like currying /// so that the suggested code become more efficient (e.g. `foo + -bar` `foo - bar`). struct Offset { value: MinifyingSugg<'static>, diff --git a/clippy_lints/src/loops/mod.rs b/clippy_lints/src/loops/mod.rs index 7ca54d539720..97cbe2c53dda 100644 --- a/clippy_lints/src/loops/mod.rs +++ b/clippy_lints/src/loops/mod.rs @@ -551,7 +551,7 @@ declare_lint_pass!(Loops => [ impl<'tcx> LateLintPass<'tcx> for Loops { #[allow(clippy::too_many_lines)] fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if let Some((pat, arg, body, span)) = higher::for_loop(expr) { + if let Some(higher::ForLoop { pat, arg, body, span }) = higher::ForLoop::hir(expr) { // we don't want to check expanded macros // this check is not at the top of the function // since higher::for_loop expressions are marked as expansions @@ -580,8 +580,8 @@ impl<'tcx> LateLintPass<'tcx> for Loops { while_let_on_iterator::check(cx, expr); - if let Some((cond, body)) = higher::while_loop(expr) { - while_immutable_condition::check(cx, cond, body); + if let Some(higher::While { condition, body }) = higher::While::hir(expr) { + while_immutable_condition::check(cx, condition, body); } needless_collect::check(expr, cx); diff --git a/clippy_lints/src/loops/mut_range_bound.rs b/clippy_lints/src/loops/mut_range_bound.rs index 1e54a1e2de16..344dc5074d36 100644 --- a/clippy_lints/src/loops/mut_range_bound.rs +++ b/clippy_lints/src/loops/mut_range_bound.rs @@ -14,7 +14,7 @@ pub(super) fn check(cx: &LateContext<'_>, arg: &Expr<'_>, body: &Expr<'_>) { start: Some(start), end: Some(end), .. - }) = higher::range(arg) + }) = higher::Range::hir(arg) { let mut_ids = vec![check_for_mutability(cx, start), check_for_mutability(cx, end)]; if mut_ids[0].is_some() || mut_ids[1].is_some() { diff --git a/clippy_lints/src/loops/needless_range_loop.rs b/clippy_lints/src/loops/needless_range_loop.rs index fa33d068d3d8..f17e76d9682c 100644 --- a/clippy_lints/src/loops/needless_range_loop.rs +++ b/clippy_lints/src/loops/needless_range_loop.rs @@ -32,7 +32,7 @@ pub(super) fn check<'tcx>( start: Some(start), ref end, limits, - }) = higher::range(arg) + }) = higher::Range::hir(arg) { // the var must be a single name if let PatKind::Binding(_, canonical_id, ident, _) = pat.kind { diff --git a/clippy_lints/src/loops/never_loop.rs b/clippy_lints/src/loops/never_loop.rs index 6d9f6215ed41..2c46971d5f74 100644 --- a/clippy_lints/src/loops/never_loop.rs +++ b/clippy_lints/src/loops/never_loop.rs @@ -1,7 +1,7 @@ use super::utils::make_iterator_snippet; use super::NEVER_LOOP; use clippy_utils::diagnostics::span_lint_and_then; -use clippy_utils::higher; +use clippy_utils::higher::ForLoop; use clippy_utils::source::snippet; use rustc_errors::Applicability; use rustc_hir::{Block, Expr, ExprKind, HirId, InlineAsmOperand, LoopSource, Node, Pat, Stmt, StmtKind}; @@ -16,7 +16,7 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { if_chain! { if let LoopSource::ForLoop = source; if let Some((_, Node::Expr(parent_match))) = cx.tcx.hir().parent_iter(expr.hir_id).nth(1); - if let Some((pat, iterator, _, for_span)) = higher::for_loop(parent_match); + if let Some(ForLoop { arg: iterator, pat, span: for_span, .. }) = ForLoop::hir(parent_match); then { // Suggests using an `if let` instead. This is `Unspecified` because the // loop may (probably) contain `break` statements which would be invalid @@ -111,6 +111,7 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult { | ExprKind::Unary(_, e) | ExprKind::Cast(e, _) | ExprKind::Type(e, _) + | ExprKind::Let(_, e, _) | ExprKind::Field(e, _) | ExprKind::AddrOf(_, _, e) | ExprKind::Struct(_, _, Some(e)) @@ -128,7 +129,7 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult { // Break can come from the inner loop so remove them. absorb_break(&never_loop_block(b, main_loop_id)) }, - ExprKind::If(e, e2, ref e3) => { + ExprKind::If(e, e2, e3) => { let e1 = never_loop_expr(e, main_loop_id); let e2 = never_loop_expr(e2, main_loop_id); let e3 = e3 @@ -156,7 +157,7 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult { NeverLoopResult::AlwaysBreak } }, - ExprKind::Break(_, ref e) | ExprKind::Ret(ref e) => e.as_ref().map_or(NeverLoopResult::AlwaysBreak, |e| { + ExprKind::Break(_, e) | ExprKind::Ret(e) => e.as_ref().map_or(NeverLoopResult::AlwaysBreak, |e| { combine_seq(never_loop_expr(e, main_loop_id), NeverLoopResult::AlwaysBreak) }), ExprKind::InlineAsm(asm) => asm diff --git a/clippy_lints/src/loops/same_item_push.rs b/clippy_lints/src/loops/same_item_push.rs index 0f6cd5de761f..545498a10478 100644 --- a/clippy_lints/src/loops/same_item_push.rs +++ b/clippy_lints/src/loops/same_item_push.rs @@ -49,7 +49,7 @@ pub(super) fn check<'tcx>( if same_item_push_visitor.should_lint(); if let Some((vec, pushed_item)) = same_item_push_visitor.vec_push; let vec_ty = cx.typeck_results().expr_ty(vec); - let ty = vec_ty.walk().nth(1).unwrap().expect_ty(); + let ty = vec_ty.walk(cx.tcx).nth(1).unwrap().expect_ty(); if cx .tcx .lang_items() diff --git a/clippy_lints/src/loops/while_let_loop.rs b/clippy_lints/src/loops/while_let_loop.rs index 9c1720798529..1848f5b5de2f 100644 --- a/clippy_lints/src/loops/while_let_loop.rs +++ b/clippy_lints/src/loops/while_let_loop.rs @@ -1,8 +1,9 @@ use super::WHILE_LET_LOOP; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::higher; use clippy_utils::source::snippet_with_applicability; use rustc_errors::Applicability; -use rustc_hir::{Block, Expr, ExprKind, MatchSource, StmtKind}; +use rustc_hir::{Block, Expr, ExprKind, MatchSource, Pat, StmtKind}; use rustc_lint::{LateContext, LintContext}; use rustc_middle::lint::in_external_macro; @@ -11,41 +12,25 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &' let inner_stmt_expr = extract_expr_from_first_stmt(loop_block); // or extract the first expression (if any) from the block if let Some(inner) = inner_stmt_expr.or_else(|| extract_first_expr(loop_block)) { - if let ExprKind::Match(matchexpr, arms, ref source) = inner.kind { - // ensure "if let" compatible match structure - match *source { - MatchSource::Normal | MatchSource::IfLetDesugar { .. } => { - if arms.len() == 2 - && arms[0].guard.is_none() - && arms[1].guard.is_none() - && is_simple_break_expr(arms[1].body) - { - if in_external_macro(cx.sess(), expr.span) { - return; - } + if let Some(higher::IfLet { + let_pat, + let_expr, + if_else: Some(if_else), + .. + }) = higher::IfLet::hir(cx, inner) + { + if is_simple_break_expr(if_else) { + could_be_while_let(cx, expr, let_pat, let_expr); + } + } - // NOTE: we used to build a body here instead of using - // ellipsis, this was removed because: - // 1) it was ugly with big bodies; - // 2) it was not indented properly; - // 3) it wasn’t very smart (see #675). - let mut applicability = Applicability::HasPlaceholders; - span_lint_and_sugg( - cx, - WHILE_LET_LOOP, - expr.span, - "this loop could be written as a `while let` loop", - "try", - format!( - "while let {} = {} {{ .. }}", - snippet_with_applicability(cx, arms[0].pat.span, "..", &mut applicability), - snippet_with_applicability(cx, matchexpr.span, "..", &mut applicability), - ), - applicability, - ); - } - }, - _ => (), + if let ExprKind::Match(matchexpr, arms, MatchSource::Normal) = inner.kind { + if arms.len() == 2 + && arms[0].guard.is_none() + && arms[1].guard.is_none() + && is_simple_break_expr(arms[1].body) + { + could_be_while_let(cx, expr, arms[0].pat, matchexpr); } } } @@ -54,14 +39,12 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &' /// If a block begins with a statement (possibly a `let` binding) and has an /// expression, return it. fn extract_expr_from_first_stmt<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> { - if block.stmts.is_empty() { - return None; - } - if let StmtKind::Local(local) = block.stmts[0].kind { - local.init //.map(|expr| expr) - } else { - None + if let Some(first_stmt) = block.stmts.get(0) { + if let StmtKind::Local(local) = first_stmt.kind { + return local.init; + } } + None } /// If a block begins with an expression (with or without semicolon), return it. @@ -86,3 +69,34 @@ fn is_simple_break_expr(expr: &Expr<'_>) -> bool { _ => false, } } + +fn could_be_while_let<'tcx>( + cx: &LateContext<'tcx>, + expr: &'tcx Expr<'_>, + let_pat: &'tcx Pat<'_>, + let_expr: &'tcx Expr<'_>, +) { + if in_external_macro(cx.sess(), expr.span) { + return; + } + + // NOTE: we used to build a body here instead of using + // ellipsis, this was removed because: + // 1) it was ugly with big bodies; + // 2) it was not indented properly; + // 3) it wasn’t very smart (see #675). + let mut applicability = Applicability::HasPlaceholders; + span_lint_and_sugg( + cx, + WHILE_LET_LOOP, + expr.span, + "this loop could be written as a `while let` loop", + "try", + format!( + "while let {} = {} {{ .. }}", + snippet_with_applicability(cx, let_pat.span, "..", &mut applicability), + snippet_with_applicability(cx, let_expr.span, "..", &mut applicability), + ), + applicability, + ); +} diff --git a/clippy_lints/src/loops/while_let_on_iterator.rs b/clippy_lints/src/loops/while_let_on_iterator.rs index ef822e0cbe54..79527e3bfa92 100644 --- a/clippy_lints/src/loops/while_let_on_iterator.rs +++ b/clippy_lints/src/loops/while_let_on_iterator.rs @@ -1,5 +1,6 @@ use super::WHILE_LET_ON_ITERATOR; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::higher; use clippy_utils::source::snippet_with_applicability; use clippy_utils::{ get_enclosing_loop_or_closure, is_refutable, is_trait_method, match_def_path, paths, visitors::is_res_used, @@ -7,27 +8,26 @@ use clippy_utils::{ use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor}; -use rustc_hir::{def::Res, Expr, ExprKind, HirId, Local, MatchSource, Mutability, Node, PatKind, QPath, UnOp}; +use rustc_hir::{def::Res, Expr, ExprKind, HirId, Local, Mutability, PatKind, QPath, UnOp}; use rustc_lint::LateContext; use rustc_span::{symbol::sym, Span, Symbol}; pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { let (scrutinee_expr, iter_expr, some_pat, loop_expr) = if_chain! { - if let ExprKind::Match(scrutinee_expr, [arm, _], MatchSource::WhileLetDesugar) = expr.kind; + if let Some(higher::WhileLet { if_then, let_pat, let_expr }) = higher::WhileLet::hir(expr); // check for `Some(..)` pattern - if let PatKind::TupleStruct(QPath::Resolved(None, pat_path), some_pat, _) = arm.pat.kind; + if let PatKind::TupleStruct(QPath::Resolved(None, pat_path), some_pat, _) = let_pat.kind; if let Res::Def(_, pat_did) = pat_path.res; if match_def_path(cx, pat_did, &paths::OPTION_SOME); // check for call to `Iterator::next` - if let ExprKind::MethodCall(method_name, _, [iter_expr], _) = scrutinee_expr.kind; + if let ExprKind::MethodCall(method_name, _, [iter_expr], _) = let_expr.kind; if method_name.ident.name == sym::next; - if is_trait_method(cx, scrutinee_expr, sym::Iterator); - if let Some(iter_expr) = try_parse_iter_expr(cx, iter_expr); + if is_trait_method(cx, let_expr, sym::Iterator); + if let Some(iter_expr_struct) = try_parse_iter_expr(cx, iter_expr); // get the loop containing the match expression - if let Some((_, Node::Expr(loop_expr))) = cx.tcx.hir().parent_iter(expr.hir_id).nth(1); - if !uses_iter(cx, &iter_expr, arm.body); + if !uses_iter(cx, &iter_expr_struct, if_then); then { - (scrutinee_expr, iter_expr, some_pat, loop_expr) + (let_expr, iter_expr_struct, some_pat, expr) } else { return; } @@ -81,6 +81,7 @@ struct IterExpr { /// The path being used. path: Res, } + /// Parses any expression to find out which field of which variable is used. Will return `None` if /// the expression might have side effects. fn try_parse_iter_expr(cx: &LateContext<'_>, mut e: &Expr<'_>) -> Option { @@ -285,6 +286,7 @@ fn needs_mutable_borrow(cx: &LateContext<'tcx>, iter_expr: &IterExpr, loop_expr: } impl Visitor<'tcx> for NestedLoopVisitor<'a, 'b, 'tcx> { type Map = ErasedMap<'tcx>; + fn nested_visit_map(&mut self) -> NestedVisitorMap { NestedVisitorMap::None } diff --git a/clippy_lints/src/manual_map.rs b/clippy_lints/src/manual_map.rs index 3ea88f52a1cc..b5f573cb104e 100644 --- a/clippy_lints/src/manual_map.rs +++ b/clippy_lints/src/manual_map.rs @@ -1,5 +1,6 @@ use crate::{map_unit_fn::OPTION_MAP_UNIT_FN, matches::MATCH_AS_REF}; use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::higher::IfLetOrMatch; use clippy_utils::source::{snippet_with_applicability, snippet_with_context}; use clippy_utils::ty::{is_type_diagnostic_item, peel_mid_ty_refs_is_mutable}; use clippy_utils::{ @@ -10,7 +11,7 @@ use rustc_ast::util::parser::PREC_POSTFIX; use rustc_errors::Applicability; use rustc_hir::LangItem::{OptionNone, OptionSome}; use rustc_hir::{ - def::Res, Arm, BindingAnnotation, Block, Expr, ExprKind, HirId, MatchSource, Mutability, Pat, PatKind, Path, QPath, + def::Res, Arm, BindingAnnotation, Block, Expr, ExprKind, HirId, Mutability, Pat, PatKind, Path, QPath, }; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; @@ -45,176 +46,168 @@ declare_lint_pass!(ManualMap => [MANUAL_MAP]); impl LateLintPass<'_> for ManualMap { #[allow(clippy::too_many_lines)] fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if let ExprKind::Match( - scrutinee, - [arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }], - match_kind, - ) = expr.kind - { - if in_external_macro(cx.sess(), expr.span) || in_constant(cx, expr.hir_id) { - return; - } + let (scrutinee, then_pat, then_body, else_pat, else_body) = match IfLetOrMatch::parse(cx, expr) { + Some(IfLetOrMatch::IfLet(scrutinee, pat, body, Some(r#else))) => (scrutinee, pat, body, None, r#else), + Some(IfLetOrMatch::Match( + scrutinee, + [arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }], + _, + )) => (scrutinee, arm1.pat, arm1.body, Some(arm2.pat), arm2.body), + _ => return, + }; + if in_external_macro(cx.sess(), expr.span) || in_constant(cx, expr.hir_id) { + return; + } - let (scrutinee_ty, ty_ref_count, ty_mutability) = - peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(scrutinee)); - if !(is_type_diagnostic_item(cx, scrutinee_ty, sym::option_type) - && is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::option_type)) - { - return; - } + let (scrutinee_ty, ty_ref_count, ty_mutability) = + peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(scrutinee)); + if !(is_type_diagnostic_item(cx, scrutinee_ty, sym::option_type) + && is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::option_type)) + { + return; + } - let expr_ctxt = expr.span.ctxt(); - let (some_expr, some_pat, pat_ref_count, is_wild_none) = match ( - try_parse_pattern(cx, arm1.pat, expr_ctxt), - try_parse_pattern(cx, arm2.pat, expr_ctxt), - ) { - (Some(OptionPat::Wild), Some(OptionPat::Some { pattern, ref_count })) - if is_none_expr(cx, arm1.body) => - { - (arm2.body, pattern, ref_count, true) - }, - (Some(OptionPat::None), Some(OptionPat::Some { pattern, ref_count })) - if is_none_expr(cx, arm1.body) => - { - (arm2.body, pattern, ref_count, false) - }, - (Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::Wild)) - if is_none_expr(cx, arm2.body) => - { - (arm1.body, pattern, ref_count, true) - }, - (Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::None)) - if is_none_expr(cx, arm2.body) => - { - (arm1.body, pattern, ref_count, false) - }, - _ => return, - }; + let expr_ctxt = expr.span.ctxt(); + let (some_expr, some_pat, pat_ref_count, is_wild_none) = match ( + try_parse_pattern(cx, then_pat, expr_ctxt), + else_pat.map_or(Some(OptionPat::Wild), |p| try_parse_pattern(cx, p, expr_ctxt)), + ) { + (Some(OptionPat::Wild), Some(OptionPat::Some { pattern, ref_count })) if is_none_expr(cx, then_body) => { + (else_body, pattern, ref_count, true) + }, + (Some(OptionPat::None), Some(OptionPat::Some { pattern, ref_count })) if is_none_expr(cx, then_body) => { + (else_body, pattern, ref_count, false) + }, + (Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::Wild)) if is_none_expr(cx, else_body) => { + (then_body, pattern, ref_count, true) + }, + (Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::None)) if is_none_expr(cx, else_body) => { + (then_body, pattern, ref_count, false) + }, + _ => return, + }; - // Top level or patterns aren't allowed in closures. - if matches!(some_pat.kind, PatKind::Or(_)) { - return; - } + // Top level or patterns aren't allowed in closures. + if matches!(some_pat.kind, PatKind::Or(_)) { + return; + } - let some_expr = match get_some_expr(cx, some_expr, expr_ctxt) { - Some(expr) => expr, - None => return, - }; + let some_expr = match get_some_expr(cx, some_expr, expr_ctxt) { + Some(expr) => expr, + None => return, + }; - // These two lints will go back and forth with each other. - if cx.typeck_results().expr_ty(some_expr) == cx.tcx.types.unit - && !is_lint_allowed(cx, OPTION_MAP_UNIT_FN, expr.hir_id) - { - return; - } + // These two lints will go back and forth with each other. + if cx.typeck_results().expr_ty(some_expr) == cx.tcx.types.unit + && !is_lint_allowed(cx, OPTION_MAP_UNIT_FN, expr.hir_id) + { + return; + } - // `map` won't perform any adjustments. - if !cx.typeck_results().expr_adjustments(some_expr).is_empty() { - return; - } + // `map` won't perform any adjustments. + if !cx.typeck_results().expr_adjustments(some_expr).is_empty() { + return; + } - // Determine which binding mode to use. - let explicit_ref = some_pat.contains_explicit_ref_binding(); - let binding_ref = explicit_ref.or_else(|| (ty_ref_count != pat_ref_count).then(|| ty_mutability)); + // Determine which binding mode to use. + let explicit_ref = some_pat.contains_explicit_ref_binding(); + let binding_ref = explicit_ref.or_else(|| (ty_ref_count != pat_ref_count).then(|| ty_mutability)); - let as_ref_str = match binding_ref { - Some(Mutability::Mut) => ".as_mut()", - Some(Mutability::Not) => ".as_ref()", - None => "", - }; + let as_ref_str = match binding_ref { + Some(Mutability::Mut) => ".as_mut()", + Some(Mutability::Not) => ".as_ref()", + None => "", + }; - match can_move_expr_to_closure(cx, some_expr) { - Some(captures) => { - // Check if captures the closure will need conflict with borrows made in the scrutinee. - // TODO: check all the references made in the scrutinee expression. This will require interacting - // with the borrow checker. Currently only `[.]*` is checked for. - if let Some(binding_ref_mutability) = binding_ref { - let e = peel_hir_expr_while(scrutinee, |e| match e.kind { - ExprKind::Field(e, _) | ExprKind::AddrOf(_, _, e) => Some(e), - _ => None, - }); - if let ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(l), .. })) = e.kind { - match captures.get(l) { - Some(CaptureKind::Value | CaptureKind::Ref(Mutability::Mut)) => return, - Some(CaptureKind::Ref(Mutability::Not)) - if binding_ref_mutability == Mutability::Mut => - { - return; - } - Some(CaptureKind::Ref(Mutability::Not)) | None => (), - } + match can_move_expr_to_closure(cx, some_expr) { + Some(captures) => { + // Check if captures the closure will need conflict with borrows made in the scrutinee. + // TODO: check all the references made in the scrutinee expression. This will require interacting + // with the borrow checker. Currently only `[.]*` is checked for. + if let Some(binding_ref_mutability) = binding_ref { + let e = peel_hir_expr_while(scrutinee, |e| match e.kind { + ExprKind::Field(e, _) | ExprKind::AddrOf(_, _, e) => Some(e), + _ => None, + }); + if let ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(l), .. })) = e.kind { + match captures.get(l) { + Some(CaptureKind::Value | CaptureKind::Ref(Mutability::Mut)) => return, + Some(CaptureKind::Ref(Mutability::Not)) if binding_ref_mutability == Mutability::Mut => { + return; + }, + Some(CaptureKind::Ref(Mutability::Not)) | None => (), } } - }, - None => return, - }; - - let mut app = Applicability::MachineApplicable; + } + }, + None => return, + }; - // Remove address-of expressions from the scrutinee. Either `as_ref` will be called, or - // it's being passed by value. - let scrutinee = peel_hir_expr_refs(scrutinee).0; - let (scrutinee_str, _) = snippet_with_context(cx, scrutinee.span, expr_ctxt, "..", &mut app); - let scrutinee_str = - if scrutinee.span.ctxt() == expr.span.ctxt() && scrutinee.precedence().order() < PREC_POSTFIX { - format!("({})", scrutinee_str) - } else { - scrutinee_str.into() - }; + let mut app = Applicability::MachineApplicable; - let body_str = if let PatKind::Binding(annotation, id, some_binding, None) = some_pat.kind { - match can_pass_as_func(cx, id, some_expr) { - Some(func) if func.span.ctxt() == some_expr.span.ctxt() => { - snippet_with_applicability(cx, func.span, "..", &mut app).into_owned() - }, - _ => { - if path_to_local_id(some_expr, id) - && !is_lint_allowed(cx, MATCH_AS_REF, expr.hir_id) - && binding_ref.is_some() - { - return; - } - - // `ref` and `ref mut` annotations were handled earlier. - let annotation = if matches!(annotation, BindingAnnotation::Mutable) { - "mut " - } else { - "" - }; - format!( - "|{}{}| {}", - annotation, - some_binding, - snippet_with_context(cx, some_expr.span, expr_ctxt, "..", &mut app).0 - ) - }, - } - } else if !is_wild_none && explicit_ref.is_none() { - // TODO: handle explicit reference annotations. - format!( - "|{}| {}", - snippet_with_context(cx, some_pat.span, expr_ctxt, "..", &mut app).0, - snippet_with_context(cx, some_expr.span, expr_ctxt, "..", &mut app).0 - ) + // Remove address-of expressions from the scrutinee. Either `as_ref` will be called, or + // it's being passed by value. + let scrutinee = peel_hir_expr_refs(scrutinee).0; + let (scrutinee_str, _) = snippet_with_context(cx, scrutinee.span, expr_ctxt, "..", &mut app); + let scrutinee_str = + if scrutinee.span.ctxt() == expr.span.ctxt() && scrutinee.precedence().order() < PREC_POSTFIX { + format!("({})", scrutinee_str) } else { - // Refutable bindings and mixed reference annotations can't be handled by `map`. - return; + scrutinee_str.into() }; - span_lint_and_sugg( - cx, - MANUAL_MAP, - expr.span, - "manual implementation of `Option::map`", - "try this", - if matches!(match_kind, MatchSource::IfLetDesugar { .. }) && is_else_clause(cx.tcx, expr) { - format!("{{ {}{}.map({}) }}", scrutinee_str, as_ref_str, body_str) - } else { - format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str) + let body_str = if let PatKind::Binding(annotation, id, some_binding, None) = some_pat.kind { + match can_pass_as_func(cx, id, some_expr) { + Some(func) if func.span.ctxt() == some_expr.span.ctxt() => { + snippet_with_applicability(cx, func.span, "..", &mut app).into_owned() }, - app, - ); - } + _ => { + if path_to_local_id(some_expr, id) + && !is_lint_allowed(cx, MATCH_AS_REF, expr.hir_id) + && binding_ref.is_some() + { + return; + } + + // `ref` and `ref mut` annotations were handled earlier. + let annotation = if matches!(annotation, BindingAnnotation::Mutable) { + "mut " + } else { + "" + }; + format!( + "|{}{}| {}", + annotation, + some_binding, + snippet_with_context(cx, some_expr.span, expr_ctxt, "..", &mut app).0 + ) + }, + } + } else if !is_wild_none && explicit_ref.is_none() { + // TODO: handle explicit reference annotations. + format!( + "|{}| {}", + snippet_with_context(cx, some_pat.span, expr_ctxt, "..", &mut app).0, + snippet_with_context(cx, some_expr.span, expr_ctxt, "..", &mut app).0 + ) + } else { + // Refutable bindings and mixed reference annotations can't be handled by `map`. + return; + }; + + span_lint_and_sugg( + cx, + MANUAL_MAP, + expr.span, + "manual implementation of `Option::map`", + "try this", + if else_pat.is_none() && is_else_clause(cx.tcx, expr) { + format!("{{ {}{}.map({}) }}", scrutinee_str, as_ref_str, body_str) + } else { + format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str) + }, + app, + ); } } diff --git a/clippy_lints/src/manual_strip.rs b/clippy_lints/src/manual_strip.rs index db12c377488b..4e040508b6bf 100644 --- a/clippy_lints/src/manual_strip.rs +++ b/clippy_lints/src/manual_strip.rs @@ -73,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip { } if_chain! { - if let ExprKind::If(cond, then, _) = &expr.kind; + if let Some(higher::If { cond, then, .. }) = higher::If::hir(expr); if let ExprKind::MethodCall(_, _, [target_arg, pattern], _) = cond.kind; if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(cond.hir_id); if let ExprKind::Path(target_path) = &target_arg.kind; @@ -212,7 +212,7 @@ fn find_stripping<'tcx>( if is_ref_str(self.cx, ex); let unref = peel_ref(ex); if let ExprKind::Index(indexed, index) = &unref.kind; - if let Some(higher::Range { start, end, .. }) = higher::range(index); + if let Some(higher::Range { start, end, .. }) = higher::Range::hir(index); if let ExprKind::Path(path) = &indexed.kind; if self.cx.qpath_res(path, ex.hir_id) == self.target; then { diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 149c9bee9fbd..2f1ff567e844 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -2,18 +2,21 @@ use clippy_utils::consts::{constant, miri_to_const, Constant}; use clippy_utils::diagnostics::{ multispan_sugg, span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then, }; +use clippy_utils::higher; use clippy_utils::source::{expr_block, indent_of, snippet, snippet_block, snippet_opt, snippet_with_applicability}; use clippy_utils::sugg::Sugg; use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, match_type, peel_mid_ty_refs}; use clippy_utils::visitors::is_local_used; use clippy_utils::{ - get_parent_expr, in_macro, is_expn_of, is_lang_ctor, is_lint_allowed, is_refutable, is_wild, meets_msrv, msrvs, - path_to_local, path_to_local_id, peel_hir_pat_refs, peel_n_hir_expr_refs, recurse_or_patterns, remove_blocks, - strip_pat_refs, + get_parent_expr, in_macro, is_expn_of, is_lang_ctor, is_lint_allowed, is_refutable, is_unit_expr, is_wild, + meets_msrv, msrvs, path_to_local, path_to_local_id, peel_hir_pat_refs, peel_n_hir_expr_refs, recurse_or_patterns, + remove_blocks, strip_pat_refs, }; use clippy_utils::{paths, search_same, SpanlessEq, SpanlessHash}; +use core::array; +use core::iter::{once, ExactSizeIterator}; use if_chain::if_chain; -use rustc_ast::ast::LitKind; +use rustc_ast::ast::{Attribute, LitKind}; use rustc_errors::Applicability; use rustc_hir::def::{CtorKind, DefKind, Res}; use rustc_hir::LangItem::{OptionNone, OptionSome}; @@ -629,7 +632,10 @@ impl<'tcx> LateLintPass<'tcx> for Matches { } } if let ExprKind::Match(ex, arms, _) = expr.kind { - check_match_ref_pats(cx, ex, arms, expr); + check_match_ref_pats(cx, ex, arms.iter().map(|el| el.pat), expr); + } + if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr) { + check_match_ref_pats(cx, let_expr, once(let_pat), expr); } } @@ -1177,39 +1183,40 @@ fn is_panic_block(block: &Block<'_>) -> bool { } } -fn check_match_ref_pats(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) { - if has_only_ref_pats(arms) { - let mut suggs = Vec::with_capacity(arms.len() + 1); - let (title, msg) = if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = ex.kind { - let span = ex.span.source_callsite(); - suggs.push((span, Sugg::hir_with_macro_callsite(cx, inner, "..").to_string())); - ( - "you don't need to add `&` to both the expression and the patterns", - "try", - ) - } else { - let span = ex.span.source_callsite(); - suggs.push((span, Sugg::hir_with_macro_callsite(cx, ex, "..").deref().to_string())); - ( - "you don't need to add `&` to all patterns", - "instead of prefixing all patterns with `&`, you can dereference the expression", - ) - }; - - suggs.extend(arms.iter().filter_map(|a| { - if let PatKind::Ref(refp, _) = a.pat.kind { - Some((a.pat.span, snippet(cx, refp.span, "..").to_string())) - } else { - None - } - })); +fn check_match_ref_pats<'a, 'b, I>(cx: &LateContext<'_>, ex: &Expr<'_>, pats: I, expr: &Expr<'_>) +where + 'b: 'a, + I: Clone + Iterator>, +{ + if !has_only_ref_pats(pats.clone()) { + return; + } - span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| { - if !expr.span.from_expansion() { - multispan_sugg(diag, msg, suggs); - } - }); + let (first_sugg, msg, title); + let span = ex.span.source_callsite(); + if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = ex.kind { + first_sugg = once((span, Sugg::hir_with_macro_callsite(cx, inner, "..").to_string())); + msg = "try"; + title = "you don't need to add `&` to both the expression and the patterns"; + } else { + first_sugg = once((span, Sugg::hir_with_macro_callsite(cx, ex, "..").deref().to_string())); + msg = "instead of prefixing all patterns with `&`, you can dereference the expression"; + title = "you don't need to add `&` to all patterns"; } + + let remaining_suggs = pats.filter_map(|pat| { + if let PatKind::Ref(refp, _) = pat.kind { + Some((pat.span, snippet(cx, refp.span, "..").to_string())) + } else { + None + } + }); + + span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| { + if !expr.span.from_expansion() { + multispan_sugg(diag, msg, first_sugg.chain(remaining_suggs)); + } + }); } fn check_match_as_ref(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) { @@ -1284,46 +1291,99 @@ fn check_wild_in_or_pats(cx: &LateContext<'_>, arms: &[Arm<'_>]) { /// Lint a `match` or `if let .. { .. } else { .. }` expr that could be replaced by `matches!` fn check_match_like_matches<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool { - if let ExprKind::Match(ex, arms, ref match_source) = &expr.kind { - match match_source { - MatchSource::Normal => find_matches_sugg(cx, ex, arms, expr, false), - MatchSource::IfLetDesugar { .. } => find_matches_sugg(cx, ex, arms, expr, true), - _ => false, - } - } else { - false + if let Some(higher::IfLet { + let_pat, + let_expr, + if_then, + if_else: Some(if_else), + }) = higher::IfLet::hir(cx, expr) + { + return find_matches_sugg( + cx, + let_expr, + array::IntoIter::new([(&[][..], Some(let_pat), if_then, None), (&[][..], None, if_else, None)]), + expr, + true, + ); } + + if let ExprKind::Match(scrut, arms, MatchSource::Normal) = expr.kind { + return find_matches_sugg( + cx, + scrut, + arms.iter().map(|arm| { + ( + cx.tcx.hir().attrs(arm.hir_id), + Some(arm.pat), + arm.body, + arm.guard.as_ref(), + ) + }), + expr, + false, + ); + } + + false } -/// Lint a `match` or desugared `if let` for replacement by `matches!` -fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>, desugared: bool) -> bool { +/// Lint a `match` or `if let` for replacement by `matches!` +fn find_matches_sugg<'a, 'b, I>( + cx: &LateContext<'_>, + ex: &Expr<'_>, + mut iter: I, + expr: &Expr<'_>, + is_if_let: bool, +) -> bool +where + 'b: 'a, + I: Clone + + DoubleEndedIterator + + ExactSizeIterator + + Iterator< + Item = ( + &'a [Attribute], + Option<&'a Pat<'b>>, + &'a Expr<'b>, + Option<&'a Guard<'b>>, + ), + >, +{ if_chain! { - if arms.len() >= 2; + if iter.len() >= 2; if cx.typeck_results().expr_ty(expr).is_bool(); - if let Some((b1_arm, b0_arms)) = arms.split_last(); - if let Some(b0) = find_bool_lit(&b0_arms[0].body.kind, desugared); - if let Some(b1) = find_bool_lit(&b1_arm.body.kind, desugared); - if is_wild(b1_arm.pat); + if let Some((_, last_pat_opt, last_expr, _)) = iter.next_back(); + let iter_without_last = iter.clone(); + if let Some((first_attrs, _, first_expr, first_guard)) = iter.next(); + if let Some(b0) = find_bool_lit(&first_expr.kind, is_if_let); + if let Some(b1) = find_bool_lit(&last_expr.kind, is_if_let); if b0 != b1; - let if_guard = &b0_arms[0].guard; - if if_guard.is_none() || b0_arms.len() == 1; - if cx.tcx.hir().attrs(b0_arms[0].hir_id).is_empty(); - if b0_arms[1..].iter() + if first_guard.is_none() || iter.len() == 0; + if first_attrs.is_empty(); + if iter .all(|arm| { - find_bool_lit(&arm.body.kind, desugared).map_or(false, |b| b == b0) && - arm.guard.is_none() && cx.tcx.hir().attrs(arm.hir_id).is_empty() + find_bool_lit(&arm.2.kind, is_if_let).map_or(false, |b| b == b0) && arm.3.is_none() && arm.0.is_empty() }); then { + if let Some(last_pat) = last_pat_opt { + if !is_wild(last_pat) { + return false; + } + } + // The suggestion may be incorrect, because some arms can have `cfg` attributes // evaluated into `false` and so such arms will be stripped before. let mut applicability = Applicability::MaybeIncorrect; let pat = { use itertools::Itertools as _; - b0_arms.iter() - .map(|arm| snippet_with_applicability(cx, arm.pat.span, "..", &mut applicability)) + iter_without_last + .filter_map(|arm| { + let pat_span = arm.1?.span; + Some(snippet_with_applicability(cx, pat_span, "..", &mut applicability)) + }) .join(" | ") }; - let pat_and_guard = if let Some(Guard::If(g)) = if_guard { + let pat_and_guard = if let Some(Guard::If(g)) = first_guard { format!("{} if {}", pat, snippet_with_applicability(cx, g.span, "..", &mut applicability)) } else { pat @@ -1340,7 +1400,7 @@ fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr cx, MATCH_LIKE_MATCHES_MACRO, expr.span, - &format!("{} expression looks like `matches!` macro", if desugared { "if let .. else" } else { "match" }), + &format!("{} expression looks like `matches!` macro", if is_if_let { "if let .. else" } else { "match" }), "try this", format!( "{}matches!({}, {})", @@ -1358,7 +1418,7 @@ fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr } /// Extract a `bool` or `{ bool }` -fn find_bool_lit(ex: &ExprKind<'_>, desugared: bool) -> Option { +fn find_bool_lit(ex: &ExprKind<'_>, is_if_let: bool) -> Option { match ex { ExprKind::Lit(Spanned { node: LitKind::Bool(b), .. @@ -1370,7 +1430,7 @@ fn find_bool_lit(ex: &ExprKind<'_>, desugared: bool) -> Option { .. }, _, - ) if desugared => { + ) if is_if_let => { if let ExprKind::Lit(Spanned { node: LitKind::Bool(b), .. }) = exp.kind @@ -1610,14 +1670,6 @@ fn type_ranges(ranges: &[SpannedRange]) -> TypedRanges { .collect() } -fn is_unit_expr(expr: &Expr<'_>) -> bool { - match expr.kind { - ExprKind::Tup(v) if v.is_empty() => true, - ExprKind::Block(b, _) if b.stmts.is_empty() && b.expr.is_none() => true, - _ => false, - } -} - // Checks if arm has the form `None => None` fn is_none_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool { matches!(arm.pat.kind, PatKind::Path(ref qpath) if is_lang_ctor(cx, qpath, OptionNone)) @@ -1642,19 +1694,26 @@ fn is_ref_some_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> Option]) -> bool { - let mapped = arms - .iter() - .map(|a| { - match a.pat.kind { - PatKind::Ref(..) => Some(true), // &-patterns - PatKind::Wild => Some(false), // an "anything" wildcard is also fine - _ => None, // any other pattern is not fine +fn has_only_ref_pats<'a, 'b, I>(pats: I) -> bool +where + 'b: 'a, + I: Iterator>, +{ + let mut at_least_one_is_true = false; + for opt in pats.map(|pat| match pat.kind { + PatKind::Ref(..) => Some(true), // &-patterns + PatKind::Wild => Some(false), // an "anything" wildcard is also fine + _ => None, // any other pattern is not fine + }) { + if let Some(inner) = opt { + if inner { + at_least_one_is_true = true; } - }) - .collect::>>(); - // look for Some(v) where there's at least one true element - mapped.map_or(false, |v| v.iter().any(|el| *el)) + } else { + return false; + } + } + at_least_one_is_true } pub fn overlapping(ranges: &[SpannedRange]) -> Option<(&SpannedRange, &SpannedRange)> @@ -1743,6 +1802,7 @@ where mod redundant_pattern_match { use super::REDUNDANT_PATTERN_MATCHING; use clippy_utils::diagnostics::span_lint_and_then; + use clippy_utils::higher; use clippy_utils::source::{snippet, snippet_with_applicability}; use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, is_type_lang_item, match_type}; use clippy_utils::{is_lang_ctor, is_qpath_def_path, is_trait_method, paths}; @@ -1753,22 +1813,27 @@ mod redundant_pattern_match { use rustc_hir::LangItem::{OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk}; use rustc_hir::{ intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor}, - Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, PatKind, QPath, + Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath, }; use rustc_lint::LateContext; use rustc_middle::ty::{self, subst::GenericArgKind, Ty}; use rustc_span::sym; pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if let ExprKind::Match(op, arms, ref match_source) = &expr.kind { - match match_source { - MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms), - MatchSource::IfLetDesugar { contains_else_clause } => { - find_sugg_for_if_let(cx, expr, op, &arms[0], "if", *contains_else_clause); - }, - MatchSource::WhileLetDesugar => find_sugg_for_if_let(cx, expr, op, &arms[0], "while", false), - _ => {}, - } + if let Some(higher::IfLet { + if_else, + let_pat, + let_expr, + .. + }) = higher::IfLet::hir(cx, expr) + { + find_sugg_for_if_let(cx, expr, let_pat, let_expr, "if", if_else.is_some()); + } + if let ExprKind::Match(op, arms, MatchSource::Normal) = &expr.kind { + find_sugg_for_match(cx, expr, op, arms); + } + if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(expr) { + find_sugg_for_if_let(cx, expr, let_pat, let_expr, "while", false); } } @@ -1922,18 +1987,18 @@ mod redundant_pattern_match { fn find_sugg_for_if_let<'tcx>( cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, - op: &'tcx Expr<'tcx>, - arm: &Arm<'_>, + let_pat: &Pat<'_>, + let_expr: &'tcx Expr<'_>, keyword: &'static str, has_else: bool, ) { // also look inside refs - let mut kind = &arm.pat.kind; + let mut kind = &let_pat.kind; // if we have &None for example, peel it so we can detect "if let None = x" if let PatKind::Ref(inner, _mutability) = kind { kind = &inner.kind; } - let op_ty = cx.typeck_results().expr_ty(op); + let op_ty = cx.typeck_results().expr_ty(let_expr); // Determine which function should be used, and the type contained by the corresponding // variant. let (good_method, inner_ty) = match kind { @@ -1987,38 +2052,38 @@ mod redundant_pattern_match { // scrutinee would be, so they have to be considered as well. // e.g. in `if let Some(x) = foo.lock().unwrap().baz.as_ref() { .. }` the lock will be held // for the duration if body. - let needs_drop = type_needs_ordered_drop(cx, check_ty) || temporaries_need_ordered_drop(cx, op); + let needs_drop = type_needs_ordered_drop(cx, check_ty) || temporaries_need_ordered_drop(cx, let_expr); // check that `while_let_on_iterator` lint does not trigger if_chain! { if keyword == "while"; - if let ExprKind::MethodCall(method_path, _, _, _) = op.kind; + if let ExprKind::MethodCall(method_path, _, _, _) = let_expr.kind; if method_path.ident.name == sym::next; - if is_trait_method(cx, op, sym::Iterator); + if is_trait_method(cx, let_expr, sym::Iterator); then { return; } } - let result_expr = match &op.kind { + let result_expr = match &let_expr.kind { ExprKind::AddrOf(_, _, borrowed) => borrowed, - _ => op, + _ => let_expr, }; span_lint_and_then( cx, REDUNDANT_PATTERN_MATCHING, - arm.pat.span, + let_pat.span, &format!("redundant pattern matching, consider using `{}`", good_method), |diag| { - // while let ... = ... { ... } + // if/while let ... = ... { ... } // ^^^^^^^^^^^^^^^^^^^^^^^^^^^ let expr_span = expr.span; - // while let ... = ... { ... } + // if/while let ... = ... { ... } // ^^^ let op_span = result_expr.span.source_callsite(); - // while let ... = ... { ... } + // if/while let ... = ... { ... } // ^^^^^^^^^^^^^^^^^^^ let span = expr_span.until(op_span.shrink_to_hi()); diff --git a/clippy_lints/src/methods/iter_next_slice.rs b/clippy_lints/src/methods/iter_next_slice.rs index a49851de38e1..6954da67e32c 100644 --- a/clippy_lints/src/methods/iter_next_slice.rs +++ b/clippy_lints/src/methods/iter_next_slice.rs @@ -18,7 +18,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, cal // since it is already covered by `&loops::ITER_NEXT_LOOP` let mut parent_expr_opt = get_parent_expr(cx, expr); while let Some(parent_expr) = parent_expr_opt { - if higher::for_loop(parent_expr).is_some() { + if higher::ForLoop::hir(parent_expr).is_some() { return; } parent_expr_opt = get_parent_expr(cx, parent_expr); @@ -29,7 +29,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, cal if_chain! { if let hir::ExprKind::Index(caller_var, index_expr) = &caller_expr.kind; if let Some(higher::Range { start: Some(start_expr), end: None, limits: ast::RangeLimits::HalfOpen }) - = higher::range(index_expr); + = higher::Range::hir(index_expr); if let hir::ExprKind::Lit(ref start_lit) = &start_expr.kind; if let ast::LitKind::Int(start_idx, _) = start_lit.node; then { diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index b6438bce8746..e89b2d295b92 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2013,10 +2013,10 @@ impl<'tcx> LateLintPass<'tcx> for Methods { // walk the return type and check for Self (this does not check associated types) if let Some(self_adt) = self_ty.ty_adt_def() { - if contains_adt_constructor(ret_ty, self_adt) { + if contains_adt_constructor(cx.tcx, ret_ty, self_adt) { return; } - } else if contains_ty(ret_ty, self_ty) { + } else if contains_ty(cx.tcx, ret_ty, self_ty) { return; } @@ -2027,10 +2027,10 @@ impl<'tcx> LateLintPass<'tcx> for Methods { if let ty::PredicateKind::Projection(projection_predicate) = predicate.kind().skip_binder() { // walk the associated type and check for Self if let Some(self_adt) = self_ty.ty_adt_def() { - if contains_adt_constructor(projection_predicate.ty, self_adt) { + if contains_adt_constructor(cx.tcx, projection_predicate.ty, self_adt) { return; } - } else if contains_ty(projection_predicate.ty, self_ty) { + } else if contains_ty(cx.tcx, projection_predicate.ty, self_ty) { return; } } @@ -2079,7 +2079,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { if let TraitItemKind::Fn(_, _) = item.kind; let ret_ty = return_ty(cx, item.hir_id()); let self_ty = TraitRef::identity(cx.tcx, item.def_id.to_def_id()).self_ty(); - if !contains_ty(ret_ty, self_ty); + if !contains_ty(cx.tcx, ret_ty, self_ty); then { span_lint( diff --git a/clippy_lints/src/missing_doc.rs b/clippy_lints/src/missing_doc.rs index 8f5aeae85dfc..55d2e29e8bc1 100644 --- a/clippy_lints/src/missing_doc.rs +++ b/clippy_lints/src/missing_doc.rs @@ -137,6 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { }, hir::ItemKind::Const(..) | hir::ItemKind::Enum(..) + | hir::ItemKind::Macro(..) | hir::ItemKind::Mod(..) | hir::ItemKind::Static(..) | hir::ItemKind::Struct(..) diff --git a/clippy_lints/src/missing_inline.rs b/clippy_lints/src/missing_inline.rs index 977e6d966e87..667cdd830252 100644 --- a/clippy_lints/src/missing_inline.rs +++ b/clippy_lints/src/missing_inline.rs @@ -118,6 +118,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline { }, hir::ItemKind::Const(..) | hir::ItemKind::Enum(..) + | hir::ItemKind::Macro(..) | hir::ItemKind::Mod(..) | hir::ItemKind::Static(..) | hir::ItemKind::Struct(..) diff --git a/clippy_lints/src/module_style.rs b/clippy_lints/src/module_style.rs index 73e93cf5a3ec..80a930d0c547 100644 --- a/clippy_lints/src/module_style.rs +++ b/clippy_lints/src/module_style.rs @@ -80,7 +80,7 @@ impl EarlyLintPass for ModStyle { let files = cx.sess.source_map().files(); - let trim_to_src = if let RealFileName::LocalPath(p) = &cx.sess.working_dir { + let trim_to_src = if let RealFileName::LocalPath(p) = &cx.sess.opts.working_dir { p.to_string_lossy() } else { return; diff --git a/clippy_lints/src/mut_mut.rs b/clippy_lints/src/mut_mut.rs index d5032c5ba7f2..610152a217f1 100644 --- a/clippy_lints/src/mut_mut.rs +++ b/clippy_lints/src/mut_mut.rs @@ -53,7 +53,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> { return; } - if let Some((_, arg, body, _)) = higher::for_loop(expr) { + if let Some(higher::ForLoop { arg, body, .. }) = higher::ForLoop::hir(expr) { // A `for` loop lowers to: // ```rust // match ::std::iter::Iterator::next(&mut iter) { diff --git a/clippy_lints/src/needless_bool.rs b/clippy_lints/src/needless_bool.rs index 36f2829a5b94..c9dd94400efb 100644 --- a/clippy_lints/src/needless_bool.rs +++ b/clippy_lints/src/needless_bool.rs @@ -3,6 +3,7 @@ //! This lint is **warn** by default use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg}; +use clippy_utils::higher; use clippy_utils::source::snippet_with_applicability; use clippy_utils::sugg::Sugg; use clippy_utils::{is_else_clause, is_expn_of}; @@ -77,10 +78,15 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool { if e.span.from_expansion() { return; } - if let ExprKind::If(pred, then_block, Some(else_expr)) = e.kind { + if let Some(higher::If { + cond, + then, + r#else: Some(r#else), + }) = higher::If::hir(e) + { let reduce = |ret, not| { let mut applicability = Applicability::MachineApplicable; - let snip = Sugg::hir_with_applicability(cx, pred, "", &mut applicability); + let snip = Sugg::hir_with_applicability(cx, cond, "", &mut applicability); let mut snip = if not { !snip } else { snip }; if ret { @@ -101,8 +107,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool { applicability, ); }; - if let ExprKind::Block(then_block, _) = then_block.kind { - match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) { + if let ExprKind::Block(then, _) = then.kind { + match (fetch_bool_block(then), fetch_bool_expr(r#else)) { (RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => { span_lint( cx, diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index 03eeb54d8d1c..90b2aa168962 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -117,11 +117,11 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { let fn_def_id = cx.tcx.hir().local_def_id(hir_id); let preds = traits::elaborate_predicates(cx.tcx, cx.param_env.caller_bounds().iter()) - .filter(|p| !p.is_global()) + .filter(|p| !p.is_global(cx.tcx)) .filter_map(|obligation| { // Note that we do not want to deal with qualified predicates here. match obligation.predicate.kind().no_bound_vars() { - Some(ty::PredicateKind::Trait(pred, _)) if pred.def_id() != sized_trait => Some(pred), + Some(ty::PredicateKind::Trait(pred)) if pred.def_id() != sized_trait => Some(pred), _ => None, } }) diff --git a/clippy_lints/src/non_copy_const.rs b/clippy_lints/src/non_copy_const.rs index aa3067876ebf..2a85a67fa099 100644 --- a/clippy_lints/src/non_copy_const.rs +++ b/clippy_lints/src/non_copy_const.rs @@ -122,7 +122,7 @@ fn is_unfrozen<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { // Ignore types whose layout is unknown since `is_freeze` reports every generic types as `!Freeze`, // making it indistinguishable from `UnsafeCell`. i.e. it isn't a tool to prove a type is // 'unfrozen'. However, this code causes a false negative in which - // a type contains a layout-unknown type, but also a unsafe cell like `const CELL: Cell`. + // a type contains a layout-unknown type, but also an unsafe cell like `const CELL: Cell`. // Yet, it's better than `ty.has_type_flags(TypeFlags::HAS_TY_PARAM | TypeFlags::HAS_PROJECTION)` // since it works when a pointer indirection involves (`Cell<*const T>`). // Making up a `ParamEnv` where every generic params and assoc types are `Freeze`is another option; @@ -187,11 +187,7 @@ fn is_value_unfrozen_expr<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId, def_id: D let result = cx.tcx.const_eval_resolve( cx.param_env, - ty::Unevaluated { - def: ty::WithOptConstParam::unknown(def_id), - substs, - promoted: None, - }, + ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs), None, ); is_value_unfrozen_raw(cx, result, ty) @@ -266,7 +262,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst { // in other words, lint consts whose value *could* be unfrozen, not definitely is. // This feels inconsistent with how the lint treats generic types, // which avoids linting types which potentially become unfrozen. - // One could check whether a unfrozen type have a *frozen variant* + // One could check whether an unfrozen type have a *frozen variant* // (like `body_id_opt.map_or_else(|| !has_frozen_variant(...), ...)`), // and do the same as the case of generic types at impl items. // Note that it isn't sufficient to check if it has an enum @@ -293,7 +289,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst { }) => { if_chain! { // Lint a trait impl item only when the definition is a generic type, - // assuming a assoc const is not meant to be a interior mutable type. + // assuming an assoc const is not meant to be an interior mutable type. if let Some(of_trait_def_id) = of_trait_ref.trait_def_id(); if let Some(of_assoc_item) = specialization_graph::Node::Trait(of_trait_def_id) .item(cx.tcx, impl_item.ident, AssocKind::Const, of_trait_def_id); diff --git a/clippy_lints/src/non_expressive_names.rs b/clippy_lints/src/non_expressive_names.rs index ac21eb5275f0..2ffc00b449d0 100644 --- a/clippy_lints/src/non_expressive_names.rs +++ b/clippy_lints/src/non_expressive_names.rs @@ -316,8 +316,11 @@ impl<'a, 'b> SimilarNamesLocalVisitor<'a, 'b> { impl<'a, 'tcx> Visitor<'tcx> for SimilarNamesLocalVisitor<'a, 'tcx> { fn visit_local(&mut self, local: &'tcx Local) { - if let Some(ref init) = local.init { - self.apply(|this| walk_expr(this, &**init)); + if let Some((init, els)) = &local.kind.init_else_opt() { + self.apply(|this| walk_expr(this, init)); + if let Some(els) = els { + self.apply(|this| walk_block(this, els)); + } } // add the pattern after the expression because the bindings aren't available // yet in the init diff --git a/clippy_lints/src/option_if_let_else.rs b/clippy_lints/src/option_if_let_else.rs index 0614c14462b7..15f6dcae8870 100644 --- a/clippy_lints/src/option_if_let_else.rs +++ b/clippy_lints/src/option_if_let_else.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::higher; use clippy_utils::sugg::Sugg; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::{ @@ -8,9 +9,7 @@ use clippy_utils::{ use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::LangItem::OptionSome; -use rustc_hir::{ - def::Res, Arm, BindingAnnotation, Block, Expr, ExprKind, MatchSource, Mutability, PatKind, Path, QPath, UnOp, -}; +use rustc_hir::{def::Res, BindingAnnotation, Block, Expr, ExprKind, Mutability, PatKind, Path, QPath, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::sym; @@ -88,20 +87,20 @@ struct OptionIfLetElseOccurence { /// Extracts the body of a given arm. If the arm contains only an expression, /// then it returns the expression. Otherwise, it returns the entire block -fn extract_body_from_arm<'a>(arm: &'a Arm<'a>) -> Option<&'a Expr<'a>> { +fn extract_body_from_expr<'a>(expr: &'a Expr<'a>) -> Option<&'a Expr<'a>> { if let ExprKind::Block( Block { - stmts: statements, - expr: Some(expr), + stmts: block_stmts, + expr: Some(block_expr), .. }, _, - ) = &arm.body.kind + ) = expr.kind { - if let [] = statements { - Some(expr) + if let [] = block_stmts { + Some(block_expr) } else { - Some(arm.body) + Some(expr) } } else { None @@ -125,22 +124,19 @@ fn format_option_in_sugg(cx: &LateContext<'_>, cond_expr: &Expr<'_>, as_ref: boo /// If this expression is the option if let/else construct we're detecting, then /// this function returns an `OptionIfLetElseOccurence` struct with details if /// this construct is found, or None if this construct is not found. -fn detect_option_if_let_else<'tcx>( - cx: &'_ LateContext<'tcx>, - expr: &'_ Expr<'tcx>, -) -> Option { +fn detect_option_if_let_else<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option { if_chain! { if !in_macro(expr.span); // Don't lint macros, because it behaves weirdly if !in_constant(cx, expr.hir_id); - if let ExprKind::Match(cond_expr, [some_arm, none_arm], MatchSource::IfLetDesugar{contains_else_clause: true}) - = &expr.kind; + if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else: Some(if_else) }) + = higher::IfLet::hir(cx, expr); if !is_else_clause(cx.tcx, expr); - if !is_result_ok(cx, cond_expr); // Don't lint on Result::ok because a different lint does it already - if let PatKind::TupleStruct(struct_qpath, [inner_pat], _) = &some_arm.pat.kind; + if !is_result_ok(cx, let_expr); // Don't lint on Result::ok because a different lint does it already + if let PatKind::TupleStruct(struct_qpath, [inner_pat], _) = &let_pat.kind; if is_lang_ctor(cx, struct_qpath, OptionSome); if let PatKind::Binding(bind_annotation, _, id, _) = &inner_pat.kind; - if let Some(some_captures) = can_move_expr_to_closure(cx, some_arm.body); - if let Some(none_captures) = can_move_expr_to_closure(cx, none_arm.body); + if let Some(some_captures) = can_move_expr_to_closure(cx, if_then); + if let Some(none_captures) = can_move_expr_to_closure(cx, if_else); if some_captures .iter() .filter_map(|(id, &c)| none_captures.get(id).map(|&c2| (c, c2))) @@ -148,23 +144,23 @@ fn detect_option_if_let_else<'tcx>( then { let capture_mut = if bind_annotation == &BindingAnnotation::Mutable { "mut " } else { "" }; - let some_body = extract_body_from_arm(some_arm)?; - let none_body = extract_body_from_arm(none_arm)?; + let some_body = extract_body_from_expr(if_then)?; + let none_body = extract_body_from_expr(if_else)?; let method_sugg = if eager_or_lazy::is_eagerness_candidate(cx, none_body) { "map_or" } else { "map_or_else" }; let capture_name = id.name.to_ident_string(); - let (as_ref, as_mut) = match &cond_expr.kind { + let (as_ref, as_mut) = match &let_expr.kind { ExprKind::AddrOf(_, Mutability::Not, _) => (true, false), ExprKind::AddrOf(_, Mutability::Mut, _) => (false, true), _ => (bind_annotation == &BindingAnnotation::Ref, bind_annotation == &BindingAnnotation::RefMut), }; - let cond_expr = match &cond_expr.kind { + let cond_expr = match let_expr.kind { // Pointer dereferencing happens automatically, so we can omit it in the suggestion ExprKind::Unary(UnOp::Deref, expr) | ExprKind::AddrOf(_, _, expr) => expr, - _ => cond_expr, + _ => let_expr, }; // Check if captures the closure will need conflict with borrows made in the scrutinee. // TODO: check all the references made in the scrutinee expression. This will require interacting diff --git a/clippy_lints/src/pattern_type_mismatch.rs b/clippy_lints/src/pattern_type_mismatch.rs index 4534f6e25165..e7bc24465908 100644 --- a/clippy_lints/src/pattern_type_mismatch.rs +++ b/clippy_lints/src/pattern_type_mismatch.rs @@ -104,22 +104,25 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch { } fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { - if let ExprKind::Match(expr, arms, source) = expr.kind { - match source { - MatchSource::Normal | MatchSource::IfLetDesugar { .. } | MatchSource::WhileLetDesugar => { - if let Some(expr_ty) = cx.typeck_results().node_type_opt(expr.hir_id) { - 'pattern_checks: for arm in arms { - let pat = &arm.pat; - if in_external_macro(cx.sess(), pat.span) { - continue 'pattern_checks; - } - if apply_lint(cx, pat, expr_ty, DerefPossible::Possible) { - break 'pattern_checks; - } - } + if let ExprKind::Match(scrutinee, arms, MatchSource::Normal) = expr.kind { + if let Some(expr_ty) = cx.typeck_results().node_type_opt(scrutinee.hir_id) { + 'pattern_checks: for arm in arms { + let pat = &arm.pat; + if in_external_macro(cx.sess(), pat.span) { + continue 'pattern_checks; } - }, - _ => (), + if apply_lint(cx, pat, expr_ty, DerefPossible::Possible) { + break 'pattern_checks; + } + } + } + } + if let ExprKind::Let(let_pat, let_expr, _) = expr.kind { + if let Some(expr_ty) = cx.typeck_results().node_type_opt(let_expr.hir_id) { + if in_external_macro(cx.sess(), let_pat.span) { + return; + } + apply_lint(cx, let_pat, expr_ty, DerefPossible::Possible); } } } diff --git a/clippy_lints/src/ptr_eq.rs b/clippy_lints/src/ptr_eq.rs index d6d7049fb61b..3258c9fb3fed 100644 --- a/clippy_lints/src/ptr_eq.rs +++ b/clippy_lints/src/ptr_eq.rs @@ -74,7 +74,7 @@ impl LateLintPass<'_> for PtrEq { } } -// If the given expression is a cast to an usize, return the lhs of the cast +// If the given expression is a cast to a usize, return the lhs of the cast // E.g., `foo as *const _ as usize` returns `foo as *const _`. fn expr_as_cast_to_usize<'tcx>(cx: &LateContext<'tcx>, cast_expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> { if cx.typeck_results().expr_ty(cast_expr) == cx.tcx.types.usize { diff --git a/clippy_lints/src/question_mark.rs b/clippy_lints/src/question_mark.rs index 0e682c692c7a..e79cd7ed4ec4 100644 --- a/clippy_lints/src/question_mark.rs +++ b/clippy_lints/src/question_mark.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::higher; use clippy_utils::is_lang_ctor; use clippy_utils::source::snippet_with_applicability; use clippy_utils::sugg::Sugg; @@ -7,7 +8,7 @@ use clippy_utils::{eq_expr_value, path_to_local_id}; use if_chain::if_chain; use rustc_errors::Applicability; use rustc_hir::LangItem::{OptionNone, OptionSome}; -use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, MatchSource, PatKind, StmtKind}; +use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, PatKind, StmtKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::sym; @@ -50,10 +51,10 @@ impl QuestionMark { /// If it matches, it will suggest to use the question mark operator instead fn check_is_none_and_early_return_none(cx: &LateContext<'_>, expr: &Expr<'_>) { if_chain! { - if let ExprKind::If(if_expr, body, else_) = &expr.kind; - if let ExprKind::MethodCall(segment, _, args, _) = &if_expr.kind; + if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr); + if let ExprKind::MethodCall(segment, _, args, _) = &cond.kind; if segment.ident.name == sym!(is_none); - if Self::expression_returns_none(cx, body); + if Self::expression_returns_none(cx, then); if let Some(subject) = args.get(0); if Self::is_option(cx, subject); @@ -61,9 +62,9 @@ impl QuestionMark { let mut applicability = Applicability::MachineApplicable; let receiver_str = &Sugg::hir_with_applicability(cx, subject, "..", &mut applicability); let mut replacement: Option = None; - if let Some(else_) = else_ { + if let Some(else_inner) = r#else { if_chain! { - if let ExprKind::Block(block, None) = &else_.kind; + if let ExprKind::Block(block, None) = &else_inner.kind; if block.stmts.is_empty(); if let Some(block_expr) = &block.expr; if eq_expr_value(cx, subject, block_expr); @@ -96,25 +97,24 @@ impl QuestionMark { fn check_if_let_some_and_early_return_none(cx: &LateContext<'_>, expr: &Expr<'_>) { if_chain! { - if let ExprKind::Match(subject, arms, source) = &expr.kind; - if *source == MatchSource::IfLetDesugar { contains_else_clause: true }; - if Self::is_option(cx, subject); + if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else: Some(if_else) }) + = higher::IfLet::hir(cx, expr); + if Self::is_option(cx, let_expr); - if let PatKind::TupleStruct(path1, fields, None) = &arms[0].pat.kind; + if let PatKind::TupleStruct(ref path1, fields, None) = let_pat.kind; if is_lang_ctor(cx, path1, OptionSome); if let PatKind::Binding(annot, bind_id, _, _) = fields[0].kind; let by_ref = matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut); - if let ExprKind::Block(block, None) = &arms[0].body.kind; + if let ExprKind::Block(block, None) = if_then.kind; if block.stmts.is_empty(); if let Some(trailing_expr) = &block.expr; if path_to_local_id(trailing_expr, bind_id); - if let PatKind::Wild = arms[1].pat.kind; - if Self::expression_returns_none(cx, arms[1].body); + if Self::expression_returns_none(cx, if_else); then { let mut applicability = Applicability::MachineApplicable; - let receiver_str = snippet_with_applicability(cx, subject.span, "..", &mut applicability); + let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability); let replacement = format!( "{}{}?", receiver_str, diff --git a/clippy_lints/src/ranges.rs b/clippy_lints/src/ranges.rs index 0179bd48ee3c..87364a88ed0d 100644 --- a/clippy_lints/src/ranges.rs +++ b/clippy_lints/src/ranges.rs @@ -51,7 +51,7 @@ declare_clippy_lint! { /// /// ### Known problems /// Will add unnecessary pair of parentheses when the - /// expression is not wrapped in a pair but starts with a opening parenthesis + /// expression is not wrapped in a pair but starts with an opening parenthesis /// and ends with a closing one. /// I.e., `let _ = (f()+1)..(f()+1)` results in `let _ = ((f()+1)..=f())`. /// @@ -329,7 +329,7 @@ fn check_range_zip_with_len(cx: &LateContext<'_>, path: &PathSegment<'_>, args: if let ExprKind::MethodCall(iter_path, _, iter_args, _) = iter.kind; if iter_path.ident.name == sym::iter; // range expression in `.zip()` call: `0..x.len()` - if let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::range(zip_arg); + if let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::Range::hir(zip_arg); if is_integer_const(cx, start, 0); // `.len()` call if let ExprKind::MethodCall(len_path, _, len_args, _) = end.kind; @@ -356,7 +356,7 @@ fn check_exclusive_range_plus_one(cx: &LateContext<'_>, expr: &Expr<'_>) { start, end: Some(end), limits: RangeLimits::HalfOpen - }) = higher::range(expr); + }) = higher::Range::hir(expr); if let Some(y) = y_plus_one(cx, end); then { let span = if expr.span.from_expansion() { @@ -401,7 +401,7 @@ fn check_exclusive_range_plus_one(cx: &LateContext<'_>, expr: &Expr<'_>) { // inclusive range minus one: `x..=(y-1)` fn check_inclusive_range_minus_one(cx: &LateContext<'_>, expr: &Expr<'_>) { if_chain! { - if let Some(higher::Range { start, end: Some(end), limits: RangeLimits::Closed }) = higher::range(expr); + if let Some(higher::Range { start, end: Some(end), limits: RangeLimits::Closed }) = higher::Range::hir(expr); if let Some(y) = y_minus_one(cx, end); then { span_lint_and_then( @@ -438,8 +438,8 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) { fn is_for_loop_arg(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { let mut cur_expr = expr; while let Some(parent_expr) = get_parent_expr(cx, cur_expr) { - match higher::for_loop(parent_expr) { - Some((_, args, _, _)) if args.hir_id == expr.hir_id => return true, + match higher::ForLoop::hir(parent_expr) { + Some(higher::ForLoop { arg, .. }) if arg.hir_id == expr.hir_id => return true, _ => cur_expr = parent_expr, } } @@ -455,7 +455,7 @@ fn check_reversed_empty_range(cx: &LateContext<'_>, expr: &Expr<'_>) { } if_chain! { - if let Some(higher::Range { start: Some(start), end: Some(end), limits }) = higher::range(expr); + if let Some(higher::Range { start: Some(start), end: Some(end), limits }) = higher::Range::hir(expr); let ty = cx.typeck_results().expr_ty(start); if let ty::Int(_) | ty::Uint(_) = ty.kind(); if let Some((start_idx, _)) = constant(cx, cx.typeck_results(), start); diff --git a/clippy_lints/src/redundant_clone.rs b/clippy_lints/src/redundant_clone.rs index 530b3396abef..cfa12ef3a321 100644 --- a/clippy_lints/src/redundant_clone.rs +++ b/clippy_lints/src/redundant_clone.rs @@ -14,7 +14,7 @@ use rustc_middle::mir::{ visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor as _}, Mutability, }; -use rustc_middle::ty::{self, fold::TypeVisitor, Ty}; +use rustc_middle::ty::{self, fold::TypeVisitor, Ty, TyCtxt}; use rustc_mir::dataflow::{Analysis, AnalysisDomain, GenKill, GenKillAnalysis, ResultsCursor}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::{BytePos, Span}; @@ -576,7 +576,7 @@ impl<'a, 'tcx> mir::visit::Visitor<'tcx> for PossibleBorrowerVisitor<'a, 'tcx> { self.possible_borrower.add(borrowed.local, lhs); }, other => { - if ContainsRegion + if ContainsRegion(self.cx.tcx) .visit_ty(place.ty(&self.body.local_decls, self.cx.tcx).ty) .is_continue() { @@ -625,7 +625,10 @@ impl<'a, 'tcx> mir::visit::Visitor<'tcx> for PossibleBorrowerVisitor<'a, 'tcx> { .flat_map(HybridBitSet::iter) .collect(); - if ContainsRegion.visit_ty(self.body.local_decls[*dest].ty).is_break() { + if ContainsRegion(self.cx.tcx) + .visit_ty(self.body.local_decls[*dest].ty) + .is_break() + { mutable_variables.push(*dest); } @@ -701,12 +704,15 @@ impl<'a, 'tcx> mir::visit::Visitor<'tcx> for PossibleOriginVisitor<'a, 'tcx> { } } -struct ContainsRegion; +struct ContainsRegion<'tcx>(TyCtxt<'tcx>); -impl TypeVisitor<'_> for ContainsRegion { +impl<'tcx> TypeVisitor<'tcx> for ContainsRegion<'tcx> { type BreakTy = (); + fn tcx_for_anon_const_substs(&self) -> Option> { + Some(self.0) + } - fn visit_region(&mut self, _: ty::Region<'_>) -> ControlFlow { + fn visit_region(&mut self, _: ty::Region<'tcx>) -> ControlFlow { ControlFlow::BREAK } } diff --git a/clippy_lints/src/redundant_closure_call.rs b/clippy_lints/src/redundant_closure_call.rs index 7314bce83e03..90e3c3f4b3e9 100644 --- a/clippy_lints/src/redundant_closure_call.rs +++ b/clippy_lints/src/redundant_closure_call.rs @@ -51,9 +51,7 @@ impl ReturnVisitor { impl<'ast> ast_visit::Visitor<'ast> for ReturnVisitor { fn visit_expr(&mut self, ex: &'ast ast::Expr) { - if let ast::ExprKind::Ret(_) = ex.kind { - self.found_return = true; - } else if let ast::ExprKind::Try(_) = ex.kind { + if let ast::ExprKind::Ret(_) | ast::ExprKind::Try(_) = ex.kind { self.found_return = true; } diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index db4b1002ce12..341b5a61631d 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -206,21 +206,10 @@ fn check_final_expr<'tcx>( // an if/if let expr, check both exprs // note, if without else is going to be a type checking error anyways // (except for unit type functions) so we don't match it - ExprKind::Match(_, arms, source) => match source { - MatchSource::Normal => { - for arm in arms.iter() { - check_final_expr(cx, arm.body, Some(arm.body.span), RetReplacement::Block); - } - }, - MatchSource::IfLetDesugar { - contains_else_clause: true, - } => { - if let ExprKind::Block(ifblock, _) = arms[0].body.kind { - check_block_return(cx, ifblock); - } - check_final_expr(cx, arms[1].body, None, RetReplacement::Empty); - }, - _ => (), + ExprKind::Match(_, arms, MatchSource::Normal) => { + for arm in arms.iter() { + check_final_expr(cx, arm.body, Some(arm.body.span), RetReplacement::Block); + } }, ExprKind::DropTemps(expr) => check_final_expr(cx, expr, None, RetReplacement::Empty), _ => (), @@ -296,7 +285,7 @@ impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> { .fn_sig(def_id) .output() .skip_binder() - .walk() + .walk(self.cx.tcx) .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_))); } diff --git a/clippy_lints/src/self_named_constructors.rs b/clippy_lints/src/self_named_constructors.rs index 4472edecbed8..4ba5e1a0f535 100644 --- a/clippy_lints/src/self_named_constructors.rs +++ b/clippy_lints/src/self_named_constructors.rs @@ -62,10 +62,10 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors { // Ensure method is constructor-like if let Some(self_adt) = self_ty.ty_adt_def() { - if !contains_adt_constructor(ret_ty, self_adt) { + if !contains_adt_constructor(cx.tcx, ret_ty, self_adt) { return; } - } else if !contains_ty(ret_ty, self_ty) { + } else if !contains_ty(cx.tcx, ret_ty, self_ty) { return; } diff --git a/clippy_lints/src/shadow.rs b/clippy_lints/src/shadow.rs index b28a37cabd40..b9e317a3cfd0 100644 --- a/clippy_lints/src/shadow.rs +++ b/clippy_lints/src/shadow.rs @@ -74,7 +74,7 @@ declare_clippy_lint! { declare_clippy_lint! { /// ### What it does /// Checks for bindings that shadow other bindings already in - /// scope, either without a initialization or with one that does not even use + /// scope, either without an initialization or with one that does not even use /// the original value. /// /// ### Why is this bad? diff --git a/clippy_lints/src/suspicious_operation_groupings.rs b/clippy_lints/src/suspicious_operation_groupings.rs index a8e962d1af3f..44d5ff0b63ad 100644 --- a/clippy_lints/src/suspicious_operation_groupings.rs +++ b/clippy_lints/src/suspicious_operation_groupings.rs @@ -588,7 +588,7 @@ fn ident_difference_expr_with_base_location( | (ForLoop(_, _, _, _), ForLoop(_, _, _, _)) | (While(_, _, _), While(_, _, _)) | (If(_, _, _), If(_, _, _)) - | (Let(_, _), Let(_, _)) + | (Let(_, _, _), Let(_, _, _)) | (Type(_, _), Type(_, _)) | (Cast(_, _), Cast(_, _)) | (Lit(_), Lit(_)) diff --git a/clippy_lints/src/unit_return_expecting_ord.rs b/clippy_lints/src/unit_return_expecting_ord.rs index 900d45317607..db0f412f2a18 100644 --- a/clippy_lints/src/unit_return_expecting_ord.rs +++ b/clippy_lints/src/unit_return_expecting_ord.rs @@ -45,7 +45,7 @@ fn get_trait_predicates_for_trait_id<'tcx>( let mut preds = Vec::new(); for (pred, _) in generics.predicates { if_chain! { - if let PredicateKind::Trait(poly_trait_pred, _) = pred.kind().skip_binder(); + if let PredicateKind::Trait(poly_trait_pred) = pred.kind().skip_binder(); let trait_pred = cx.tcx.erase_late_bound_regions(pred.kind().rebind(poly_trait_pred)); if let Some(trait_def_id) = trait_id; if trait_def_id == trait_pred.trait_ref.def_id; @@ -127,7 +127,7 @@ fn check_arg<'tcx>(cx: &LateContext<'tcx>, arg: &'tcx Expr<'tcx>) -> Option<(Spa then { let data = stmt.span.data(); // Make a span out of the semicolon for the help message - Some((span, Some(Span::new(data.hi-BytePos(1), data.hi, data.ctxt)))) + Some((span, Some(data.with_lo(data.hi-BytePos(1))))) } else { Some((span, None)) } diff --git a/clippy_lints/src/unnecessary_sort_by.rs b/clippy_lints/src/unnecessary_sort_by.rs index 6fc5707a4eef..61670fe124e3 100644 --- a/clippy_lints/src/unnecessary_sort_by.rs +++ b/clippy_lints/src/unnecessary_sort_by.rs @@ -218,7 +218,10 @@ fn detect_lint(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option { fn expr_borrows(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { let ty = cx.typeck_results().expr_ty(expr); - matches!(ty.kind(), ty::Ref(..)) || ty.walk().any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_))) + matches!(ty.kind(), ty::Ref(..)) + || ty + .walk(cx.tcx) + .any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_))) } impl LateLintPass<'_> for UnnecessarySortBy { diff --git a/clippy_lints/src/unnested_or_patterns.rs b/clippy_lints/src/unnested_or_patterns.rs index c8a231341b7e..d6cf7190abb0 100644 --- a/clippy_lints/src/unnested_or_patterns.rs +++ b/clippy_lints/src/unnested_or_patterns.rs @@ -67,7 +67,7 @@ impl EarlyLintPass for UnnestedOrPatterns { fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { if meets_msrv(self.msrv.as_ref(), &msrvs::OR_PATTERNS) { - if let ast::ExprKind::Let(pat, _) = &e.kind { + if let ast::ExprKind::Let(pat, _, _) = &e.kind { lint_unnested_or_patterns(cx, pat); } } diff --git a/clippy_lints/src/unused_async.rs b/clippy_lints/src/unused_async.rs index 3a6a07c52263..f4808682b692 100644 --- a/clippy_lints/src/unused_async.rs +++ b/clippy_lints/src/unused_async.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_help; use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, NestedVisitorMap, Visitor}; -use rustc_hir::{Body, Expr, ExprKind, FnDecl, FnHeader, HirId, IsAsync, Item, ItemKind, YieldSource}; +use rustc_hir::{Body, Expr, ExprKind, FnDecl, FnHeader, HirId, IsAsync, YieldSource}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::map::Map; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -57,11 +57,6 @@ impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> { } impl<'tcx> LateLintPass<'tcx> for UnusedAsync { - fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { - if let ItemKind::Trait(..) = item.kind { - return; - } - } fn check_fn( &mut self, cx: &LateContext<'tcx>, diff --git a/clippy_lints/src/unwrap.rs b/clippy_lints/src/unwrap.rs index c5b8acb9982d..bffd9f3612b0 100644 --- a/clippy_lints/src/unwrap.rs +++ b/clippy_lints/src/unwrap.rs @@ -1,4 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::higher; use clippy_utils::ty::is_type_diagnostic_item; use clippy_utils::{differing_macro_contexts, usage::is_potentially_mutated}; use if_chain::if_chain; @@ -160,11 +161,11 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> { if in_external_macro(self.cx.tcx.sess, expr.span) { return; } - if let ExprKind::If(cond, then, els) = &expr.kind { + if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr) { walk_expr(self, cond); self.visit_branch(cond, then, false); - if let Some(els) = els { - self.visit_branch(cond, els, true); + if let Some(else_inner) = r#else { + self.visit_branch(cond, else_inner, true); } } else { // find `unwrap[_err]()` calls: diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index a3601cca2eff..9ae50e47ca4c 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -170,7 +170,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { // // See also https://github.com/rust-lang/rust-clippy/issues/2894. for (impl_hir_ty, trait_sem_ty) in impl_inputs_outputs.zip(trait_method_sig.inputs_and_output) { - if trait_sem_ty.walk().any(|inner| inner == self_ty.into()) { + if trait_sem_ty.walk(cx.tcx).any(|inner| inner == self_ty.into()) { let mut visitor = SkipTyCollector::default(); visitor.visit_ty(impl_hir_ty); types_to_skip.extend(visitor.types_to_skip); diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs index 61fd375a9892..f93d7782e251 100644 --- a/clippy_lints/src/utils/author.rs +++ b/clippy_lints/src/utils/author.rs @@ -208,6 +208,15 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor { print!(" if let ExprKind::"); let current = format!("{}.kind", self.current); match expr.kind { + ExprKind::Let(pat, expr, _) => { + let let_pat = self.next("pat"); + let let_expr = self.next("expr"); + println!(" Let(ref {}, ref {}, _) = {};", let_pat, let_expr, current); + self.current = let_expr; + self.visit_expr(expr); + self.current = let_pat; + self.visit_pat(pat); + }, ExprKind::Box(inner) => { let inner_pat = self.next("inner"); println!("Box(ref {}) = {};", inner_pat, current); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 97062bcb2821..881dc3219308 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -194,7 +194,7 @@ define_Conf! { (enum_variant_name_threshold: u64 = 3), /// Lint: LARGE_ENUM_VARIANT. /// - /// The maximum size of a enum's variant to avoid box suggestion + /// The maximum size of an enum's variant to avoid box suggestion (enum_variant_size_threshold: u64 = 200), /// Lint: VERBOSE_BIT_MASK. /// diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index f7ddee12dcf6..43590cc78623 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -66,28 +66,6 @@ impl<'tcx> LateLintPass<'tcx> for DeepCodeInspector { hir::ImplItemKind::TyAlias(_) => println!("associated type"), } } - // fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx - // hir::TraitItem) { - // if !has_attr(&item.attrs) { - // return; - // } - // } - // - // fn check_variant(&mut self, cx: &LateContext<'tcx>, var: &'tcx - // hir::Variant, _: - // &hir::Generics) { - // if !has_attr(&var.node.attrs) { - // return; - // } - // } - // - // fn check_field_def(&mut self, cx: &LateContext<'tcx>, field: &'tcx - // hir::FieldDef) { - // if !has_attr(&field.attrs) { - // return; - // } - // } - // fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { if !has_attr(cx.sess(), cx.tcx.hir().attrs(expr.hir_id)) { @@ -127,13 +105,6 @@ impl<'tcx> LateLintPass<'tcx> for DeepCodeInspector { hir::StmtKind::Expr(e) | hir::StmtKind::Semi(e) => print_expr(cx, e, 0), } } - // fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx - // hir::ForeignItem) { - // if !has_attr(&item.attrs) { - // return; - // } - // } - // } fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool { @@ -171,6 +142,10 @@ fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) { print_expr(cx, arg, indent + 1); } }, + hir::ExprKind::Let(pat, expr, _) => { + print_pat(cx, pat, indent + 1); + print_expr(cx, expr, indent + 1); + }, hir::ExprKind::MethodCall(path, _, args, _) => { println!("{}MethodCall", ind); println!("{}method name: {}", ind, path.ident.name); @@ -406,6 +381,13 @@ fn print_item(cx: &LateContext<'_>, item: &hir::Item<'_>) { let item_ty = cx.tcx.type_of(did); println!("function of type {:#?}", item_ty); }, + hir::ItemKind::Macro(ref macro_def) => { + if macro_def.macro_rules { + println!("macro introduced by `macro_rules!`"); + } else { + println!("macro introduced by `macro`"); + } + }, hir::ItemKind::Mod(..) => println!("module"), hir::ItemKind::ForeignMod { abi, .. } => println!("foreign module with abi: {}", abi), hir::ItemKind::GlobalAsm(asm) => println!("global asm: {:?}", asm), diff --git a/clippy_lints/src/utils/internal_lints.rs b/clippy_lints/src/utils/internal_lints.rs index d660008e7d18..42d51272279e 100644 --- a/clippy_lints/src/utils/internal_lints.rs +++ b/clippy_lints/src/utils/internal_lints.rs @@ -1,5 +1,6 @@ use clippy_utils::consts::{constant_simple, Constant}; use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then}; +use clippy_utils::higher; use clippy_utils::source::snippet; use clippy_utils::ty::match_type; use clippy_utils::{ @@ -17,8 +18,8 @@ use rustc_hir::def_id::DefId; use rustc_hir::hir_id::CRATE_HIR_ID; use rustc_hir::intravisit::{NestedVisitorMap, Visitor}; use rustc_hir::{ - BinOpKind, Block, Crate, Expr, ExprKind, HirId, Item, Local, MatchSource, MutTy, Mutability, Node, Path, Stmt, - StmtKind, Ty, TyKind, UnOp, + BinOpKind, Block, Crate, Expr, ExprKind, HirId, Item, Local, MutTy, Mutability, Node, Path, Stmt, StmtKind, Ty, + TyKind, UnOp, }; use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}; use rustc_middle::hir::map::Map; @@ -1106,16 +1107,10 @@ impl<'tcx> LateLintPass<'tcx> for IfChainStyle { } fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { - let (cond, then, els) = match expr.kind { - ExprKind::If(cond, then, els) => (Some(cond), then, els.is_some()), - ExprKind::Match( - _, - [arm, ..], - MatchSource::IfLetDesugar { - contains_else_clause: els, - }, - ) => (None, arm.body, els), - _ => return, + let (cond, then, els) = if let Some(higher::IfOrIfLet { cond, r#else, then }) = higher::IfOrIfLet::hir(expr) { + (cond, then, r#else.is_some()) + } else { + return; }; let then_block = match then.kind { ExprKind::Block(block, _) => block, @@ -1131,7 +1126,6 @@ impl<'tcx> LateLintPass<'tcx> for IfChainStyle { }; // check for `if a && b;` if_chain! { - if let Some(cond) = cond; if let ExprKind::Binary(op, _, _) = cond.kind; if op.node == BinOpKind::And; if cx.sess().source_map().is_multiline(cond.span); @@ -1166,9 +1160,7 @@ fn check_nested_if_chains( _ => return, }; if_chain! { - if matches!(tail.kind, - ExprKind::If(_, _, None) - | ExprKind::Match(.., MatchSource::IfLetDesugar { contains_else_clause: false })); + if let Some(higher::IfOrIfLet { r#else: None, .. }) = higher::IfOrIfLet::hir(tail); let sm = cx.sess().source_map(); if head .iter() diff --git a/clippy_lints/src/utils/internal_lints/metadata_collector.rs b/clippy_lints/src/utils/internal_lints/metadata_collector.rs index 91533695eb30..188d0419c399 100644 --- a/clippy_lints/src/utils/internal_lints/metadata_collector.rs +++ b/clippy_lints/src/utils/internal_lints/metadata_collector.rs @@ -786,8 +786,6 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for ApplicabilityResolver<'a, 'hir> { } }; - // TODO xFrednet 2021-03-01: support function arguments? - intravisit::walk_expr(self, expr); } } diff --git a/clippy_lints/src/vec.rs b/clippy_lints/src/vec.rs index 32fa46f042ce..85ff292c53a4 100644 --- a/clippy_lints/src/vec.rs +++ b/clippy_lints/src/vec.rs @@ -50,7 +50,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec { if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty_adjusted(expr).kind(); if let ty::Slice(..) = ty.kind(); if let ExprKind::AddrOf(BorrowKind::Ref, mutability, addressee) = expr.kind; - if let Some(vec_args) = higher::vec_macro(cx, addressee); + if let Some(vec_args) = higher::VecArgs::hir(cx, addressee); then { self.check_vec_macro(cx, &vec_args, mutability, expr.span); } @@ -58,8 +58,8 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec { // search for `for _ in vec![…]` if_chain! { - if let Some((_, arg, _, _)) = higher::for_loop(expr); - if let Some(vec_args) = higher::vec_macro(cx, arg); + if let Some(higher::ForLoop { arg, .. }) = higher::ForLoop::hir(expr); + if let Some(vec_args) = higher::VecArgs::hir(cx, arg); if is_copy(cx, vec_type(cx.typeck_results().expr_ty_adjusted(arg))); then { // report the error around the `vec!` not inside `:` diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 4553ac704a28..85d1f65c51f0 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -299,7 +299,7 @@ impl EarlyLintPass for Write { let nl_span = match (dest, only_nl) { // Special case of `write!(buf, "\n")`: Mark everything from the end of // `buf` for removal so no trailing comma [`writeln!(buf, )`] remains. - (Some(dest_expr), true) => Span::new(dest_expr.span.hi(), nl_span.hi(), nl_span.ctxt()), + (Some(dest_expr), true) => nl_span.with_lo(dest_expr.span.hi()), _ => nl_span, }; span_lint_and_then( diff --git a/clippy_utils/src/ast_utils.rs b/clippy_utils/src/ast_utils.rs index 30c2260d15ca..133f6c29f7d2 100644 --- a/clippy_utils/src/ast_utils.rs +++ b/clippy_utils/src/ast_utils.rs @@ -158,7 +158,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool { (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r), (Lit(l), Lit(r)) => l.kind == r.kind, (Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt), - (Let(lp, le), Let(rp, re)) => eq_pat(lp, rp) && eq_expr(le, re), + (Let(lp, le, _), Let(rp, re, _)) => eq_pat(lp, rp) && eq_expr(le, re), (If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re), (While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt), (ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => { @@ -221,7 +221,7 @@ pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool { (Local(l), Local(r)) => { eq_pat(&l.pat, &r.pat) && both(&l.ty, &r.ty, |l, r| eq_ty(l, r)) - && eq_expr_opt(&l.init, &r.init) + && eq_local_kind(&l.kind, &r.kind) && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r)) }, (Item(l), Item(r)) => eq_item(l, r, eq_item_kind), @@ -234,6 +234,16 @@ pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool { } } +pub fn eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool { + use LocalKind::*; + match (l, r) { + (Decl, Decl) => true, + (Init(l), Init(r)) => eq_expr(l, r), + (InitElse(li, le), InitElse(ri, re)) => eq_expr(li, ri) && eq_block(le, re), + _ => false, + } +} + pub fn eq_item(l: &Item, r: &Item, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool { eq_id(l.ident, r.ident) && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r)) diff --git a/clippy_utils/src/consts.rs b/clippy_utils/src/consts.rs index 15c27d1a996d..8bf31807d55d 100644 --- a/clippy_utils/src/consts.rs +++ b/clippy_utils/src/consts.rs @@ -329,7 +329,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> { vec.iter().map(|elem| self.expr(elem)).collect::>() } - /// Lookup a possibly constant expression from a `ExprKind::Path`. + /// Lookup a possibly constant expression from an `ExprKind::Path`. fn fetch_path(&mut self, qpath: &QPath<'_>, id: HirId, ty: Ty<'tcx>) -> Option { let res = self.typeck_results.qpath_res(qpath, id); match res { @@ -346,11 +346,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> { .tcx .const_eval_resolve( self.param_env, - ty::Unevaluated { - def: ty::WithOptConstParam::unknown(def_id), - substs, - promoted: None, - }, + ty::Unevaluated::new(ty::WithOptConstParam::unknown(def_id), substs), None, ) .ok() diff --git a/clippy_utils/src/eager_or_lazy.rs b/clippy_utils/src/eager_or_lazy.rs index 88b115a63d78..29e2559fc6d6 100644 --- a/clippy_utils/src/eager_or_lazy.rs +++ b/clippy_utils/src/eager_or_lazy.rs @@ -60,6 +60,7 @@ fn identify_some_pure_patterns(expr: &Expr<'_>) -> bool { | ExprKind::MethodCall(..) | ExprKind::Binary(..) | ExprKind::Unary(..) + | ExprKind::Let(..) | ExprKind::Cast(..) | ExprKind::Type(..) | ExprKind::DropTemps(..) diff --git a/clippy_utils/src/higher.rs b/clippy_utils/src/higher.rs index 884180f0586e..05a4a0143195 100644 --- a/clippy_utils/src/higher.rs +++ b/clippy_utils/src/higher.rs @@ -1,5 +1,4 @@ -//! This module contains functions for retrieve the original AST from lowered -//! `hir`. +//! This module contains functions that retrieves specifiec elements. #![deny(clippy::missing_docs_in_private_items)] @@ -7,142 +6,256 @@ use crate::{is_expn_of, match_def_path, paths}; use if_chain::if_chain; use rustc_ast::ast::{self, LitKind}; use rustc_hir as hir; -use rustc_hir::{BorrowKind, Expr, ExprKind, StmtKind, UnOp}; +use rustc_hir::{Arm, Block, BorrowKind, Expr, ExprKind, LoopSource, MatchSource, Node, Pat, StmtKind, UnOp}; use rustc_lint::LateContext; use rustc_span::{sym, ExpnKind, Span, Symbol}; -/// Represent a range akin to `ast::ExprKind::Range`. -#[derive(Debug, Copy, Clone)] -pub struct Range<'a> { - /// The lower bound of the range, or `None` for ranges such as `..X`. - pub start: Option<&'a hir::Expr<'a>>, - /// The upper bound of the range, or `None` for ranges such as `X..`. - pub end: Option<&'a hir::Expr<'a>>, - /// Whether the interval is open or closed. - pub limits: ast::RangeLimits, +/// The essential nodes of a desugared for loop as well as the entire span: +/// `for pat in arg { body }` becomes `(pat, arg, body)`. Return `(pat, arg, body, span)`. +pub struct ForLoop<'tcx> { + /// `for` loop item + pub pat: &'tcx hir::Pat<'tcx>, + /// `IntoIterator` argument + pub arg: &'tcx hir::Expr<'tcx>, + /// `for` loop body + pub body: &'tcx hir::Expr<'tcx>, + /// entire `for` loop span + pub span: Span, } -/// Higher a `hir` range to something similar to `ast::ExprKind::Range`. -pub fn range<'a>(expr: &'a hir::Expr<'_>) -> Option> { - /// Finds the field named `name` in the field. Always return `Some` for - /// convenience. - fn get_field<'c>(name: &str, fields: &'c [hir::ExprField<'_>]) -> Option<&'c hir::Expr<'c>> { - let expr = &fields.iter().find(|field| field.ident.name.as_str() == name)?.expr; - - Some(expr) +impl<'tcx> ForLoop<'tcx> { + #[inline] + /// Parses a desugared `for` loop + pub fn hir(expr: &Expr<'tcx>) -> Option { + if_chain! { + if let hir::ExprKind::Match(iterexpr, arms, hir::MatchSource::ForLoopDesugar) = expr.kind; + if let Some(first_arm) = arms.get(0); + if let hir::ExprKind::Call(_, iterargs) = iterexpr.kind; + if let Some(first_arg) = iterargs.get(0); + if iterargs.len() == 1 && arms.len() == 1 && first_arm.guard.is_none(); + if let hir::ExprKind::Loop(block, ..) = first_arm.body.kind; + if block.expr.is_none(); + if let [ _, _, ref let_stmt, ref body ] = *block.stmts; + if let hir::StmtKind::Local(local) = let_stmt.kind; + if let hir::StmtKind::Expr(body_expr) = body.kind; + then { + return Some(Self { + pat: &*local.pat, + arg: first_arg, + body: body_expr, + span: first_arm.span + }); + } + } + None } +} - match expr.kind { - hir::ExprKind::Call(path, args) - if matches!( - path.kind, - hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, _)) - ) => +/// An `if` expression without `DropTemps` +pub struct If<'hir> { + /// `if` condition + pub cond: &'hir Expr<'hir>, + /// `if` then expression + pub then: &'hir Expr<'hir>, + /// `else` expression + pub r#else: Option<&'hir Expr<'hir>>, +} + +impl<'hir> If<'hir> { + #[inline] + /// Parses an `if` expression + pub const fn hir(expr: &Expr<'hir>) -> Option { + if let ExprKind::If( + Expr { + kind: ExprKind::DropTemps(cond), + .. + }, + then, + r#else, + ) = expr.kind { - Some(Range { - start: Some(&args[0]), - end: Some(&args[1]), - limits: ast::RangeLimits::Closed, - }) - }, - hir::ExprKind::Struct(path, fields, None) => match path { - hir::QPath::LangItem(hir::LangItem::RangeFull, _) => Some(Range { - start: None, - end: None, - limits: ast::RangeLimits::HalfOpen, - }), - hir::QPath::LangItem(hir::LangItem::RangeFrom, _) => Some(Range { - start: Some(get_field("start", fields)?), - end: None, - limits: ast::RangeLimits::HalfOpen, - }), - hir::QPath::LangItem(hir::LangItem::Range, _) => Some(Range { - start: Some(get_field("start", fields)?), - end: Some(get_field("end", fields)?), - limits: ast::RangeLimits::HalfOpen, - }), - hir::QPath::LangItem(hir::LangItem::RangeToInclusive, _) => Some(Range { - start: None, - end: Some(get_field("end", fields)?), - limits: ast::RangeLimits::Closed, - }), - hir::QPath::LangItem(hir::LangItem::RangeTo, _) => Some(Range { - start: None, - end: Some(get_field("end", fields)?), - limits: ast::RangeLimits::HalfOpen, - }), - _ => None, - }, - _ => None, + Some(Self { cond, then, r#else }) + } else { + None + } } } -/// Checks if a `let` statement is from a `for` loop desugaring. -pub fn is_from_for_desugar(local: &hir::Local<'_>) -> bool { - // This will detect plain for-loops without an actual variable binding: - // - // ``` - // for x in some_vec { - // // do stuff - // } - // ``` - if_chain! { - if let Some(expr) = local.init; - if let hir::ExprKind::Match(_, _, hir::MatchSource::ForLoopDesugar) = expr.kind; - then { - return true; +/// An `if let` expression +pub struct IfLet<'hir> { + /// `if let` pattern + pub let_pat: &'hir Pat<'hir>, + /// `if let` scrutinee + pub let_expr: &'hir Expr<'hir>, + /// `if let` then expression + pub if_then: &'hir Expr<'hir>, + /// `if let` else expression + pub if_else: Option<&'hir Expr<'hir>>, +} + +impl<'hir> IfLet<'hir> { + /// Parses an `if let` expression + pub fn hir(cx: &LateContext<'_>, expr: &Expr<'hir>) -> Option { + if let ExprKind::If( + Expr { + kind: ExprKind::Let(let_pat, let_expr, _), + .. + }, + if_then, + if_else, + ) = expr.kind + { + let hir = cx.tcx.hir(); + let mut iter = hir.parent_iter(expr.hir_id); + if let Some((_, Node::Block(Block { stmts: [], .. }))) = iter.next() { + if let Some(( + _, + Node::Expr(Expr { + kind: ExprKind::Loop(_, _, LoopSource::While, _), + .. + }), + )) = iter.next() + { + // while loop desugar + return None; + } + } + return Some(Self { + let_pat, + let_expr, + if_then, + if_else, + }); } + None } +} - // This detects a variable binding in for loop to avoid `let_unit_value` - // lint (see issue #1964). - // - // ``` - // for _ in vec![()] { - // // anything - // } - // ``` - if let hir::LocalSource::ForLoopDesugar = local.source { - return true; +/// An `if let` or `match` expression. Useful for lints that trigger on one or the other. +pub enum IfLetOrMatch<'hir> { + /// Any `match` expression + Match(&'hir Expr<'hir>, &'hir [Arm<'hir>], MatchSource), + /// scrutinee, pattern, then block, else block + IfLet( + &'hir Expr<'hir>, + &'hir Pat<'hir>, + &'hir Expr<'hir>, + Option<&'hir Expr<'hir>>, + ), +} + +impl<'hir> IfLetOrMatch<'hir> { + /// Parses an `if let` or `match` expression + pub fn parse(cx: &LateContext<'_>, expr: &Expr<'hir>) -> Option { + match expr.kind { + ExprKind::Match(expr, arms, source) => Some(Self::Match(expr, arms, source)), + _ => IfLet::hir(cx, expr).map( + |IfLet { + let_expr, + let_pat, + if_then, + if_else, + }| { Self::IfLet(let_expr, let_pat, if_then, if_else) }, + ), + } } +} - false +/// An `if` or `if let` expression +pub struct IfOrIfLet<'hir> { + /// `if` condition that is maybe a `let` expression + pub cond: &'hir Expr<'hir>, + /// `if` then expression + pub then: &'hir Expr<'hir>, + /// `else` expression + pub r#else: Option<&'hir Expr<'hir>>, } -/// Recover the essential nodes of a desugared for loop as well as the entire span: -/// `for pat in arg { body }` becomes `(pat, arg, body)`. Return `(pat, arg, body, span)`. -pub fn for_loop<'tcx>( - expr: &'tcx hir::Expr<'tcx>, -) -> Option<(&hir::Pat<'_>, &'tcx hir::Expr<'tcx>, &'tcx hir::Expr<'tcx>, Span)> { - if_chain! { - if let hir::ExprKind::Match(iterexpr, arms, hir::MatchSource::ForLoopDesugar) = expr.kind; - if let hir::ExprKind::Call(_, iterargs) = iterexpr.kind; - if iterargs.len() == 1 && arms.len() == 1 && arms[0].guard.is_none(); - if let hir::ExprKind::Loop(block, ..) = arms[0].body.kind; - if block.expr.is_none(); - if let [ _, _, ref let_stmt, ref body ] = *block.stmts; - if let hir::StmtKind::Local(local) = let_stmt.kind; - if let hir::StmtKind::Expr(expr) = body.kind; - then { - return Some((&*local.pat, &iterargs[0], expr, arms[0].span)); +impl<'hir> IfOrIfLet<'hir> { + #[inline] + /// Parses an `if` or `if let` expression + pub const fn hir(expr: &Expr<'hir>) -> Option { + if let ExprKind::If(cond, then, r#else) = expr.kind { + if let ExprKind::DropTemps(new_cond) = cond.kind { + return Some(Self { + cond: new_cond, + r#else, + then, + }); + } + if let ExprKind::Let(..) = cond.kind { + return Some(Self { cond, then, r#else }); + } } + None } - None } -/// Recover the essential nodes of a desugared while loop: -/// `while cond { body }` becomes `(cond, body)`. -pub fn while_loop<'tcx>(expr: &'tcx hir::Expr<'tcx>) -> Option<(&'tcx hir::Expr<'tcx>, &'tcx hir::Expr<'tcx>)> { - if_chain! { - if let hir::ExprKind::Loop(hir::Block { expr: Some(expr), .. }, _, hir::LoopSource::While, _) = &expr.kind; - if let hir::ExprKind::Match(cond, arms, hir::MatchSource::WhileDesugar) = &expr.kind; - if let hir::ExprKind::DropTemps(cond) = &cond.kind; - if let [hir::Arm { body, .. }, ..] = &arms[..]; - then { - return Some((cond, body)); +/// Represent a range akin to `ast::ExprKind::Range`. +#[derive(Debug, Copy, Clone)] +pub struct Range<'a> { + /// The lower bound of the range, or `None` for ranges such as `..X`. + pub start: Option<&'a hir::Expr<'a>>, + /// The upper bound of the range, or `None` for ranges such as `X..`. + pub end: Option<&'a hir::Expr<'a>>, + /// Whether the interval is open or closed. + pub limits: ast::RangeLimits, +} + +impl<'a> Range<'a> { + /// Higher a `hir` range to something similar to `ast::ExprKind::Range`. + pub fn hir(expr: &'a hir::Expr<'_>) -> Option> { + /// Finds the field named `name` in the field. Always return `Some` for + /// convenience. + fn get_field<'c>(name: &str, fields: &'c [hir::ExprField<'_>]) -> Option<&'c hir::Expr<'c>> { + let expr = &fields.iter().find(|field| field.ident.name.as_str() == name)?.expr; + Some(expr) + } + + match expr.kind { + hir::ExprKind::Call(path, args) + if matches!( + path.kind, + hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, _)) + ) => + { + Some(Range { + start: Some(&args[0]), + end: Some(&args[1]), + limits: ast::RangeLimits::Closed, + }) + }, + hir::ExprKind::Struct(path, fields, None) => match &path { + hir::QPath::LangItem(hir::LangItem::RangeFull, _) => Some(Range { + start: None, + end: None, + limits: ast::RangeLimits::HalfOpen, + }), + hir::QPath::LangItem(hir::LangItem::RangeFrom, _) => Some(Range { + start: Some(get_field("start", fields)?), + end: None, + limits: ast::RangeLimits::HalfOpen, + }), + hir::QPath::LangItem(hir::LangItem::Range, _) => Some(Range { + start: Some(get_field("start", fields)?), + end: Some(get_field("end", fields)?), + limits: ast::RangeLimits::HalfOpen, + }), + hir::QPath::LangItem(hir::LangItem::RangeToInclusive, _) => Some(Range { + start: None, + end: Some(get_field("end", fields)?), + limits: ast::RangeLimits::Closed, + }), + hir::QPath::LangItem(hir::LangItem::RangeTo, _) => Some(Range { + start: None, + end: Some(get_field("end", fields)?), + limits: ast::RangeLimits::HalfOpen, + }), + _ => None, + }, + _ => None, } } - None } /// Represent the pre-expansion arguments of a `vec!` invocation. @@ -153,41 +266,154 @@ pub enum VecArgs<'a> { Vec(&'a [hir::Expr<'a>]), } -/// Returns the arguments of the `vec!` macro if this expression was expanded -/// from `vec!`. -pub fn vec_macro<'e>(cx: &LateContext<'_>, expr: &'e hir::Expr<'_>) -> Option> { - if_chain! { - if let hir::ExprKind::Call(fun, args) = expr.kind; - if let hir::ExprKind::Path(ref qpath) = fun.kind; - if is_expn_of(fun.span, "vec").is_some(); - if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id(); - then { - return if match_def_path(cx, fun_def_id, &paths::VEC_FROM_ELEM) && args.len() == 2 { - // `vec![elem; size]` case - Some(VecArgs::Repeat(&args[0], &args[1])) - } - else if match_def_path(cx, fun_def_id, &paths::SLICE_INTO_VEC) && args.len() == 1 { - // `vec![a, b, c]` case - if_chain! { - if let hir::ExprKind::Box(boxed) = args[0].kind; - if let hir::ExprKind::Array(args) = boxed.kind; - then { - return Some(VecArgs::Vec(&*args)); - } +impl<'a> VecArgs<'a> { + /// Returns the arguments of the `vec!` macro if this expression was expanded + /// from `vec!`. + pub fn hir(cx: &LateContext<'_>, expr: &'a hir::Expr<'_>) -> Option> { + if_chain! { + if let hir::ExprKind::Call(fun, args) = expr.kind; + if let hir::ExprKind::Path(ref qpath) = fun.kind; + if is_expn_of(fun.span, "vec").is_some(); + if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id(); + then { + return if match_def_path(cx, fun_def_id, &paths::VEC_FROM_ELEM) && args.len() == 2 { + // `vec![elem; size]` case + Some(VecArgs::Repeat(&args[0], &args[1])) } + else if match_def_path(cx, fun_def_id, &paths::SLICE_INTO_VEC) && args.len() == 1 { + // `vec![a, b, c]` case + if_chain! { + if let hir::ExprKind::Box(boxed) = args[0].kind; + if let hir::ExprKind::Array(args) = boxed.kind; + then { + return Some(VecArgs::Vec(args)); + } + } - None - } - else if match_def_path(cx, fun_def_id, &paths::VEC_NEW) && args.is_empty() { - Some(VecArgs::Vec(&[])) + None + } + else if match_def_path(cx, fun_def_id, &paths::VEC_NEW) && args.is_empty() { + Some(VecArgs::Vec(&[])) + } + else { + None + }; } - else { - None - }; } + + None } +} - None +/// A desugared `while` loop +pub struct While<'hir> { + /// `while` loop condition + pub condition: &'hir Expr<'hir>, + /// `while` loop body + pub body: &'hir Expr<'hir>, +} + +impl<'hir> While<'hir> { + #[inline] + /// Parses a desugared `while` loop + pub const fn hir(expr: &Expr<'hir>) -> Option { + if let ExprKind::Loop( + Block { + expr: + Some(Expr { + kind: + ExprKind::If( + Expr { + kind: ExprKind::DropTemps(condition), + .. + }, + body, + _, + ), + .. + }), + .. + }, + _, + LoopSource::While, + _, + ) = expr.kind + { + return Some(Self { condition, body }); + } + None + } +} + +/// A desugared `while let` loop +pub struct WhileLet<'hir> { + /// `while let` loop item pattern + pub let_pat: &'hir Pat<'hir>, + /// `while let` loop scrutinee + pub let_expr: &'hir Expr<'hir>, + /// `while let` loop body + pub if_then: &'hir Expr<'hir>, +} + +impl<'hir> WhileLet<'hir> { + #[inline] + /// Parses a desugared `while let` loop + pub const fn hir(expr: &Expr<'hir>) -> Option { + if let ExprKind::Loop( + Block { + expr: + Some(Expr { + kind: + ExprKind::If( + Expr { + kind: ExprKind::Let(let_pat, let_expr, _), + .. + }, + if_then, + _, + ), + .. + }), + .. + }, + _, + LoopSource::While, + _, + ) = expr.kind + { + return Some(Self { + let_pat, + let_expr, + if_then, + }); + } + None + } +} + +/// Converts a hir binary operator to the corresponding `ast` type. +#[must_use] +pub fn binop(op: hir::BinOpKind) -> ast::BinOpKind { + match op { + hir::BinOpKind::Eq => ast::BinOpKind::Eq, + hir::BinOpKind::Ge => ast::BinOpKind::Ge, + hir::BinOpKind::Gt => ast::BinOpKind::Gt, + hir::BinOpKind::Le => ast::BinOpKind::Le, + hir::BinOpKind::Lt => ast::BinOpKind::Lt, + hir::BinOpKind::Ne => ast::BinOpKind::Ne, + hir::BinOpKind::Or => ast::BinOpKind::Or, + hir::BinOpKind::Add => ast::BinOpKind::Add, + hir::BinOpKind::And => ast::BinOpKind::And, + hir::BinOpKind::BitAnd => ast::BinOpKind::BitAnd, + hir::BinOpKind::BitOr => ast::BinOpKind::BitOr, + hir::BinOpKind::BitXor => ast::BinOpKind::BitXor, + hir::BinOpKind::Div => ast::BinOpKind::Div, + hir::BinOpKind::Mul => ast::BinOpKind::Mul, + hir::BinOpKind::Rem => ast::BinOpKind::Rem, + hir::BinOpKind::Shl => ast::BinOpKind::Shl, + hir::BinOpKind::Shr => ast::BinOpKind::Shr, + hir::BinOpKind::Sub => ast::BinOpKind::Sub, + } } /// Extract args from an assert-like macro. @@ -218,8 +444,8 @@ pub fn extract_assert_macro_args<'tcx>(e: &'tcx Expr<'tcx>) -> Option { if let ExpnKind::Macro(_, name) = expr.span.ctxt().outer_expn_data().kind; let name = name.as_str(); if name.ends_with("format_args") || name.ends_with("format_args_nl"); - if let ExprKind::Call(_, args) = expr.kind; - if let Some((strs_ref, args, fmt_expr)) = match args { + + if let ExprKind::Match(inner_match, [arm], _) = expr.kind; + + // `match match`, if you will + if let ExprKind::Match(args, [inner_arm], _) = inner_match.kind; + if let ExprKind::Tup(value_args) = args.kind; + if let Some(value_args) = value_args + .iter() + .map(|e| match e.kind { + ExprKind::AddrOf(_, _, e) => Some(e), + _ => None, + }) + .collect(); + if let ExprKind::Array(args) = inner_arm.body.kind; + + if let ExprKind::Block(Block { stmts: [], expr: Some(expr), .. }, _) = arm.body.kind; + if let ExprKind::Call(_, call_args) = expr.kind; + if let Some((strs_ref, fmt_expr)) = match call_args { // Arguments::new_v1 - [strs_ref, args] => Some((strs_ref, args, None)), + [strs_ref, _] => Some((strs_ref, None)), // Arguments::new_v1_formatted - [strs_ref, args, fmt_expr] => Some((strs_ref, args, Some(fmt_expr))), + [strs_ref, _, fmt_expr] => Some((strs_ref, Some(fmt_expr))), _ => None, }; if let ExprKind::AddrOf(BorrowKind::Ref, _, strs_arr) = strs_ref.kind; @@ -319,17 +561,6 @@ impl FormatArgsExpn<'tcx> { None }) .collect(); - if let ExprKind::AddrOf(BorrowKind::Ref, _, args) = args.kind; - if let ExprKind::Match(args, [arm], _) = args.kind; - if let ExprKind::Tup(value_args) = args.kind; - if let Some(value_args) = value_args - .iter() - .map(|e| match e.kind { - ExprKind::AddrOf(_, _, e) => Some(e), - _ => None, - }) - .collect(); - if let ExprKind::Array(args) = arm.body.kind; then { Some(FormatArgsExpn { format_string_span: strs_ref.span, @@ -345,3 +576,35 @@ impl FormatArgsExpn<'tcx> { } } } + +/// Checks if a `let` statement is from a `for` loop desugaring. +pub fn is_from_for_desugar(local: &hir::Local<'_>) -> bool { + // This will detect plain for-loops without an actual variable binding: + // + // ``` + // for x in some_vec { + // // do stuff + // } + // ``` + if_chain! { + if let Some(expr) = local.init; + if let hir::ExprKind::Match(_, _, hir::MatchSource::ForLoopDesugar) = expr.kind; + then { + return true; + } + } + + // This detects a variable binding in for loop to avoid `let_unit_value` + // lint (see issue #1964). + // + // ``` + // for _ in vec![()] { + // // anything + // } + // ``` + if let hir::LocalSource::ForLoopDesugar = local.source { + return true; + } + + false +} diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs index fd70553e064b..6e9a1de21eef 100644 --- a/clippy_utils/src/hir_utils.rs +++ b/clippy_utils/src/hir_utils.rs @@ -232,6 +232,7 @@ impl HirEqInterExpr<'_, '_, '_> { (&ExprKind::If(lc, lt, ref le), &ExprKind::If(rc, rt, ref re)) => { self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r)) }, + (&ExprKind::Let(lp, le, _), &ExprKind::Let(rp, re, _)) => self.eq_pat(lp, rp) && self.eq_expr(le, re), (&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node, (&ExprKind::Loop(lb, ref ll, ref lls, _), &ExprKind::Loop(rb, ref rl, ref rls, _)) => { lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name) @@ -665,6 +666,10 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> { } } }, + ExprKind::Let(pat, expr, _) => { + self.hash_expr(expr); + self.hash_pat(pat); + }, ExprKind::LlvmInlineAsm(..) | ExprKind::Err => {}, ExprKind::Lit(ref l) => { l.node.hash(&mut self.s); diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 4d8d4709e04a..40d7963a5e54 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -259,6 +259,20 @@ pub fn in_macro(span: Span) -> bool { } } +pub fn is_unit_expr(expr: &Expr<'_>) -> bool { + matches!( + expr.kind, + ExprKind::Block( + Block { + stmts: [], + expr: None, + .. + }, + _ + ) | ExprKind::Tup([]) + ) +} + /// Checks if given pattern is a wildcard (`_`) pub fn is_wild(pat: &Pat<'_>) -> bool { matches!(pat.kind, PatKind::Wild) @@ -588,7 +602,7 @@ pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Optio /// For example, if `e` represents the `v[0].a.b[x]` /// this method will return a tuple, composed of a `Vec` /// containing the `Expr`s for `v[0], v[0].a, v[0].a.b, v[0].a.b[x]` -/// and a `Expr` for root of them, `v` +/// and an `Expr` for root of them, `v` fn projection_stack<'a, 'hir>(mut e: &'a Expr<'hir>) -> (Vec<&'a Expr<'hir>>, &'a Expr<'hir>) { let mut result = vec![]; let root = loop { @@ -794,6 +808,13 @@ pub fn capture_local_usage(cx: &LateContext<'tcx>, e: &Expr<'_>) -> CaptureKind capture_expr_ty = e; } }, + ExprKind::Let(pat, ..) => { + let mutability = match pat_capture_kind(cx, pat) { + CaptureKind::Value => Mutability::Not, + CaptureKind::Ref(m) => m, + }; + return CaptureKind::Ref(mutability); + }, ExprKind::Match(_, arms, _) => { let mut mutability = Mutability::Not; for capture in arms.iter().map(|arm| pat_capture_kind(cx, arm.pat)) { @@ -1098,7 +1119,7 @@ fn line_span(cx: &T, span: Span) -> Span { let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap(); let line_no = source_map_and_line.line; let line_start = source_map_and_line.sf.lines[line_no]; - Span::new(line_start, span.hi(), span.ctxt()) + span.with_lo(line_start) } /// Gets the parent node, if any. @@ -1182,17 +1203,6 @@ pub fn is_else_clause(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool { let map = tcx.hir(); let mut iter = map.parent_iter(expr.hir_id); match iter.next() { - Some((arm_id, Node::Arm(..))) => matches!( - iter.next(), - Some(( - _, - Node::Expr(Expr { - kind: ExprKind::Match(_, [_, else_arm], MatchSource::IfLetDesugar { .. }), - .. - }) - )) - if else_arm.hir_id == arm_id - ), Some(( _, Node::Expr(Expr { @@ -1591,15 +1601,15 @@ pub fn if_sequence<'tcx>(mut expr: &'tcx Expr<'tcx>) -> (Vec<&'tcx Expr<'tcx>>, let mut conds = Vec::new(); let mut blocks: Vec<&Block<'_>> = Vec::new(); - while let ExprKind::If(cond, then_expr, ref else_expr) = expr.kind { - conds.push(cond); - if let ExprKind::Block(block, _) = then_expr.kind { + while let Some(higher::IfOrIfLet { cond, then, r#else }) = higher::IfOrIfLet::hir(expr) { + conds.push(&*cond); + if let ExprKind::Block(block, _) = then.kind { blocks.push(block); } else { panic!("ExprKind::If node is not an ExprKind::Block"); } - if let Some(else_expr) = *else_expr { + if let Some(else_expr) = r#else { expr = else_expr; } else { break; @@ -1809,7 +1819,7 @@ pub fn fn_has_unsatisfiable_preds(cx: &LateContext<'_>, did: DefId) -> bool { .predicates_of(did) .predicates .iter() - .filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None }); + .filter_map(|(p, _)| if p.is_global(cx.tcx) { Some(*p) } else { None }); traits::impossible_predicates( cx.tcx, traits::elaborate_predicates(cx.tcx, predicates) @@ -1855,7 +1865,7 @@ pub fn is_slice_of_primitives(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option return Some("slice".into()), rustc_ty::Array(..) => return Some("array".into()), rustc_ty::Tuple(..) => return Some("tuple".into()), @@ -1863,7 +1873,7 @@ pub fn is_slice_of_primitives(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option, body: &'a Body<'tcx>, msrv: Option<&Ru ty::PredicateKind::ObjectSafe(_) => panic!("object safe predicate on function: {:#?}", predicate), ty::PredicateKind::ClosureKind(..) => panic!("closure kind predicate on function: {:#?}", predicate), ty::PredicateKind::Subtype(_) => panic!("subtype predicate on function: {:#?}", predicate), - ty::PredicateKind::Trait(pred, _) => { + ty::PredicateKind::Coerce(_) => panic!("coerce predicate on function: {:#?}", predicate), + ty::PredicateKind::Trait(pred) => { if Some(pred.def_id()) == tcx.lang_items().sized_trait() { continue; } @@ -85,7 +86,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, msrv: Option<&Ru } fn check_ty(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult { - for arg in ty.walk() { + for arg in ty.walk(tcx) { let ty = match arg.unpack() { GenericArgKind::Type(ty) => ty, diff --git a/clippy_utils/src/sugg.rs b/clippy_utils/src/sugg.rs index 3bd75b10e905..ab05a0b42385 100644 --- a/clippy_utils/src/sugg.rs +++ b/clippy_utils/src/sugg.rs @@ -116,7 +116,7 @@ impl<'a> Sugg<'a> { /// Generate a suggestion for an expression with the given snippet. This is used by the `hir_*` /// function variants of `Sugg`, since these use different snippet functions. fn hir_from_snippet(expr: &hir::Expr<'_>, snippet: Cow<'a, str>) -> Self { - if let Some(range) = higher::range(expr) { + if let Some(range) = higher::Range::hir(expr) { let op = match range.limits { ast::RangeLimits::HalfOpen => AssocOp::DotDot, ast::RangeLimits::Closed => AssocOp::DotDotEq, @@ -128,6 +128,7 @@ impl<'a> Sugg<'a> { hir::ExprKind::AddrOf(..) | hir::ExprKind::Box(..) | hir::ExprKind::If(..) + | hir::ExprKind::Let(..) | hir::ExprKind::Closure(..) | hir::ExprKind::Unary(..) | hir::ExprKind::Match(..) => Sugg::MaybeParen(snippet), @@ -328,7 +329,7 @@ fn has_enclosing_paren(sugg: impl AsRef) -> bool { } } -// Copied from the rust standart library, and then edited +/// Copied from the rust standard library, and then edited macro_rules! forward_binop_impls_to_ref { (impl $imp:ident, $method:ident for $t:ty, type Output = $o:ty) => { impl $imp<$t> for &$t { @@ -433,7 +434,7 @@ pub fn make_assoc(op: AssocOp, lhs: &Sugg<'_>, rhs: &Sugg<'_>) -> Sugg<'static> matches!(op, AssocOp::ShiftLeft | AssocOp::ShiftRight) } - /// Returns `true` if the operator is a arithmetic operator + /// Returns `true` if the operator is an arithmetic operator /// (i.e., `+`, `-`, `*`, `/`, `%`). fn is_arith(op: AssocOp) -> bool { matches!( diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 4f9aaf396b80..7f7fbdc9f214 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -10,7 +10,7 @@ use rustc_hir::{TyKind, Unsafety}; use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::LateContext; use rustc_middle::ty::subst::{GenericArg, GenericArgKind}; -use rustc_middle::ty::{self, AdtDef, IntTy, Ty, TypeFoldable, UintTy}; +use rustc_middle::ty::{self, AdtDef, IntTy, Ty, TyCtxt, TypeFoldable, UintTy}; use rustc_span::sym; use rustc_span::symbol::{Ident, Symbol}; use rustc_span::DUMMY_SP; @@ -36,8 +36,8 @@ pub fn can_partially_move_ty(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { } /// Walks into `ty` and returns `true` if any inner type is the same as `other_ty` -pub fn contains_ty(ty: Ty<'_>, other_ty: Ty<'_>) -> bool { - ty.walk().any(|inner| match inner.unpack() { +pub fn contains_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, other_ty: Ty<'tcx>) -> bool { + ty.walk(tcx).any(|inner| match inner.unpack() { GenericArgKind::Type(inner_ty) => ty::TyS::same_type(other_ty, inner_ty), GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, }) @@ -45,8 +45,8 @@ pub fn contains_ty(ty: Ty<'_>, other_ty: Ty<'_>) -> bool { /// Walks into `ty` and returns `true` if any inner type is an instance of the given adt /// constructor. -pub fn contains_adt_constructor(ty: Ty<'_>, adt: &AdtDef) -> bool { - ty.walk().any(|inner| match inner.unpack() { +pub fn contains_adt_constructor<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, adt: &'tcx AdtDef) -> bool { + ty.walk(tcx).any(|inner| match inner.unpack() { GenericArgKind::Type(inner_ty) => inner_ty.ty_adt_def() == Some(adt), GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, }) @@ -157,7 +157,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { ty::Tuple(substs) => substs.types().any(|ty| is_must_use_ty(cx, ty)), ty::Opaque(ref def_id, _) => { for (predicate, _) in cx.tcx.explicit_item_bounds(*def_id) { - if let ty::PredicateKind::Trait(trait_predicate, _) = predicate.kind().skip_binder() { + if let ty::PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() { if must_use_attr(cx.tcx.get_attrs(trait_predicate.trait_ref.def_id)).is_some() { return true; } @@ -209,7 +209,7 @@ fn is_normalizable_helper<'tcx>( .iter() .all(|field| is_normalizable_helper(cx, param_env, field.ty(cx.tcx, substs), cache)) }), - _ => ty.walk().all(|generic_arg| match generic_arg.unpack() { + _ => ty.walk(cx.tcx).all(|generic_arg| match generic_arg.unpack() { GenericArgKind::Type(inner_ty) if inner_ty != ty => { is_normalizable_helper(cx, param_env, inner_ty, cache) }, diff --git a/rust-toolchain b/rust-toolchain index 23887f178454..8b0563eda6d1 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2021-08-12" +channel = "nightly-2021-09-02" components = ["llvm-tools-preview", "rustc-dev", "rust-src"] diff --git a/tests/ui/atomic_ordering_bool.rs b/tests/ui/atomic_ordering_bool.rs deleted file mode 100644 index cdbde79b19eb..000000000000 --- a/tests/ui/atomic_ordering_bool.rs +++ /dev/null @@ -1,25 +0,0 @@ -#![warn(clippy::invalid_atomic_ordering)] - -use std::sync::atomic::{AtomicBool, Ordering}; - -fn main() { - let x = AtomicBool::new(true); - - // Allowed load ordering modes - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - - // Disallowed load ordering modes - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - // Allowed store ordering modes - x.store(false, Ordering::Release); - x.store(false, Ordering::SeqCst); - x.store(false, Ordering::Relaxed); - - // Disallowed store ordering modes - x.store(false, Ordering::Acquire); - x.store(false, Ordering::AcqRel); -} diff --git a/tests/ui/atomic_ordering_bool.stderr b/tests/ui/atomic_ordering_bool.stderr deleted file mode 100644 index 397b893aed96..000000000000 --- a/tests/ui/atomic_ordering_bool.stderr +++ /dev/null @@ -1,35 +0,0 @@ -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_bool.rs:14:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::invalid-atomic-ordering` implied by `-D warnings` - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_bool.rs:15:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_bool.rs:23:20 - | -LL | x.store(false, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_bool.rs:24:20 - | -LL | x.store(false, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: aborting due to 4 previous errors - diff --git a/tests/ui/atomic_ordering_exchange.rs b/tests/ui/atomic_ordering_exchange.rs deleted file mode 100644 index 1ddc12f9ab21..000000000000 --- a/tests/ui/atomic_ordering_exchange.rs +++ /dev/null @@ -1,45 +0,0 @@ -#![warn(clippy::invalid_atomic_ordering)] - -use std::sync::atomic::{AtomicUsize, Ordering}; - -fn main() { - // `compare_exchange` (not weak) testing - let x = AtomicUsize::new(0); - - // Allowed ordering combos - let _ = x.compare_exchange(0, 0, Ordering::Relaxed, Ordering::Relaxed); - let _ = x.compare_exchange(0, 0, Ordering::Acquire, Ordering::Acquire); - let _ = x.compare_exchange(0, 0, Ordering::Acquire, Ordering::Relaxed); - let _ = x.compare_exchange(0, 0, Ordering::Release, Ordering::Relaxed); - let _ = x.compare_exchange(0, 0, Ordering::AcqRel, Ordering::Acquire); - let _ = x.compare_exchange(0, 0, Ordering::AcqRel, Ordering::Relaxed); - let _ = x.compare_exchange(0, 0, Ordering::SeqCst, Ordering::Relaxed); - let _ = x.compare_exchange(0, 0, Ordering::SeqCst, Ordering::Acquire); - let _ = x.compare_exchange(0, 0, Ordering::SeqCst, Ordering::SeqCst); - - // AcqRel is always forbidden as a failure ordering - let _ = x.compare_exchange(0, 0, Ordering::Relaxed, Ordering::AcqRel); - let _ = x.compare_exchange(0, 0, Ordering::Acquire, Ordering::AcqRel); - let _ = x.compare_exchange(0, 0, Ordering::Release, Ordering::AcqRel); - let _ = x.compare_exchange(0, 0, Ordering::AcqRel, Ordering::AcqRel); - let _ = x.compare_exchange(0, 0, Ordering::SeqCst, Ordering::AcqRel); - - // Release is always forbidden as a failure ordering - let _ = x.compare_exchange(0, 0, Ordering::Relaxed, Ordering::Release); - let _ = x.compare_exchange(0, 0, Ordering::Acquire, Ordering::Release); - let _ = x.compare_exchange(0, 0, Ordering::Release, Ordering::Release); - let _ = x.compare_exchange(0, 0, Ordering::AcqRel, Ordering::Release); - let _ = x.compare_exchange(0, 0, Ordering::SeqCst, Ordering::Release); - - // Release success order forbids failure order of Acquire or SeqCst - let _ = x.compare_exchange(0, 0, Ordering::Release, Ordering::Acquire); - let _ = x.compare_exchange(0, 0, Ordering::Release, Ordering::SeqCst); - - // Relaxed success order also forbids failure order of Acquire or SeqCst - let _ = x.compare_exchange(0, 0, Ordering::Relaxed, Ordering::SeqCst); - let _ = x.compare_exchange(0, 0, Ordering::Relaxed, Ordering::Acquire); - - // Acquire/AcqRel forbids failure order of SeqCst - let _ = x.compare_exchange(0, 0, Ordering::Acquire, Ordering::SeqCst); - let _ = x.compare_exchange(0, 0, Ordering::AcqRel, Ordering::SeqCst); -} diff --git a/tests/ui/atomic_ordering_exchange.stderr b/tests/ui/atomic_ordering_exchange.stderr deleted file mode 100644 index 4b9bfef79748..000000000000 --- a/tests/ui/atomic_ordering_exchange.stderr +++ /dev/null @@ -1,131 +0,0 @@ -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:21:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Relaxed, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::invalid-atomic-ordering` implied by `-D warnings` - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:22:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Acquire, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:23:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Release, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:24:56 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::AcqRel, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:25:56 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::SeqCst, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` instead - -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:28:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Relaxed, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:29:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Acquire, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:30:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Release, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:31:56 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::AcqRel, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:32:56 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::SeqCst, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` instead - -error: compare_exchange's failure ordering may not be stronger than the success ordering of `Release` - --> $DIR/atomic_ordering_exchange.rs:35:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Release, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange's failure ordering may not be stronger than the success ordering of `Release` - --> $DIR/atomic_ordering_exchange.rs:36:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Release, Ordering::SeqCst); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange's failure ordering may not be stronger than the success ordering of `Relaxed` - --> $DIR/atomic_ordering_exchange.rs:39:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Relaxed, Ordering::SeqCst); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange's failure ordering may not be stronger than the success ordering of `Relaxed` - --> $DIR/atomic_ordering_exchange.rs:40:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Relaxed, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange's failure ordering may not be stronger than the success ordering of `Acquire` - --> $DIR/atomic_ordering_exchange.rs:43:57 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::Acquire, Ordering::SeqCst); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange's failure ordering may not be stronger than the success ordering of `AcqRel` - --> $DIR/atomic_ordering_exchange.rs:44:56 - | -LL | let _ = x.compare_exchange(0, 0, Ordering::AcqRel, Ordering::SeqCst); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: aborting due to 16 previous errors - diff --git a/tests/ui/atomic_ordering_exchange_weak.rs b/tests/ui/atomic_ordering_exchange_weak.rs deleted file mode 100644 index 590699025072..000000000000 --- a/tests/ui/atomic_ordering_exchange_weak.rs +++ /dev/null @@ -1,47 +0,0 @@ -#![warn(clippy::invalid_atomic_ordering)] - -use std::sync::atomic::{AtomicPtr, Ordering}; - -fn main() { - let ptr = &mut 5; - let ptr2 = &mut 10; - // `compare_exchange_weak` testing - let x = AtomicPtr::new(ptr); - - // Allowed ordering combos - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Relaxed, Ordering::Relaxed); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Acquire, Ordering::Acquire); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Acquire, Ordering::Relaxed); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Release, Ordering::Relaxed); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::AcqRel, Ordering::Acquire); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::AcqRel, Ordering::Relaxed); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::SeqCst, Ordering::Relaxed); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::SeqCst, Ordering::Acquire); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::SeqCst, Ordering::SeqCst); - - // AcqRel is always forbidden as a failure ordering - let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Relaxed, Ordering::AcqRel); - let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Acquire, Ordering::AcqRel); - let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Release, Ordering::AcqRel); - let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::AcqRel, Ordering::AcqRel); - let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::SeqCst, Ordering::AcqRel); - - // Release is always forbidden as a failure ordering - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Relaxed, Ordering::Release); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Acquire, Ordering::Release); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Release, Ordering::Release); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::AcqRel, Ordering::Release); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::SeqCst, Ordering::Release); - - // Release success order forbids failure order of Acquire or SeqCst - let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Release, Ordering::Acquire); - let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Release, Ordering::SeqCst); - - // Relaxed success order also forbids failure order of Acquire or SeqCst - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Relaxed, Ordering::SeqCst); - let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Relaxed, Ordering::Acquire); - - // Acquire/AcqRel forbids failure order of SeqCst - let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Acquire, Ordering::SeqCst); - let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::AcqRel, Ordering::SeqCst); -} diff --git a/tests/ui/atomic_ordering_exchange_weak.stderr b/tests/ui/atomic_ordering_exchange_weak.stderr deleted file mode 100644 index de7026f3ffaf..000000000000 --- a/tests/ui/atomic_ordering_exchange_weak.stderr +++ /dev/null @@ -1,131 +0,0 @@ -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:23:67 - | -LL | let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Relaxed, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::invalid-atomic-ordering` implied by `-D warnings` - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:24:67 - | -LL | let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Acquire, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:25:67 - | -LL | let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Release, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:26:66 - | -LL | let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::AcqRel, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:27:66 - | -LL | let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::SeqCst, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:30:67 - | -LL | let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Relaxed, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:31:67 - | -LL | let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Acquire, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:32:67 - | -LL | let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Release, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:33:66 - | -LL | let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::AcqRel, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:34:66 - | -LL | let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::SeqCst, Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be stronger than the success ordering of `Release` - --> $DIR/atomic_ordering_exchange_weak.rs:37:67 - | -LL | let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Release, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be stronger than the success ordering of `Release` - --> $DIR/atomic_ordering_exchange_weak.rs:38:67 - | -LL | let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Release, Ordering::SeqCst); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be stronger than the success ordering of `Relaxed` - --> $DIR/atomic_ordering_exchange_weak.rs:41:67 - | -LL | let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Relaxed, Ordering::SeqCst); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be stronger than the success ordering of `Relaxed` - --> $DIR/atomic_ordering_exchange_weak.rs:42:67 - | -LL | let _ = x.compare_exchange_weak(ptr, ptr2, Ordering::Relaxed, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be stronger than the success ordering of `Acquire` - --> $DIR/atomic_ordering_exchange_weak.rs:45:67 - | -LL | let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::Acquire, Ordering::SeqCst); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: compare_exchange_weak's failure ordering may not be stronger than the success ordering of `AcqRel` - --> $DIR/atomic_ordering_exchange_weak.rs:46:66 - | -LL | let _ = x.compare_exchange_weak(ptr2, ptr, Ordering::AcqRel, Ordering::SeqCst); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: aborting due to 16 previous errors - diff --git a/tests/ui/atomic_ordering_fence.rs b/tests/ui/atomic_ordering_fence.rs deleted file mode 100644 index 5ee5182ca051..000000000000 --- a/tests/ui/atomic_ordering_fence.rs +++ /dev/null @@ -1,20 +0,0 @@ -#![warn(clippy::invalid_atomic_ordering)] - -use std::sync::atomic::{compiler_fence, fence, Ordering}; - -fn main() { - // Allowed fence ordering modes - fence(Ordering::Acquire); - fence(Ordering::Release); - fence(Ordering::AcqRel); - fence(Ordering::SeqCst); - - // Disallowed fence ordering modes - fence(Ordering::Relaxed); - - compiler_fence(Ordering::Acquire); - compiler_fence(Ordering::Release); - compiler_fence(Ordering::AcqRel); - compiler_fence(Ordering::SeqCst); - compiler_fence(Ordering::Relaxed); -} diff --git a/tests/ui/atomic_ordering_fence.stderr b/tests/ui/atomic_ordering_fence.stderr deleted file mode 100644 index 3ceff27d9ad5..000000000000 --- a/tests/ui/atomic_ordering_fence.stderr +++ /dev/null @@ -1,19 +0,0 @@ -error: memory fences cannot have `Relaxed` ordering - --> $DIR/atomic_ordering_fence.rs:13:11 - | -LL | fence(Ordering::Relaxed); - | ^^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::invalid-atomic-ordering` implied by `-D warnings` - = help: consider using ordering modes `Acquire`, `Release`, `AcqRel` or `SeqCst` - -error: memory fences cannot have `Relaxed` ordering - --> $DIR/atomic_ordering_fence.rs:19:20 - | -LL | compiler_fence(Ordering::Relaxed); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `Release`, `AcqRel` or `SeqCst` - -error: aborting due to 2 previous errors - diff --git a/tests/ui/atomic_ordering_fetch_update.rs b/tests/ui/atomic_ordering_fetch_update.rs deleted file mode 100644 index 550bdb001e4c..000000000000 --- a/tests/ui/atomic_ordering_fetch_update.rs +++ /dev/null @@ -1,45 +0,0 @@ -#![warn(clippy::invalid_atomic_ordering)] - -use std::sync::atomic::{AtomicIsize, Ordering}; - -fn main() { - // `fetch_update` testing - let x = AtomicIsize::new(0); - - // Allowed ordering combos - let _ = x.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Acquire, Ordering::Acquire, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Acquire, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Release, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::AcqRel, Ordering::Acquire, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::AcqRel, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::SeqCst, Ordering::Relaxed, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::SeqCst, Ordering::Acquire, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |old| Some(old + 1)); - - // AcqRel is always forbidden as a failure ordering - let _ = x.fetch_update(Ordering::Relaxed, Ordering::AcqRel, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Acquire, Ordering::AcqRel, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Release, Ordering::AcqRel, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::AcqRel, Ordering::AcqRel, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::SeqCst, Ordering::AcqRel, |old| Some(old + 1)); - - // Release is always forbidden as a failure ordering - let _ = x.fetch_update(Ordering::Relaxed, Ordering::Release, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Acquire, Ordering::Release, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Release, Ordering::Release, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::AcqRel, Ordering::Release, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::SeqCst, Ordering::Release, |old| Some(old + 1)); - - // Release success order forbids failure order of Acquire or SeqCst - let _ = x.fetch_update(Ordering::Release, Ordering::Acquire, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Release, Ordering::SeqCst, |old| Some(old + 1)); - - // Relaxed success order also forbids failure order of Acquire or SeqCst - let _ = x.fetch_update(Ordering::Relaxed, Ordering::SeqCst, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::Relaxed, Ordering::Acquire, |old| Some(old + 1)); - - // Acquire/AcqRel forbids failure order of SeqCst - let _ = x.fetch_update(Ordering::Acquire, Ordering::SeqCst, |old| Some(old + 1)); - let _ = x.fetch_update(Ordering::AcqRel, Ordering::SeqCst, |old| Some(old + 1)); -} diff --git a/tests/ui/atomic_ordering_fetch_update.stderr b/tests/ui/atomic_ordering_fetch_update.stderr deleted file mode 100644 index 694548ece97b..000000000000 --- a/tests/ui/atomic_ordering_fetch_update.stderr +++ /dev/null @@ -1,131 +0,0 @@ -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:21:47 - | -LL | let _ = x.fetch_update(Ordering::Relaxed, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::invalid-atomic-ordering` implied by `-D warnings` - = help: consider using ordering mode `Relaxed` instead - -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:22:47 - | -LL | let _ = x.fetch_update(Ordering::Acquire, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:23:47 - | -LL | let _ = x.fetch_update(Ordering::Release, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:24:46 - | -LL | let _ = x.fetch_update(Ordering::AcqRel, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:25:46 - | -LL | let _ = x.fetch_update(Ordering::SeqCst, Ordering::AcqRel, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` instead - -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:28:47 - | -LL | let _ = x.fetch_update(Ordering::Relaxed, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:29:47 - | -LL | let _ = x.fetch_update(Ordering::Acquire, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:30:47 - | -LL | let _ = x.fetch_update(Ordering::Release, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:31:46 - | -LL | let _ = x.fetch_update(Ordering::AcqRel, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: fetch_update's failure ordering may not be `Release` or `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:32:46 - | -LL | let _ = x.fetch_update(Ordering::SeqCst, Ordering::Release, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` instead - -error: fetch_update's failure ordering may not be stronger than the success ordering of `Release` - --> $DIR/atomic_ordering_fetch_update.rs:35:47 - | -LL | let _ = x.fetch_update(Ordering::Release, Ordering::Acquire, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: fetch_update's failure ordering may not be stronger than the success ordering of `Release` - --> $DIR/atomic_ordering_fetch_update.rs:36:47 - | -LL | let _ = x.fetch_update(Ordering::Release, Ordering::SeqCst, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: fetch_update's failure ordering may not be stronger than the success ordering of `Relaxed` - --> $DIR/atomic_ordering_fetch_update.rs:39:47 - | -LL | let _ = x.fetch_update(Ordering::Relaxed, Ordering::SeqCst, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: fetch_update's failure ordering may not be stronger than the success ordering of `Relaxed` - --> $DIR/atomic_ordering_fetch_update.rs:40:47 - | -LL | let _ = x.fetch_update(Ordering::Relaxed, Ordering::Acquire, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering mode `Relaxed` instead - -error: fetch_update's failure ordering may not be stronger than the success ordering of `Acquire` - --> $DIR/atomic_ordering_fetch_update.rs:43:47 - | -LL | let _ = x.fetch_update(Ordering::Acquire, Ordering::SeqCst, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: fetch_update's failure ordering may not be stronger than the success ordering of `AcqRel` - --> $DIR/atomic_ordering_fetch_update.rs:44:46 - | -LL | let _ = x.fetch_update(Ordering::AcqRel, Ordering::SeqCst, |old| Some(old + 1)); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire` or `Relaxed` instead - -error: aborting due to 16 previous errors - diff --git a/tests/ui/atomic_ordering_int.rs b/tests/ui/atomic_ordering_int.rs deleted file mode 100644 index 40a00ba3de35..000000000000 --- a/tests/ui/atomic_ordering_int.rs +++ /dev/null @@ -1,86 +0,0 @@ -#![warn(clippy::invalid_atomic_ordering)] - -use std::sync::atomic::{AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, Ordering}; - -fn main() { - // `AtomicI8` test cases - let x = AtomicI8::new(0); - - // Allowed load ordering modes - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - - // Disallowed load ordering modes - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - // Allowed store ordering modes - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - - // Disallowed store ordering modes - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); - - // `AtomicI16` test cases - let x = AtomicI16::new(0); - - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); - - // `AtomicI32` test cases - let x = AtomicI32::new(0); - - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); - - // `AtomicI64` test cases - let x = AtomicI64::new(0); - - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); - - // `AtomicIsize` test cases - let x = AtomicIsize::new(0); - - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); -} diff --git a/tests/ui/atomic_ordering_int.stderr b/tests/ui/atomic_ordering_int.stderr deleted file mode 100644 index bbaf234d3c9f..000000000000 --- a/tests/ui/atomic_ordering_int.stderr +++ /dev/null @@ -1,163 +0,0 @@ -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:15:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::invalid-atomic-ordering` implied by `-D warnings` - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:16:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:24:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:25:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:33:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:34:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:39:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:40:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:48:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:49:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:54:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:55:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:63:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:64:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:69:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:70:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:78:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:79:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:84:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_int.rs:85:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: aborting due to 20 previous errors - diff --git a/tests/ui/atomic_ordering_ptr.rs b/tests/ui/atomic_ordering_ptr.rs deleted file mode 100644 index ecbb05c7fbc3..000000000000 --- a/tests/ui/atomic_ordering_ptr.rs +++ /dev/null @@ -1,27 +0,0 @@ -#![warn(clippy::invalid_atomic_ordering)] - -use std::sync::atomic::{AtomicPtr, Ordering}; - -fn main() { - let ptr = &mut 5; - let other_ptr = &mut 10; - let x = AtomicPtr::new(ptr); - - // Allowed load ordering modes - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - - // Disallowed load ordering modes - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - // Allowed store ordering modes - x.store(other_ptr, Ordering::Release); - x.store(other_ptr, Ordering::SeqCst); - x.store(other_ptr, Ordering::Relaxed); - - // Disallowed store ordering modes - x.store(other_ptr, Ordering::Acquire); - x.store(other_ptr, Ordering::AcqRel); -} diff --git a/tests/ui/atomic_ordering_ptr.stderr b/tests/ui/atomic_ordering_ptr.stderr deleted file mode 100644 index 558ae55518d5..000000000000 --- a/tests/ui/atomic_ordering_ptr.stderr +++ /dev/null @@ -1,35 +0,0 @@ -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_ptr.rs:16:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::invalid-atomic-ordering` implied by `-D warnings` - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_ptr.rs:17:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_ptr.rs:25:24 - | -LL | x.store(other_ptr, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_ptr.rs:26:24 - | -LL | x.store(other_ptr, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: aborting due to 4 previous errors - diff --git a/tests/ui/atomic_ordering_uint.rs b/tests/ui/atomic_ordering_uint.rs deleted file mode 100644 index a0d5d7c40103..000000000000 --- a/tests/ui/atomic_ordering_uint.rs +++ /dev/null @@ -1,86 +0,0 @@ -#![warn(clippy::invalid_atomic_ordering)] - -use std::sync::atomic::{AtomicU16, AtomicU32, AtomicU64, AtomicU8, AtomicUsize, Ordering}; - -fn main() { - // `AtomicU8` test cases - let x = AtomicU8::new(0); - - // Allowed load ordering modes - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - - // Disallowed load ordering modes - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - // Allowed store ordering modes - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - - // Disallowed store ordering modes - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); - - // `AtomicU16` test cases - let x = AtomicU16::new(0); - - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); - - // `AtomicU32` test cases - let x = AtomicU32::new(0); - - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); - - // `AtomicU64` test cases - let x = AtomicU64::new(0); - - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); - - // `AtomicUsize` test cases - let x = AtomicUsize::new(0); - - let _ = x.load(Ordering::Acquire); - let _ = x.load(Ordering::SeqCst); - let _ = x.load(Ordering::Relaxed); - let _ = x.load(Ordering::Release); - let _ = x.load(Ordering::AcqRel); - - x.store(1, Ordering::Release); - x.store(1, Ordering::SeqCst); - x.store(1, Ordering::Relaxed); - x.store(1, Ordering::Acquire); - x.store(1, Ordering::AcqRel); -} diff --git a/tests/ui/atomic_ordering_uint.stderr b/tests/ui/atomic_ordering_uint.stderr deleted file mode 100644 index 5703135bcf1e..000000000000 --- a/tests/ui/atomic_ordering_uint.stderr +++ /dev/null @@ -1,163 +0,0 @@ -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:15:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = note: `-D clippy::invalid-atomic-ordering` implied by `-D warnings` - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:16:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:24:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:25:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:33:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:34:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:39:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:40:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:48:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:49:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:54:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:55:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:63:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:64:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:69:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:70:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:78:20 - | -LL | let _ = x.load(Ordering::Release); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic loads cannot have `Release` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:79:20 - | -LL | let _ = x.load(Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Acquire`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:84:16 - | -LL | x.store(1, Ordering::Acquire); - | ^^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: atomic stores cannot have `Acquire` and `AcqRel` ordering - --> $DIR/atomic_ordering_uint.rs:85:16 - | -LL | x.store(1, Ordering::AcqRel); - | ^^^^^^^^^^^^^^^^ - | - = help: consider using ordering modes `Release`, `SeqCst` or `Relaxed` - -error: aborting due to 20 previous errors - diff --git a/tests/ui/author/if.stdout b/tests/ui/author/if.stdout index 502b38545b8e..1653de9a6f26 100644 --- a/tests/ui/author/if.stdout +++ b/tests/ui/author/if.stdout @@ -12,7 +12,8 @@ if_chain! { if let ExprKind::Lit(ref lit1) = right.kind; if let LitKind::Int(2, _) = lit1.node; if block.expr.is_none(); - if let ExprKind::Lit(ref lit2) = cond.kind; + if let ExprKind::DropTemps(ref expr) = cond.kind; + if let ExprKind::Lit(ref lit2) = expr.kind; if let LitKind::Bool(true) = lit2.node; if let ExprKind::Block(ref block1) = then.kind; if block1.stmts.len() == 1; diff --git a/tests/ui/collapsible_match.rs b/tests/ui/collapsible_match.rs index 55467cf4229d..4ce365cc7649 100644 --- a/tests/ui/collapsible_match.rs +++ b/tests/ui/collapsible_match.rs @@ -98,6 +98,11 @@ fn lint_cases(opt_opt: Option>, res_opt: Result, String> } fn negative_cases(res_opt: Result, String>, res_res: Result, String>) { + while let Some(x) = make() { + if let Some(1) = x { + todo!(); + } + } // no wild pattern in outer match match res_opt { Ok(val) => match val { diff --git a/tests/ui/collapsible_match.stderr b/tests/ui/collapsible_match.stderr index 779788849008..c119570e8abd 100644 --- a/tests/ui/collapsible_match.stderr +++ b/tests/ui/collapsible_match.stderr @@ -1,4 +1,4 @@ -error: unnecessary nested match +error: this `match` can be collapsed into the outer `match` --> $DIR/collapsible_match.rs:7:20 | LL | Ok(val) => match val { @@ -17,7 +17,7 @@ LL | Ok(val) => match val { LL | Some(n) => foo(n), | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `match` can be collapsed into the outer `match` --> $DIR/collapsible_match.rs:16:20 | LL | Ok(val) => match val { @@ -35,7 +35,7 @@ LL | Ok(val) => match val { LL | Some(n) => foo(n), | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `if let` can be collapsed into the outer `if let` --> $DIR/collapsible_match.rs:25:9 | LL | / if let Some(n) = val { @@ -51,7 +51,7 @@ LL | if let Ok(val) = res_opt { LL | if let Some(n) = val { | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `if let` can be collapsed into the outer `if let` --> $DIR/collapsible_match.rs:32:9 | LL | / if let Some(n) = val { @@ -69,7 +69,7 @@ LL | if let Ok(val) = res_opt { LL | if let Some(n) = val { | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `match` can be collapsed into the outer `if let` --> $DIR/collapsible_match.rs:43:9 | LL | / match val { @@ -87,7 +87,7 @@ LL | match val { LL | Some(n) => foo(n), | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `if let` can be collapsed into the outer `match` --> $DIR/collapsible_match.rs:52:13 | LL | / if let Some(n) = val { @@ -103,7 +103,7 @@ LL | Ok(val) => { LL | if let Some(n) = val { | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `match` can be collapsed into the outer `if let` --> $DIR/collapsible_match.rs:61:9 | LL | / match val { @@ -121,7 +121,7 @@ LL | match val { LL | Some(n) => foo(n), | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `if let` can be collapsed into the outer `match` --> $DIR/collapsible_match.rs:72:13 | LL | / if let Some(n) = val { @@ -139,7 +139,7 @@ LL | Ok(val) => { LL | if let Some(n) = val { | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `match` can be collapsed into the outer `match` --> $DIR/collapsible_match.rs:83:20 | LL | Ok(val) => match val { @@ -157,7 +157,7 @@ LL | Ok(val) => match val { LL | Some(n) => foo(n), | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `match` can be collapsed into the outer `match` --> $DIR/collapsible_match.rs:92:22 | LL | Some(val) => match val { diff --git a/tests/ui/collapsible_match2.stderr b/tests/ui/collapsible_match2.stderr index 8975b2efbaee..55e70dce208a 100644 --- a/tests/ui/collapsible_match2.stderr +++ b/tests/ui/collapsible_match2.stderr @@ -1,4 +1,4 @@ -error: unnecessary nested match +error: this `match` can be collapsed into the outer `match` --> $DIR/collapsible_match2.rs:13:34 | LL | Ok(val) if make() => match val { @@ -17,7 +17,7 @@ LL | Ok(val) if make() => match val { LL | Some(n) => foo(n), | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `match` can be collapsed into the outer `match` --> $DIR/collapsible_match2.rs:20:24 | LL | Ok(val) => match val { @@ -35,7 +35,7 @@ LL | Ok(val) => match val { LL | Some(n) => foo(n), | ^^^^^^^ with this pattern -error: unnecessary nested match +error: this `match` can be collapsed into the outer `match` --> $DIR/collapsible_match2.rs:34:29 | LL | $pat => match $e { @@ -57,7 +57,7 @@ LL | mac!(res_opt => Ok(val), val => Some(n), foo(n)); | replace this binding = note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info) -error: unnecessary nested match +error: this `match` can be collapsed into the outer `match` --> $DIR/collapsible_match2.rs:51:20 | LL | Some(s) => match *s { @@ -75,7 +75,7 @@ LL | Some(s) => match *s { LL | [n] => foo(n), | ^^^ with this pattern -error: unnecessary nested match +error: this `match` can be collapsed into the outer `match` --> $DIR/collapsible_match2.rs:60:24 | LL | Some(ref s) => match &*s { diff --git a/tests/ui/crashes/ice-3969.stderr b/tests/ui/crashes/ice-3969.stderr index 8b2c318acf84..9a89047f0727 100644 --- a/tests/ui/crashes/ice-3969.stderr +++ b/tests/ui/crashes/ice-3969.stderr @@ -6,7 +6,7 @@ LL | for<'a> Dst: Sized, | = note: `-D bare-trait-objects` implied by `-D warnings` = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! - = note: for more information, see issue #80165 + = note: for more information, see error: trait objects without an explicit `dyn` are deprecated --> $DIR/ice-3969.rs:27:16 @@ -15,7 +15,7 @@ LL | let x: Dst = *(Box::new(Dst { x: 1 }) as Box>); | ^ help: use `dyn`: `dyn A` | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! - = note: for more information, see issue #80165 + = note: for more information, see error: trait objects without an explicit `dyn` are deprecated --> $DIR/ice-3969.rs:27:57 @@ -24,7 +24,7 @@ LL | let x: Dst = *(Box::new(Dst { x: 1 }) as Box>); | ^ help: use `dyn`: `dyn A` | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! - = note: for more information, see issue #80165 + = note: for more information, see error: aborting due to 3 previous errors diff --git a/tests/ui/crashes/ice-4775.rs b/tests/ui/crashes/ice-4775.rs index 31e53e846d54..405e3039e7d0 100644 --- a/tests/ui/crashes/ice-4775.rs +++ b/tests/ui/crashes/ice-4775.rs @@ -1,6 +1,3 @@ -#![feature(const_generics)] -#![allow(incomplete_features)] - pub struct ArrayWrapper([usize; N]); impl ArrayWrapper<{ N }> { diff --git a/tests/ui/crashes/ice-5223.rs b/tests/ui/crashes/ice-5223.rs index 9bb2e227fc12..e3b3b27a6fc3 100644 --- a/tests/ui/crashes/ice-5223.rs +++ b/tests/ui/crashes/ice-5223.rs @@ -1,7 +1,4 @@ // Regression test for #5233 - -#![feature(const_generics)] -#![allow(incomplete_features)] #![warn(clippy::indexing_slicing, clippy::iter_cloned_collect)] pub struct KotomineArray { diff --git a/tests/ui/crashes/ice-7410.rs b/tests/ui/crashes/ice-7410.rs index aaa422d88c3e..85fa42103219 100644 --- a/tests/ui/crashes/ice-7410.rs +++ b/tests/ui/crashes/ice-7410.rs @@ -4,6 +4,7 @@ #![feature(lang_items, start, libc)] #![no_std] +#![allow(clippy::if_same_then_else)] #![allow(clippy::redundant_pattern_matching)] use core::panic::PanicInfo; diff --git a/tests/ui/crashes/issues_loop_mut_cond.rs b/tests/ui/crashes/issues_loop_mut_cond.rs index bb238c81ebc0..553c840f9b08 100644 --- a/tests/ui/crashes/issues_loop_mut_cond.rs +++ b/tests/ui/crashes/issues_loop_mut_cond.rs @@ -1,3 +1,4 @@ +#![allow(clippy::blocks_in_if_conditions)] #![allow(dead_code)] /// Issue: https://github.com/rust-lang/rust-clippy/issues/2596 diff --git a/tests/ui/declare_interior_mutable_const/traits.rs b/tests/ui/declare_interior_mutable_const/traits.rs index 535147ccc645..256a336db821 100644 --- a/tests/ui/declare_interior_mutable_const/traits.rs +++ b/tests/ui/declare_interior_mutable_const/traits.rs @@ -117,7 +117,7 @@ impl SelfType for AtomicUsize { const WRAPPED_SELF: Option = Some(AtomicUsize::new(21)); //~ ERROR interior mutable } -// Even though a constant contains a generic type, if it also have a interior mutable type, +// Even though a constant contains a generic type, if it also have an interior mutable type, // it should be linted at the definition site. trait BothOfCellAndGeneric { // this is a false negative in the current implementation. diff --git a/tests/ui/deprecated.rs b/tests/ui/deprecated.rs index 4ba9f0c1fcff..1943d0092e62 100644 --- a/tests/ui/deprecated.rs +++ b/tests/ui/deprecated.rs @@ -14,5 +14,6 @@ #[warn(clippy::filter_map)] #[warn(clippy::pub_enum_variant_names)] #[warn(clippy::wrong_pub_self_convention)] +#[warn(clippy::invalid_atomic_ordering)] fn main() {} diff --git a/tests/ui/deprecated.stderr b/tests/ui/deprecated.stderr index c0002e535431..51048e45c067 100644 --- a/tests/ui/deprecated.stderr +++ b/tests/ui/deprecated.stderr @@ -96,5 +96,11 @@ error: lint `clippy::wrong_pub_self_convention` has been removed: set the `avoid LL | #[warn(clippy::wrong_pub_self_convention)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 16 previous errors +error: lint `clippy::invalid_atomic_ordering` has been renamed to `invalid_atomic_ordering` + --> $DIR/deprecated.rs:17:8 + | +LL | #[warn(clippy::invalid_atomic_ordering)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_atomic_ordering` + +error: aborting due to 17 previous errors diff --git a/tests/ui/doc/doc.rs b/tests/ui/doc/doc.rs index 8afef6b23d47..8b0c0f304fce 100644 --- a/tests/ui/doc/doc.rs +++ b/tests/ui/doc/doc.rs @@ -2,7 +2,7 @@ #![allow(dead_code, incomplete_features)] #![warn(clippy::doc_markdown)] -#![feature(custom_inner_attributes, const_generics, const_evaluatable_checked, const_option)] +#![feature(custom_inner_attributes, generic_const_exprs, const_option)] #![rustfmt::skip] /// The foo_bar function does _nothing_. See also foo::bar. (note the dot there) @@ -203,7 +203,7 @@ fn issue_2343() {} /// __|_ _|__||_| fn pulldown_cmark_crash() {} -// issue #7033 - const_evaluatable_checked ICE +// issue #7033 - generic_const_exprs ICE struct S where [(); N.checked_next_power_of_two().unwrap()]: { arr: [T; N.checked_next_power_of_two().unwrap()], diff --git a/tests/ui/if_let_some_result.fixed b/tests/ui/if_let_some_result.fixed index 62a25ce2d128..1bddc47721e5 100644 --- a/tests/ui/if_let_some_result.fixed +++ b/tests/ui/if_let_some_result.fixed @@ -1,6 +1,7 @@ // run-rustfix #![warn(clippy::if_let_some_result)] +#![allow(dead_code)] fn str_to_int(x: &str) -> i32 { if let Ok(y) = x.parse() { y } else { 0 } @@ -20,8 +21,8 @@ fn strange_some_no_else(x: &str) -> i32 { } } -fn main() { - let _ = str_to_int("1"); - let _ = str_to_int_ok("2"); - let _ = strange_some_no_else("3"); +fn negative() { + while let Some(1) = "".parse().ok() {} } + +fn main() {} diff --git a/tests/ui/if_let_some_result.rs b/tests/ui/if_let_some_result.rs index 234ff5e9e80e..d4a52ec9881d 100644 --- a/tests/ui/if_let_some_result.rs +++ b/tests/ui/if_let_some_result.rs @@ -1,6 +1,7 @@ // run-rustfix #![warn(clippy::if_let_some_result)] +#![allow(dead_code)] fn str_to_int(x: &str) -> i32 { if let Some(y) = x.parse().ok() { y } else { 0 } @@ -20,8 +21,8 @@ fn strange_some_no_else(x: &str) -> i32 { } } -fn main() { - let _ = str_to_int("1"); - let _ = str_to_int_ok("2"); - let _ = strange_some_no_else("3"); +fn negative() { + while let Some(1) = "".parse().ok() {} } + +fn main() {} diff --git a/tests/ui/if_let_some_result.stderr b/tests/ui/if_let_some_result.stderr index 134ce9d24116..bc3a5e7698d7 100644 --- a/tests/ui/if_let_some_result.stderr +++ b/tests/ui/if_let_some_result.stderr @@ -1,5 +1,5 @@ error: matching on `Some` with `ok()` is redundant - --> $DIR/if_let_some_result.rs:6:5 + --> $DIR/if_let_some_result.rs:7:5 | LL | if let Some(y) = x.parse().ok() { y } else { 0 } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -11,7 +11,7 @@ LL | if let Ok(y) = x.parse() { y } else { 0 } | ~~~~~~~~~~~~~~~~~~~~~~~~ error: matching on `Some` with `ok()` is redundant - --> $DIR/if_let_some_result.rs:16:9 + --> $DIR/if_let_some_result.rs:17:9 | LL | if let Some(y) = x . parse() . ok () { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/infinite_loop.rs b/tests/ui/infinite_loop.rs index 3d8fb8507e51..e518b2677b7b 100644 --- a/tests/ui/infinite_loop.rs +++ b/tests/ui/infinite_loop.rs @@ -1,3 +1,5 @@ +#![allow(clippy::blocks_in_if_conditions)] + fn fn_val(i: i32) -> i32 { unimplemented!() } diff --git a/tests/ui/infinite_loop.stderr b/tests/ui/infinite_loop.stderr index 1fcb29eff18e..2736753c14b6 100644 --- a/tests/ui/infinite_loop.stderr +++ b/tests/ui/infinite_loop.stderr @@ -1,5 +1,5 @@ error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:21:11 + --> $DIR/infinite_loop.rs:23:11 | LL | while y < 10 { | ^^^^^^ @@ -8,7 +8,7 @@ LL | while y < 10 { = note: this may lead to an infinite or to a never running loop error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:26:11 + --> $DIR/infinite_loop.rs:28:11 | LL | while y < 10 && x < 3 { | ^^^^^^^^^^^^^^^ @@ -16,7 +16,7 @@ LL | while y < 10 && x < 3 { = note: this may lead to an infinite or to a never running loop error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:33:11 + --> $DIR/infinite_loop.rs:35:11 | LL | while !cond { | ^^^^^ @@ -24,7 +24,7 @@ LL | while !cond { = note: this may lead to an infinite or to a never running loop error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:77:11 + --> $DIR/infinite_loop.rs:79:11 | LL | while i < 3 { | ^^^^^ @@ -32,7 +32,7 @@ LL | while i < 3 { = note: this may lead to an infinite or to a never running loop error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:82:11 + --> $DIR/infinite_loop.rs:84:11 | LL | while i < 3 && j > 0 { | ^^^^^^^^^^^^^^ @@ -40,7 +40,7 @@ LL | while i < 3 && j > 0 { = note: this may lead to an infinite or to a never running loop error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:86:11 + --> $DIR/infinite_loop.rs:88:11 | LL | while i < 3 { | ^^^^^ @@ -48,7 +48,7 @@ LL | while i < 3 { = note: this may lead to an infinite or to a never running loop error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:101:11 + --> $DIR/infinite_loop.rs:103:11 | LL | while i < 3 { | ^^^^^ @@ -56,7 +56,7 @@ LL | while i < 3 { = note: this may lead to an infinite or to a never running loop error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:106:11 + --> $DIR/infinite_loop.rs:108:11 | LL | while i < 3 { | ^^^^^ @@ -64,7 +64,7 @@ LL | while i < 3 { = note: this may lead to an infinite or to a never running loop error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:172:15 + --> $DIR/infinite_loop.rs:174:15 | LL | while self.count < n { | ^^^^^^^^^^^^^^ @@ -72,7 +72,7 @@ LL | while self.count < n { = note: this may lead to an infinite or to a never running loop error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:180:11 + --> $DIR/infinite_loop.rs:182:11 | LL | while y < 10 { | ^^^^^^ @@ -82,7 +82,7 @@ LL | while y < 10 { = help: rewrite it as `if cond { loop { } }` error: variables in the condition are not mutated in the loop body - --> $DIR/infinite_loop.rs:187:11 + --> $DIR/infinite_loop.rs:189:11 | LL | while y < 10 { | ^^^^^^ diff --git a/tests/ui/match_overlapping_arm.rs b/tests/ui/match_overlapping_arm.rs index 44c51e8112a7..c84e31ea482a 100644 --- a/tests/ui/match_overlapping_arm.rs +++ b/tests/ui/match_overlapping_arm.rs @@ -2,6 +2,7 @@ #![feature(half_open_range_patterns)] #![warn(clippy::match_overlapping_arm)] #![allow(clippy::redundant_pattern_matching)] +#![allow(clippy::if_same_then_else)] /// Tests for match_overlapping_arm diff --git a/tests/ui/match_overlapping_arm.stderr b/tests/ui/match_overlapping_arm.stderr index f25a66d634e8..359fa49f51be 100644 --- a/tests/ui/match_overlapping_arm.stderr +++ b/tests/ui/match_overlapping_arm.stderr @@ -1,60 +1,60 @@ error: some ranges overlap - --> $DIR/match_overlapping_arm.rs:12:9 + --> $DIR/match_overlapping_arm.rs:13:9 | LL | 0..=10 => println!("0 ... 10"), | ^^^^^^ | = note: `-D clippy::match-overlapping-arm` implied by `-D warnings` note: overlaps with this - --> $DIR/match_overlapping_arm.rs:13:9 + --> $DIR/match_overlapping_arm.rs:14:9 | LL | 0..=11 => println!("0 ... 11"), | ^^^^^^ error: some ranges overlap - --> $DIR/match_overlapping_arm.rs:18:9 + --> $DIR/match_overlapping_arm.rs:19:9 | LL | 0..=5 => println!("0 ... 5"), | ^^^^^ | note: overlaps with this - --> $DIR/match_overlapping_arm.rs:20:9 + --> $DIR/match_overlapping_arm.rs:21:9 | LL | FOO..=11 => println!("0 ... 11"), | ^^^^^^^^ error: some ranges overlap - --> $DIR/match_overlapping_arm.rs:55:9 + --> $DIR/match_overlapping_arm.rs:56:9 | LL | 0..11 => println!("0 .. 11"), | ^^^^^ | note: overlaps with this - --> $DIR/match_overlapping_arm.rs:56:9 + --> $DIR/match_overlapping_arm.rs:57:9 | LL | 0..=11 => println!("0 ... 11"), | ^^^^^^ error: some ranges overlap - --> $DIR/match_overlapping_arm.rs:80:9 + --> $DIR/match_overlapping_arm.rs:81:9 | LL | 0..=10 => println!("0 ... 10"), | ^^^^^^ | note: overlaps with this - --> $DIR/match_overlapping_arm.rs:79:9 + --> $DIR/match_overlapping_arm.rs:80:9 | LL | 5..14 => println!("5 .. 14"), | ^^^^^ error: some ranges overlap - --> $DIR/match_overlapping_arm.rs:85:9 + --> $DIR/match_overlapping_arm.rs:86:9 | LL | 0..7 => println!("0 .. 7"), | ^^^^ | note: overlaps with this - --> $DIR/match_overlapping_arm.rs:86:9 + --> $DIR/match_overlapping_arm.rs:87:9 | LL | 0..=10 => println!("0 ... 10"), | ^^^^^^ diff --git a/tests/ui/missing_const_for_fn/cant_be_const.rs b/tests/ui/missing_const_for_fn/cant_be_const.rs index 6d2cbb6ad96f..aa60d0504e5e 100644 --- a/tests/ui/missing_const_for_fn/cant_be_const.rs +++ b/tests/ui/missing_const_for_fn/cant_be_const.rs @@ -5,8 +5,7 @@ // aux-build:helper.rs #![warn(clippy::missing_const_for_fn)] -#![allow(incomplete_features)] -#![feature(start, const_generics)] +#![feature(start)] #![feature(custom_inner_attributes)] extern crate helper; diff --git a/tests/ui/missing_const_for_fn/could_be_const.rs b/tests/ui/missing_const_for_fn/could_be_const.rs index 0accb516f5f6..baa7eec05462 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.rs +++ b/tests/ui/missing_const_for_fn/could_be_const.rs @@ -1,6 +1,5 @@ #![warn(clippy::missing_const_for_fn)] #![allow(incomplete_features, clippy::let_and_return)] -#![feature(const_generics)] #![feature(custom_inner_attributes)] use std::mem::transmute; diff --git a/tests/ui/missing_const_for_fn/could_be_const.stderr b/tests/ui/missing_const_for_fn/could_be_const.stderr index 63c211f39fa1..b89cc6451bb5 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.stderr +++ b/tests/ui/missing_const_for_fn/could_be_const.stderr @@ -1,5 +1,5 @@ error: this could be a `const fn` - --> $DIR/could_be_const.rs:14:5 + --> $DIR/could_be_const.rs:13:5 | LL | / pub fn new() -> Self { LL | | Self { guess: 42 } @@ -9,7 +9,7 @@ LL | | } = note: `-D clippy::missing-const-for-fn` implied by `-D warnings` error: this could be a `const fn` - --> $DIR/could_be_const.rs:18:5 + --> $DIR/could_be_const.rs:17:5 | LL | / fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] { LL | | b @@ -17,7 +17,7 @@ LL | | } | |_____^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:24:1 + --> $DIR/could_be_const.rs:23:1 | LL | / fn one() -> i32 { LL | | 1 @@ -25,7 +25,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:29:1 + --> $DIR/could_be_const.rs:28:1 | LL | / fn two() -> i32 { LL | | let abc = 2; @@ -34,7 +34,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:35:1 + --> $DIR/could_be_const.rs:34:1 | LL | / fn string() -> String { LL | | String::new() @@ -42,7 +42,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:40:1 + --> $DIR/could_be_const.rs:39:1 | LL | / unsafe fn four() -> i32 { LL | | 4 @@ -50,7 +50,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:45:1 + --> $DIR/could_be_const.rs:44:1 | LL | / fn generic(t: T) -> T { LL | | t @@ -58,7 +58,7 @@ LL | | } | |_^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:68:9 + --> $DIR/could_be_const.rs:67:9 | LL | / pub fn b(self, a: &A) -> B { LL | | B @@ -66,7 +66,7 @@ LL | | } | |_________^ error: this could be a `const fn` - --> $DIR/could_be_const.rs:78:5 + --> $DIR/could_be_const.rs:77:5 | LL | / fn const_fn_stabilized_before_msrv(byte: u8) { LL | | byte.is_ascii_digit();