Skip to content

Commit b42334c

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

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

clippy_lints/src/operators/eq_op.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,34 @@ 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_different(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_symbol, l_style), LitKind::Str(r_symbol, r_style)) => {
15+
l_style != r_style || l_symbol != r_symbol
16+
},
17+
(LitKind::ByteStr(l_symbol, l_style), LitKind::ByteStr(r_symbol, r_style))
18+
| (LitKind::CStr(l_symbol, l_style), LitKind::CStr(r_symbol, r_style)) => {
19+
l_style != r_style || l_symbol != r_symbol
20+
},
21+
(LitKind::Int(_, l_style), LitKind::Int(_, r_style)) => l_style != r_style,
22+
(LitKind::Float(_, l_style), LitKind::Float(_, r_style)) => l_style != r_style,
23+
(LitKind::Byte(_), LitKind::Byte(_))
24+
| (LitKind::Char(_), LitKind::Char(_))
25+
| (LitKind::Bool(_), LitKind::Bool(_)) => false,
26+
_ => true,
27+
};
28+
}
29+
30+
false
31+
}
32+
1033
pub(crate) fn check_assert<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
1134
if let Some(macro_call) = first_node_macro_backtrace(cx, e).find(|macro_call| {
1235
matches!(
@@ -17,6 +40,7 @@ pub(crate) fn check_assert<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
1740
&& eq_expr_value(cx, lhs, rhs)
1841
&& macro_call.is_local()
1942
&& !is_in_test_function(cx.tcx, e.hir_id)
43+
&& !is_literal_different(lhs, rhs)
2044
{
2145
span_lint(
2246
cx,
@@ -37,7 +61,11 @@ pub(crate) fn check<'tcx>(
3761
left: &'tcx Expr<'_>,
3862
right: &'tcx Expr<'_>,
3963
) {
40-
if is_useless_with_eq_exprs(op) && eq_expr_value(cx, left, right) && !is_in_test_function(cx.tcx, e.hir_id) {
64+
if is_useless_with_eq_exprs(op)
65+
&& eq_expr_value(cx, left, right)
66+
&& !is_in_test_function(cx.tcx, e.hir_id)
67+
&& !is_literal_different(left, right)
68+
{
4169
span_lint_and_then(
4270
cx,
4371
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)