Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions clippy_lints/src/unit_types/unit_cmp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
use clippy_utils::sym;
use clippy_utils::{is_unit_expr, sym};
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::LateContext;

Expand All @@ -16,10 +16,13 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
sym::assert_ne_macro | sym::debug_assert_ne_macro => "fail",
_ => return,
};
let Some((left, _, _)) = find_assert_eq_args(cx, expr, macro_call.expn) else {
let Some((lhs, rhs, _)) = find_assert_eq_args(cx, expr, macro_call.expn) else {
return;
};
if !cx.typeck_results().expr_ty(left).is_unit() {
if is_unit_expr(lhs) || is_unit_expr(rhs) {
return;
}
if !cx.typeck_results().expr_ty(lhs).is_unit() {
return;
}
span_lint(
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/unit_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,20 @@ fn main() {
}
);
}

fn issue15559() {
fn foo() {}
assert_eq!(
//~^ unit_cmp
{
1;
},
foo()
);
assert_eq!(foo(), foo());
//~^ unit_cmp

// don't lint on explicitly written unit expr
assert_eq!(foo(), ());
assert_ne!((), ContainsUnit(()).0);
}
20 changes: 19 additions & 1 deletion tests/ui/unit_cmp.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,23 @@ LL | | true;
LL | | );
| |_____^

error: aborting due to 6 previous errors
error: `assert_eq` of unit values detected. This will always succeed
--> tests/ui/unit_cmp.rs:74:5
|
LL | / assert_eq!(
LL | |
LL | | {
LL | | 1;
LL | | },
LL | | foo()
LL | | );
| |_____^

error: `assert_eq` of unit values detected. This will always succeed
--> tests/ui/unit_cmp.rs:81:5
|
LL | assert_eq!(foo(), foo());
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 8 previous errors