summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/unit_types/unit_cmp.rs
blob: d4342ec5169a2fe49f7bc758d6f6c25ba28b4e53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use clippy_utils::diagnostics::span_lint;
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::LateContext;

use super::UNIT_CMP;

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
    if expr.span.from_expansion() {
        if let Some(macro_call) = root_macro_call_first_node(cx, expr) {
            let macro_name = cx.tcx.item_name(macro_call.def_id);
            let result = match macro_name.as_str() {
                "assert_eq" | "debug_assert_eq" => "succeed",
                "assert_ne" | "debug_assert_ne" => "fail",
                _ => return,
            };
            let Some((left, _, _)) = find_assert_eq_args(cx, expr, macro_call.expn) else {
                return;
            };
            if !cx.typeck_results().expr_ty(left).is_unit() {
                return;
            }
            span_lint(
                cx,
                UNIT_CMP,
                macro_call.span,
                &format!("`{macro_name}` of unit values detected. This will always {result}"),
            );
        }
        return;
    }

    if let ExprKind::Binary(ref cmp, left, _) = expr.kind {
        let op = cmp.node;
        if op.is_comparison() && cx.typeck_results().expr_ty(left).is_unit() {
            let result = match op {
                BinOpKind::Eq | BinOpKind::Le | BinOpKind::Ge => "true",
                _ => "false",
            };
            span_lint(
                cx,
                UNIT_CMP,
                expr.span,
                &format!(
                    "{}-comparison of unit values detected. This will always be {result}",
                    op.as_str()
                ),
            );
        }
    }
}