Skip to content

Commit cd0de93

Browse files
committed
fix: eq_op FP on literal of different form
1 parent 1a415e6 commit cd0de93

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

clippy_lints/src/operators/eq_op.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@ use clippy_utils::ast_utils::is_useless_with_eq_exprs;
22
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
33
use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace};
44
use clippy_utils::{eq_expr_value, is_in_test_function, sym};
5-
use rustc_hir::{BinOpKind, Expr};
5+
use rustc_ast::LitKind;
6+
use rustc_hir::{BinOpKind, Expr, ExprKind};
67
use rustc_lint::LateContext;
78

89
use super::EQ_OP;
910

11+
/// Checks if `l` and `r` are two literals using different forms (e.g., `b' '` vs. `0x20u8`)
12+
fn are_different_form_literals(l: &Expr<'_>, r: &Expr<'_>) -> bool {
13+
let (ExprKind::Lit(l), ExprKind::Lit(r)) = (l.kind, r.kind) else {
14+
return false;
15+
};
16+
match (l.node, r.node) {
17+
(LitKind::Str(_, l_style), LitKind::Str(_, r_style))
18+
| (LitKind::ByteStr(_, l_style), LitKind::ByteStr(_, r_style))
19+
| (LitKind::CStr(_, l_style), LitKind::CStr(_, r_style)) => l_style != r_style,
20+
(LitKind::Int(_, l_int_type), LitKind::Int(_, r_int_type)) => l_int_type != r_int_type,
21+
(LitKind::Float(_, l_float_type), LitKind::Float(_, r_float_type)) => l_float_type != r_float_type,
22+
(LitKind::Byte(_), LitKind::Byte(_))
23+
| (LitKind::Char(_), LitKind::Char(_))
24+
| (LitKind::Bool(_), LitKind::Bool(_)) => false,
25+
_ => true,
26+
}
27+
}
28+
1029
pub(crate) fn check_assert<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
1130
if let Some(macro_call) = first_node_macro_backtrace(cx, e).find(|macro_call| {
1231
matches!(
@@ -17,6 +36,7 @@ pub(crate) fn check_assert<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
1736
&& eq_expr_value(cx, lhs, rhs)
1837
&& macro_call.is_local()
1938
&& !is_in_test_function(cx.tcx, e.hir_id)
39+
&& !are_different_form_literals(lhs, rhs)
2040
{
2141
span_lint(
2242
cx,
@@ -37,7 +57,11 @@ pub(crate) fn check<'tcx>(
3757
left: &'tcx Expr<'_>,
3858
right: &'tcx Expr<'_>,
3959
) {
40-
if is_useless_with_eq_exprs(op) && eq_expr_value(cx, left, right) && !is_in_test_function(cx.tcx, e.hir_id) {
60+
if is_useless_with_eq_exprs(op)
61+
&& eq_expr_value(cx, left, right)
62+
&& !is_in_test_function(cx.tcx, e.hir_id)
63+
&& !are_different_form_literals(left, right)
64+
{
4165
span_lint_and_then(
4266
cx,
4367
EQ_OP,

tests/ui/eq_op.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,10 @@ fn eq_op_macros_shouldnt_trigger_in_tests() {
154154
assert_eq!(a, a);
155155
assert_eq!(a + b, b + a);
156156
}
157+
158+
fn issue15609() {
159+
const {
160+
assert!(0x20 == b' ');
161+
}
162+
assert_eq!(0x20, b' ');
163+
}

0 commit comments

Comments
 (0)