Skip to content

Commit bb8ba69

Browse files
committed
unit_cmp: don't lint on explicitly written unit expr
changelog: [`unit_cmp`]: don't lint on explicitly written unit expr Signed-off-by: Zihan <[email protected]>
1 parent f35c203 commit bb8ba69

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

clippy_lints/src/unit_types/unit_cmp.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
3-
use clippy_utils::sym;
3+
use clippy_utils::{is_unit_expr, sym};
44
use rustc_hir::{BinOpKind, Expr, ExprKind};
55
use rustc_lint::LateContext;
66

@@ -16,10 +16,13 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
1616
sym::assert_ne_macro | sym::debug_assert_ne_macro => "fail",
1717
_ => return,
1818
};
19-
let Some((left, _, _)) = find_assert_eq_args(cx, expr, macro_call.expn) else {
19+
let Some((lhs, rhs, _)) = find_assert_eq_args(cx, expr, macro_call.expn) else {
2020
return;
2121
};
22-
if !cx.typeck_results().expr_ty(left).is_unit() {
22+
if is_unit_expr(lhs) || is_unit_expr(rhs) {
23+
return;
24+
}
25+
if !cx.typeck_results().expr_ty(lhs).is_unit() {
2326
return;
2427
}
2528
span_lint(

tests/ui/unit_cmp.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,20 @@ fn main() {
6868
}
6969
);
7070
}
71+
72+
fn issue15559() {
73+
fn foo() {}
74+
assert_eq!(
75+
//~^ unit_cmp
76+
{
77+
1;
78+
},
79+
foo()
80+
);
81+
assert_eq!(foo(), foo());
82+
//~^ unit_cmp
83+
84+
// don't lint on explicitly written unit expr
85+
assert_eq!(foo(), ());
86+
assert_ne!((), ContainsUnit(()).0);
87+
}

tests/ui/unit_cmp.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,23 @@ LL | | true;
7171
LL | | );
7272
| |_____^
7373

74-
error: aborting due to 6 previous errors
74+
error: `assert_eq` of unit values detected. This will always succeed
75+
--> tests/ui/unit_cmp.rs:74:5
76+
|
77+
LL | / assert_eq!(
78+
LL | |
79+
LL | | {
80+
LL | | 1;
81+
LL | | },
82+
LL | | foo()
83+
LL | | );
84+
| |_____^
85+
86+
error: `assert_eq` of unit values detected. This will always succeed
87+
--> tests/ui/unit_cmp.rs:81:5
88+
|
89+
LL | assert_eq!(foo(), foo());
90+
| ^^^^^^^^^^^^^^^^^^^^^^^^
91+
92+
error: aborting due to 8 previous errors
7593

0 commit comments

Comments
 (0)