Skip to content

Commit a04a13d

Browse files
committed
fix: eq_op FP on literal of different form
1 parent 1b21661 commit a04a13d

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-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+
fn is_literal_in_different_form(l: &Expr<'_>, r: &Expr<'_>) -> bool {
12+
if let (ExprKind::Lit(l), ExprKind::Lit(r)) = (l.kind, r.kind) {
13+
return match (l.node, r.node) {
14+
(LitKind::Str(_, l_style), LitKind::Str(_, r_style))
15+
| (LitKind::ByteStr(_, l_style), LitKind::ByteStr(_, r_style))
16+
| (LitKind::CStr(_, l_style), LitKind::CStr(_, r_style)) => l_style != r_style,
17+
(LitKind::Int(_, l_style), LitKind::Int(_, r_style)) => l_style != r_style,
18+
(LitKind::Float(_, l_style), LitKind::Float(_, r_style)) => l_style != r_style,
19+
(LitKind::Byte(_), LitKind::Byte(_))
20+
| (LitKind::Char(_), LitKind::Char(_))
21+
| (LitKind::Bool(_), LitKind::Bool(_)) => false,
22+
_ => true,
23+
};
24+
}
25+
26+
false
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+
&& !is_literal_in_different_form(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+
&& !is_literal_in_different_form(left, right)
64+
{
4165
span_lint_and_then(
4266
cx,
4367
EQ_OP,

tests/ui/eq_op.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,8 @@ 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 _: () = assert!(0x20 == b' ');
160+
assert_eq!(0x20, b' ');
161+
}

0 commit comments

Comments
 (0)