]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/unit_types/unit_cmp.rs
Auto merge of #80182 - in42:stack_trace, r=tmandry
[rust.git] / src / tools / clippy / clippy_lints / src / unit_types / unit_cmp.rs
1 use clippy_utils::diagnostics::span_lint;
2 use rustc_hir::{BinOpKind, Expr, ExprKind};
3 use rustc_lint::LateContext;
4 use rustc_span::hygiene::{ExpnKind, MacroKind};
5
6 use super::UNIT_CMP;
7
8 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
9     if expr.span.from_expansion() {
10         if let Some(callee) = expr.span.source_callee() {
11             if let ExpnKind::Macro {
12                 kind: MacroKind::Bang,
13                 name: symbol,
14                 proc_macro: _,
15             } = callee.kind
16             {
17                 if let ExprKind::Binary(ref cmp, left, _) = expr.kind {
18                     let op = cmp.node;
19                     if op.is_comparison() && cx.typeck_results().expr_ty(left).is_unit() {
20                         let result = match &*symbol.as_str() {
21                             "assert_eq" | "debug_assert_eq" => "succeed",
22                             "assert_ne" | "debug_assert_ne" => "fail",
23                             _ => return,
24                         };
25                         span_lint(
26                             cx,
27                             UNIT_CMP,
28                             expr.span,
29                             &format!(
30                                 "`{}` of unit values detected. This will always {}",
31                                 symbol.as_str(),
32                                 result
33                             ),
34                         );
35                     }
36                 }
37             }
38         }
39         return;
40     }
41
42     if let ExprKind::Binary(ref cmp, left, _) = expr.kind {
43         let op = cmp.node;
44         if op.is_comparison() && cx.typeck_results().expr_ty(left).is_unit() {
45             let result = match op {
46                 BinOpKind::Eq | BinOpKind::Le | BinOpKind::Ge => "true",
47                 _ => "false",
48             };
49             span_lint(
50                 cx,
51                 UNIT_CMP,
52                 expr.span,
53                 &format!(
54                     "{}-comparison of unit values detected. This will always be {}",
55                     op.as_str(),
56                     result
57                 ),
58             );
59         }
60     }
61 }