|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::higher::If; |
| 3 | +use clippy_utils::source::{SpanExt as _, walk_span_to_context}; |
| 4 | +use clippy_utils::sugg::Sugg; |
| 5 | +use clippy_utils::{is_else_clause, is_from_proc_macro, peel_blocks}; |
| 6 | +use rustc_ast::LitKind; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{Expr, ExprKind}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass, LintContext as _}; |
| 10 | +use rustc_session::declare_lint_pass; |
| 11 | +use rustc_span::Span; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// |
| 16 | + /// Warn about cases where `x && y` could be used in place of an if condition. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// |
| 20 | + /// `x && y` is more standard as a construction, and makes it clearer that this is just an and. |
| 21 | + /// It is also less verbose. |
| 22 | + /// |
| 23 | + /// ### Example |
| 24 | + /// ```no_run |
| 25 | + /// # fn a() -> bool { false } |
| 26 | + /// fn b(b1: bool) -> bool { |
| 27 | + /// if b1 { a() } else { false} |
| 28 | + /// } |
| 29 | + /// |
| 30 | + /// ``` |
| 31 | + /// |
| 32 | + /// Could be written |
| 33 | + /// |
| 34 | + /// ```no_run |
| 35 | + /// # fn a() -> bool { false } |
| 36 | + /// fn b(b1: bool) -> bool { |
| 37 | + /// b1 && a() |
| 38 | + /// } |
| 39 | + /// ``` |
| 40 | + #[clippy::version = "1.99.0"] |
| 41 | + pub IFS_AS_LOGICAL_OPS, |
| 42 | + pedantic, |
| 43 | + "`if` conditions that can be rewritten as logical operators" |
| 44 | +} |
| 45 | + |
| 46 | +declare_lint_pass!(IfsAsLogicalOps => [IFS_AS_LOGICAL_OPS]); |
| 47 | + |
| 48 | +impl<'tcx> LateLintPass<'tcx> for IfsAsLogicalOps { |
| 49 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) { |
| 50 | + // Make sure the if block is not an if-let block. |
| 51 | + if let Some(If {cond: if_cond, then, r#else: Some(els)}) = If::hir(e) |
| 52 | + && let ExprKind::Block(then_block, _label) = then.kind |
| 53 | + // Check if the if-block has only a trailing expression |
| 54 | + && then_block.stmts.is_empty() |
| 55 | + && let Some(then_block_inner_expr) = then_block.expr |
| 56 | + // And that the else block consists of only the boolean 'false'. |
| 57 | + && let ExprKind::Block(else_block, _label) = els.kind |
| 58 | + && else_block.stmts.is_empty() |
| 59 | + && let Some(else_block_inner_expr) = else_block.expr |
| 60 | + && let ExprKind::Lit(lit) = else_block_inner_expr.kind |
| 61 | + && matches!(lit.node, LitKind::Bool(false)) |
| 62 | + // We do not emit this lint if the expression diverges. |
| 63 | + && !cx.typeck_results().expr_ty(then_block_inner_expr).is_never() |
| 64 | + // Make sure that the expression is only in a single macro context |
| 65 | + && let ctxt = e.span.ctxt() |
| 66 | + && ctxt == then_block.span.ctxt() |
| 67 | + && ctxt == else_block.span.ctxt() |
| 68 | + && ctxt == else_block_inner_expr.span.ctxt() |
| 69 | + && ctxt == lit.span.ctxt() |
| 70 | + && !ctxt.in_external_macro(cx.sess().source_map()) |
| 71 | + && !is_from_proc_macro(cx,e) |
| 72 | + { |
| 73 | + // Do not lint if the statement is trivially a boolean. |
| 74 | + if let ExprKind::Lit(lit_ptr) = peel_blocks(then_block_inner_expr).kind |
| 75 | + && let LitKind::Bool(_) = lit_ptr.node |
| 76 | + { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if let Some(walked_then_block_inner) = walk_span_to_context(then_block_inner_expr.span, ctxt) |
| 81 | + && check_that_nested_spans_have_no_comments(then_block.span, walked_then_block_inner, cx) |
| 82 | + && check_that_nested_spans_have_no_comments(else_block.span, else_block_inner_expr.span, cx) |
| 83 | + { |
| 84 | + let mut applicability = if ctxt.is_root() { |
| 85 | + Applicability::MachineApplicable |
| 86 | + } else { |
| 87 | + Applicability::MaybeIncorrect |
| 88 | + }; |
| 89 | + |
| 90 | + let mut sugg = Sugg::hir_with_context(cx, if_cond, ctxt, "_", &mut applicability); |
| 91 | + let rhs_sugg = Sugg::hir_with_context(cx, then_block_inner_expr, ctxt, "_", &mut applicability); |
| 92 | + |
| 93 | + sugg = sugg.and(&rhs_sugg); |
| 94 | + |
| 95 | + if is_else_clause(cx.tcx, e) { |
| 96 | + sugg = sugg.blockify(); |
| 97 | + } |
| 98 | + |
| 99 | + span_lint_and_sugg( |
| 100 | + cx, |
| 101 | + IFS_AS_LOGICAL_OPS, |
| 102 | + e.span, |
| 103 | + "if expression that could be written as a logical and expression", |
| 104 | + "try", |
| 105 | + sugg.to_string(), |
| 106 | + applicability, |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +fn check_that_nested_spans_have_no_comments(outer_span: Span, inner_span: Span, cx: &LateContext<'_>) -> bool { |
| 114 | + (outer_span.lo()..inner_span.lo()).check_text(cx, |src| src.trim_end() == "{") |
| 115 | + && (inner_span.hi()..outer_span.hi()).check_text(cx, |src| src.trim_start() == "}") |
| 116 | +} |
0 commit comments