diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2025-08-27 20:34:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-27 20:34:48 +0000 |
| commit | f982b48e6aabd9ad00523f65fdb116a7bf6eb422 (patch) | |
| tree | b0e65bf66bb6bf83f872a1a6d210c0cc3e09bd97 | |
| parent | 0afd6a6bb5830cb2e0af3c7c914b2a11d10e5827 (diff) | |
| parent | bb8ba698f54421b0b1d7bee5b91f48f16182650f (diff) | |
| download | rust-f982b48e6aabd9ad00523f65fdb116a7bf6eb422.tar.gz rust-f982b48e6aabd9ad00523f65fdb116a7bf6eb422.zip | |
`unit_cmp`: don't lint on explicitly written unit expr (#15562)
Fixes rust-lang/rust-clippy#15559 changelog: [`unit_cmp`]: don't lint on explicitly written unit expr
| -rw-r--r-- | clippy_lints/src/unit_types/unit_cmp.rs | 9 | ||||
| -rw-r--r-- | tests/ui/unit_cmp.rs | 17 | ||||
| -rw-r--r-- | tests/ui/unit_cmp.stderr | 20 |
3 files changed, 42 insertions, 4 deletions
diff --git a/clippy_lints/src/unit_types/unit_cmp.rs b/clippy_lints/src/unit_types/unit_cmp.rs index 48b532968cb..38716519e23 100644 --- a/clippy_lints/src/unit_types/unit_cmp.rs +++ b/clippy_lints/src/unit_types/unit_cmp.rs @@ -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; @@ -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( diff --git a/tests/ui/unit_cmp.rs b/tests/ui/unit_cmp.rs index 93f5b87c3d2..0a0fe3a1990 100644 --- a/tests/ui/unit_cmp.rs +++ b/tests/ui/unit_cmp.rs @@ -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); +} diff --git a/tests/ui/unit_cmp.stderr b/tests/ui/unit_cmp.stderr index 8f26749fd86..21aa9dce151 100644 --- a/tests/ui/unit_cmp.stderr +++ b/tests/ui/unit_cmp.stderr @@ -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 |
