]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/operators/needless_bitwise_bool.rs
Rollup merge of #103525 - oli-obk:const_impl_on_non_const_trait, r=lcnr
[rust.git] / src / tools / clippy / clippy_lints / src / operators / needless_bitwise_bool.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet_opt;
3 use rustc_errors::Applicability;
4 use rustc_hir::{BinOpKind, Expr, ExprKind};
5 use rustc_lint::LateContext;
6
7 use super::NEEDLESS_BITWISE_BOOL;
8
9 pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, op: BinOpKind, lhs: &Expr<'_>, rhs: &Expr<'_>) {
10     let op_str = match op {
11         BinOpKind::BitAnd => "&&",
12         BinOpKind::BitOr => "||",
13         _ => return,
14     };
15     if matches!(
16         rhs.kind,
17         ExprKind::Call(..) | ExprKind::MethodCall(..) | ExprKind::Binary(..) | ExprKind::Unary(..)
18     ) && cx.typeck_results().expr_ty(e).is_bool()
19         && !rhs.can_have_side_effects()
20     {
21         span_lint_and_then(
22             cx,
23             NEEDLESS_BITWISE_BOOL,
24             e.span,
25             "use of bitwise operator instead of lazy operator between booleans",
26             |diag| {
27                 if let Some(lhs_snip) = snippet_opt(cx, lhs.span)
28                     && let Some(rhs_snip) = snippet_opt(cx, rhs.span)
29                 {
30                     let sugg = format!("{lhs_snip} {op_str} {rhs_snip}");
31                     diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable);
32                 }
33             },
34         );
35     }
36 }