]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/unit_types/unit_cmp.rs
Rollup merge of #89090 - cjgillot:bare-dyn, r=jackh726
[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(MacroKind::Bang, symbol) = callee.kind {
12                 if let ExprKind::Binary(ref cmp, left, _) = expr.kind {
13                     let op = cmp.node;
14                     if op.is_comparison() && cx.typeck_results().expr_ty(left).is_unit() {
15                         let result = match &*symbol.as_str() {
16                             "assert_eq" | "debug_assert_eq" => "succeed",
17                             "assert_ne" | "debug_assert_ne" => "fail",
18                             _ => return,
19                         };
20                         span_lint(
21                             cx,
22                             UNIT_CMP,
23                             expr.span,
24                             &format!(
25                                 "`{}` of unit values detected. This will always {}",
26                                 symbol.as_str(),
27                                 result
28                             ),
29                         );
30                     }
31                 }
32             }
33         }
34         return;
35     }
36
37     if let ExprKind::Binary(ref cmp, left, _) = expr.kind {
38         let op = cmp.node;
39         if op.is_comparison() && cx.typeck_results().expr_ty(left).is_unit() {
40             let result = match op {
41                 BinOpKind::Eq | BinOpKind::Le | BinOpKind::Ge => "true",
42                 _ => "false",
43             };
44             span_lint(
45                 cx,
46                 UNIT_CMP,
47                 expr.span,
48                 &format!(
49                     "{}-comparison of unit values detected. This will always be {}",
50                     op.as_str(),
51                     result
52                 ),
53             );
54         }
55     }
56 }