Skip to content

Commit 3c91e70

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 3c91e70

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-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 !cx.typeck_results().expr_ty(lhs).is_unit() {
23+
return;
24+
}
25+
if is_unit_expr(lhs) || is_unit_expr(rhs) {
2326
return;
2427
}
2528
span_lint(

tests/ui/unit_cmp.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,17 @@ 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+
// don't lint on explicitly written unit expr
82+
assert_eq!(foo(), ());
83+
assert_ne!((), ContainsUnit(()).0);
84+
}

tests/ui/unit_cmp.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,17 @@ 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: aborting due to 7 previous errors
7587

0 commit comments

Comments
 (0)