Skip to content

Commit 6afa4ef

Browse files
committed
Introduce function for comparing expression values
Introduce `eq_expr_value(cx, a, b)` as a shortcut for `SpanlessEq::new(cx).ignore_fn().eq_expr(cx, a, b)`. No functional changes intended.
1 parent 8d0d89a commit 6afa4ef

File tree

11 files changed

+45
-49
lines changed

11 files changed

+45
-49
lines changed

clippy_lints/src/assign_ops.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{
2-
get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method, SpanlessEq,
2+
eq_expr_value, get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method,
33
};
44
use crate::utils::{higher, sugg};
55
use if_chain::if_chain;
@@ -70,11 +70,11 @@ impl<'tcx> LateLintPass<'tcx> for AssignOps {
7070
return;
7171
}
7272
// lhs op= l op r
73-
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, l) {
73+
if eq_expr_value(cx, lhs, l) {
7474
lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, r);
7575
}
7676
// lhs op= l commutative_op r
77-
if is_commutative(op.node) && SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, r) {
77+
if is_commutative(op.node) && eq_expr_value(cx, lhs, r) {
7878
lint_misrefactored_assign_op(cx, expr, *op, rhs, lhs, l);
7979
}
8080
}
@@ -161,14 +161,12 @@ impl<'tcx> LateLintPass<'tcx> for AssignOps {
161161

162162
if visitor.counter == 1 {
163163
// a = a op b
164-
if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, l) {
164+
if eq_expr_value(cx, assignee, l) {
165165
lint(assignee, r);
166166
}
167167
// a = b commutative_op a
168168
// Limited to primitive type as these ops are know to be commutative
169-
if SpanlessEq::new(cx).ignore_fn().eq_expr(assignee, r)
170-
&& cx.typeck_results().expr_ty(assignee).is_primitive_ty()
171-
{
169+
if eq_expr_value(cx, assignee, r) && cx.typeck_results().expr_ty(assignee).is_primitive_ty() {
172170
match op.node {
173171
hir::BinOpKind::Add
174172
| hir::BinOpKind::Mul
@@ -253,7 +251,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ExprVisitor<'a, 'tcx> {
253251
type Map = Map<'tcx>;
254252

255253
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
256-
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(self.assignee, expr) {
254+
if eq_expr_value(self.cx, self.assignee, expr) {
257255
self.counter += 1;
258256
}
259257

clippy_lints/src/booleans.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{
2-
get_trait_def_id, implements_trait, in_macro, is_type_diagnostic_item, paths, snippet_opt, span_lint_and_sugg,
3-
span_lint_and_then, SpanlessEq,
2+
eq_expr_value, get_trait_def_id, implements_trait, in_macro, is_type_diagnostic_item, paths, snippet_opt,
3+
span_lint_and_sugg, span_lint_and_then,
44
};
55
use if_chain::if_chain;
66
use rustc_ast::ast::LitKind;
@@ -128,7 +128,7 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
128128
}
129129
}
130130
for (n, expr) in self.terminals.iter().enumerate() {
131-
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(e, expr) {
131+
if eq_expr_value(self.cx, e, expr) {
132132
#[allow(clippy::cast_possible_truncation)]
133133
return Ok(Bool::Term(n as u8));
134134
}
@@ -138,8 +138,8 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
138138
if implements_ord(self.cx, e_lhs);
139139
if let ExprKind::Binary(expr_binop, expr_lhs, expr_rhs) = &expr.kind;
140140
if negate(e_binop.node) == Some(expr_binop.node);
141-
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(e_lhs, expr_lhs);
142-
if SpanlessEq::new(self.cx).ignore_fn().eq_expr(e_rhs, expr_rhs);
141+
if eq_expr_value(self.cx, e_lhs, expr_lhs);
142+
if eq_expr_value(self.cx, e_rhs, expr_rhs);
143143
then {
144144
#[allow(clippy::cast_possible_truncation)]
145145
return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));

clippy_lints/src/copies.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use crate::utils::{eq_expr_value, SpanlessEq, SpanlessHash};
12
use crate::utils::{get_parent_expr, higher, if_sequence, snippet, span_lint_and_note, span_lint_and_then};
2-
use crate::utils::{SpanlessEq, SpanlessHash};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::{Arm, Block, Expr, ExprKind, MatchSource, Pat, PatKind};
55
use rustc_lint::{LateContext, LateLintPass};
@@ -197,8 +197,7 @@ fn lint_same_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
197197
h.finish()
198198
};
199199

200-
let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool =
201-
&|&lhs, &rhs| -> bool { SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, rhs) };
200+
let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool { eq_expr_value(cx, lhs, rhs) };
202201

203202
for (i, j) in search_same(conds, hash, eq) {
204203
span_lint_and_note(
@@ -222,7 +221,7 @@ fn lint_same_fns_in_if_cond(cx: &LateContext<'_>, conds: &[&Expr<'_>]) {
222221

223222
let eq: &dyn Fn(&&Expr<'_>, &&Expr<'_>) -> bool = &|&lhs, &rhs| -> bool {
224223
// Do not spawn warning if `IFS_SAME_COND` already produced it.
225-
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs, rhs) {
224+
if eq_expr_value(cx, lhs, rhs) {
226225
return false;
227226
}
228227
SpanlessEq::new(cx).eq_expr(lhs, rhs)

clippy_lints/src/double_comparison.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
77
use rustc_span::source_map::Span;
88

9-
use crate::utils::{snippet_with_applicability, span_lint_and_sugg, SpanlessEq};
9+
use crate::utils::{eq_expr_value, snippet_with_applicability, span_lint_and_sugg};
1010

1111
declare_clippy_lint! {
1212
/// **What it does:** Checks for double comparisons that could be simplified to a single expression.
@@ -46,8 +46,7 @@ impl<'tcx> DoubleComparisons {
4646
},
4747
_ => return,
4848
};
49-
let mut spanless_eq = SpanlessEq::new(cx).ignore_fn();
50-
if !(spanless_eq.eq_expr(&llhs, &rlhs) && spanless_eq.eq_expr(&lrhs, &rrhs)) {
49+
if !(eq_expr_value(cx, &llhs, &rlhs) && eq_expr_value(cx, &lrhs, &rrhs)) {
5150
return;
5251
}
5352
macro_rules! lint_double_comparison {

clippy_lints/src/eq_op.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{
2-
implements_trait, in_macro, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then, SpanlessEq,
2+
eq_expr_value, implements_trait, in_macro, is_copy, multispan_sugg, snippet, span_lint, span_lint_and_then,
33
};
44
use rustc_errors::Applicability;
55
use rustc_hir::{BinOp, BinOpKind, BorrowKind, Expr, ExprKind};
@@ -69,7 +69,7 @@ impl<'tcx> LateLintPass<'tcx> for EqOp {
6969
if macro_with_not_op(&left.kind) || macro_with_not_op(&right.kind) {
7070
return;
7171
}
72-
if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
72+
if is_valid_operator(op) && eq_expr_value(cx, left, right) {
7373
span_lint(
7474
cx,
7575
EQ_OP,

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::consts::{
22
constant, constant_simple, Constant,
33
Constant::{Int, F32, F64},
44
};
5-
use crate::utils::{get_parent_expr, higher, numeric_literal, span_lint_and_sugg, sugg, SpanlessEq};
5+
use crate::utils::{eq_expr_value, get_parent_expr, higher, numeric_literal, span_lint_and_sugg, sugg};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
@@ -363,8 +363,8 @@ fn detect_hypot(cx: &LateContext<'_>, args: &[Expr<'_>]) -> Option<String> {
363363
if_chain! {
364364
if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, ref lmul_lhs, ref lmul_rhs) = add_lhs.kind;
365365
if let ExprKind::Binary(Spanned { node: BinOpKind::Mul, .. }, ref rmul_lhs, ref rmul_rhs) = add_rhs.kind;
366-
if are_exprs_equal(cx, lmul_lhs, lmul_rhs);
367-
if are_exprs_equal(cx, rmul_lhs, rmul_rhs);
366+
if eq_expr_value(cx, lmul_lhs, lmul_rhs);
367+
if eq_expr_value(cx, rmul_lhs, rmul_rhs);
368368
then {
369369
return Some(format!("{}.hypot({})", Sugg::hir(cx, &lmul_lhs, ".."), Sugg::hir(cx, &rmul_lhs, "..")));
370370
}
@@ -502,8 +502,8 @@ fn check_mul_add(cx: &LateContext<'_>, expr: &Expr<'_>) {
502502
fn is_testing_positive(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
503503
if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
504504
match op {
505-
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, right) && are_exprs_equal(cx, left, test),
506-
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, left) && are_exprs_equal(cx, right, test),
505+
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, right) && eq_expr_value(cx, left, test),
506+
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, left) && eq_expr_value(cx, right, test),
507507
_ => false,
508508
}
509509
} else {
@@ -515,19 +515,15 @@ fn is_testing_positive(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -
515515
fn is_testing_negative(cx: &LateContext<'_>, expr: &Expr<'_>, test: &Expr<'_>) -> bool {
516516
if let ExprKind::Binary(Spanned { node: op, .. }, left, right) = expr.kind {
517517
match op {
518-
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, left) && are_exprs_equal(cx, right, test),
519-
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, right) && are_exprs_equal(cx, left, test),
518+
BinOpKind::Gt | BinOpKind::Ge => is_zero(cx, left) && eq_expr_value(cx, right, test),
519+
BinOpKind::Lt | BinOpKind::Le => is_zero(cx, right) && eq_expr_value(cx, left, test),
520520
_ => false,
521521
}
522522
} else {
523523
false
524524
}
525525
}
526526

527-
fn are_exprs_equal(cx: &LateContext<'_>, expr1: &Expr<'_>, expr2: &Expr<'_>) -> bool {
528-
SpanlessEq::new(cx).ignore_fn().eq_expr(expr1, expr2)
529-
}
530-
531527
/// Returns true iff expr is some zero literal
532528
fn is_zero(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
533529
match constant_simple(cx, cx.typeck_results(), expr) {
@@ -546,12 +542,12 @@ fn is_zero(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
546542
/// returns None.
547543
fn are_negated<'a>(cx: &LateContext<'_>, expr1: &'a Expr<'a>, expr2: &'a Expr<'a>) -> Option<(bool, &'a Expr<'a>)> {
548544
if let ExprKind::Unary(UnOp::UnNeg, expr1_negated) = &expr1.kind {
549-
if are_exprs_equal(cx, expr1_negated, expr2) {
545+
if eq_expr_value(cx, expr1_negated, expr2) {
550546
return Some((false, expr2));
551547
}
552548
}
553549
if let ExprKind::Unary(UnOp::UnNeg, expr2_negated) = &expr2.kind {
554-
if are_exprs_equal(cx, expr1, expr2_negated) {
550+
if eq_expr_value(cx, expr1, expr2_negated) {
555551
return Some((true, expr1));
556552
}
557553
}
@@ -614,7 +610,7 @@ fn are_same_base_logs(cx: &LateContext<'_>, expr_a: &Expr<'_>, expr_b: &Expr<'_>
614610
args_a.len() == args_b.len() &&
615611
(
616612
["ln", "log2", "log10"].contains(&&*method_name_a.as_str()) ||
617-
method_name_a.as_str() == "log" && args_a.len() == 2 && are_exprs_equal(cx, &args_a[1], &args_b[1])
613+
method_name_a.as_str() == "log" && args_a.len() == 2 && eq_expr_value(cx, &args_a[1], &args_b[1])
618614
);
619615
}
620616
}

clippy_lints/src/question_mark.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
77

88
use crate::utils::sugg::Sugg;
99
use crate::utils::{
10-
higher, is_type_diagnostic_item, match_def_path, match_qpath, paths, snippet_with_applicability,
11-
span_lint_and_sugg, SpanlessEq,
10+
eq_expr_value, higher, is_type_diagnostic_item, match_def_path, match_qpath, paths, snippet_with_applicability,
11+
span_lint_and_sugg,
1212
};
1313

1414
declare_clippy_lint! {
@@ -65,7 +65,7 @@ impl QuestionMark {
6565
if let ExprKind::Block(block, None) = &else_.kind;
6666
if block.stmts.is_empty();
6767
if let Some(block_expr) = &block.expr;
68-
if SpanlessEq::new(cx).ignore_fn().eq_expr(subject, block_expr);
68+
if eq_expr_value(cx, subject, block_expr);
6969
then {
7070
replacement = Some(format!("Some({}?)", receiver_str));
7171
}

clippy_lints/src/swap.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::sugg::Sugg;
22
use crate::utils::{
3-
differing_macro_contexts, is_type_diagnostic_item, snippet_with_applicability, span_lint_and_then, walk_ptrs_ty,
4-
SpanlessEq,
3+
differing_macro_contexts, eq_expr_value, is_type_diagnostic_item, snippet_with_applicability, span_lint_and_then,
4+
walk_ptrs_ty,
55
};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
@@ -92,8 +92,8 @@ fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
9292
if rhs2.segments.len() == 1;
9393

9494
if ident.as_str() == rhs2.segments[0].ident.as_str();
95-
if SpanlessEq::new(cx).ignore_fn().eq_expr(tmp_init, lhs1);
96-
if SpanlessEq::new(cx).ignore_fn().eq_expr(rhs1, lhs2);
95+
if eq_expr_value(cx, tmp_init, lhs1);
96+
if eq_expr_value(cx, rhs1, lhs2);
9797
then {
9898
if let ExprKind::Field(ref lhs1, _) = lhs1.kind {
9999
if let ExprKind::Field(ref lhs2, _) = lhs2.kind {
@@ -193,7 +193,7 @@ enum Slice<'a> {
193193
fn check_for_slice<'a>(cx: &LateContext<'_>, lhs1: &'a Expr<'_>, lhs2: &'a Expr<'_>) -> Slice<'a> {
194194
if let ExprKind::Index(ref lhs1, ref idx1) = lhs1.kind {
195195
if let ExprKind::Index(ref lhs2, ref idx2) = lhs2.kind {
196-
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, lhs2) {
196+
if eq_expr_value(cx, lhs1, lhs2) {
197197
let ty = walk_ptrs_ty(cx.typeck_results().expr_ty(lhs1));
198198

199199
if matches!(ty.kind, ty::Slice(_))
@@ -221,8 +221,8 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
221221
if !differing_macro_contexts(first.span, second.span);
222222
if let ExprKind::Assign(ref lhs0, ref rhs0, _) = first.kind;
223223
if let ExprKind::Assign(ref lhs1, ref rhs1, _) = second.kind;
224-
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs0, rhs1);
225-
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, rhs0);
224+
if eq_expr_value(cx, lhs0, rhs1);
225+
if eq_expr_value(cx, lhs1, rhs0);
226226
then {
227227
let lhs0 = Sugg::hir_opt(cx, lhs0);
228228
let rhs0 = Sugg::hir_opt(cx, rhs0);

clippy_lints/src/utils/hir_utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ pub fn over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -
340340
left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
341341
}
342342

343+
/// Checks if two expressions evaluate to the same value, and don't contain any side effects.
344+
pub fn eq_expr_value(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>) -> bool {
345+
SpanlessEq::new(cx).ignore_fn().eq_expr(left, right)
346+
}
347+
343348
/// Type used to hash an ast element. This is different from the `Hash` trait
344349
/// on ast types as this
345350
/// trait would consider IDs and spans.

clippy_lints/src/utils/internal_lints.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::utils::SpanlessEq;
21
use crate::utils::{
32
is_expn_of, match_def_path, match_qpath, match_type, method_calls, paths, run_lints, snippet, span_lint,
4-
span_lint_and_help, span_lint_and_sugg, walk_ptrs_ty,
3+
span_lint_and_help, span_lint_and_sugg, walk_ptrs_ty, SpanlessEq,
54
};
65
use if_chain::if_chain;
76
use rustc_ast::ast::{Crate as AstCrate, ItemKind, LitKind, NodeId};

0 commit comments

Comments
 (0)