@@ -23,23 +23,22 @@ pub struct SpanlessEq<'a, 'tcx> {
2323 /// Context used to evaluate constant expressions.
2424 cx : & ' a LateContext < ' tcx > ,
2525 maybe_typeck_results : Option < & ' tcx TypeckResults < ' tcx > > ,
26- /// If is true, never consider as equal expressions containing function
27- /// calls.
28- ignore_fn : bool ,
26+ allow_side_effects : bool ,
2927}
3028
3129impl < ' a , ' tcx > SpanlessEq < ' a , ' tcx > {
3230 pub fn new ( cx : & ' a LateContext < ' tcx > ) -> Self {
3331 Self {
3432 cx,
3533 maybe_typeck_results : cx. maybe_typeck_results ( ) ,
36- ignore_fn : false ,
34+ allow_side_effects : true ,
3735 }
3836 }
3937
40- pub fn ignore_fn ( self ) -> Self {
38+ /// Consider expressions containing potential side effects as not equal.
39+ pub fn deny_side_effects ( self ) -> Self {
4140 Self {
42- ignore_fn : true ,
41+ allow_side_effects : false ,
4342 ..self
4443 }
4544 }
@@ -67,7 +66,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
6766
6867 #[ allow( clippy:: similar_names) ]
6968 pub fn eq_expr ( & mut self , left : & Expr < ' _ > , right : & Expr < ' _ > ) -> bool {
70- if self . ignore_fn && differing_macro_contexts ( left. span , right. span ) {
69+ if ! self . allow_side_effects && differing_macro_contexts ( left. span , right. span ) {
7170 return false ;
7271 }
7372
@@ -108,7 +107,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
108107 } ,
109108 ( & ExprKind :: Box ( ref l) , & ExprKind :: Box ( ref r) ) => self . eq_expr ( l, r) ,
110109 ( & ExprKind :: Call ( l_fun, l_args) , & ExprKind :: Call ( r_fun, r_args) ) => {
111- ! self . ignore_fn && self . eq_expr ( l_fun, r_fun) && self . eq_exprs ( l_args, r_args)
110+ self . allow_side_effects && self . eq_expr ( l_fun, r_fun) && self . eq_exprs ( l_args, r_args)
112111 } ,
113112 ( & ExprKind :: Cast ( ref lx, ref lt) , & ExprKind :: Cast ( ref rx, ref rt) )
114113 | ( & ExprKind :: Type ( ref lx, ref lt) , & ExprKind :: Type ( ref rx, ref rt) ) => {
@@ -134,7 +133,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
134133 } )
135134 } ,
136135 ( & ExprKind :: MethodCall ( l_path, _, l_args, _) , & ExprKind :: MethodCall ( r_path, _, r_args, _) ) => {
137- ! self . ignore_fn && self . eq_path_segment ( l_path, r_path) && self . eq_exprs ( l_args, r_args)
136+ self . allow_side_effects && self . eq_path_segment ( l_path, r_path) && self . eq_exprs ( l_args, r_args)
138137 } ,
139138 ( & ExprKind :: Repeat ( ref le, ref ll_id) , & ExprKind :: Repeat ( ref re, ref rl_id) ) => {
140139 let mut celcx = constant_context ( self . cx , self . cx . tcx . typeck_body ( ll_id. body ) ) ;
@@ -342,7 +341,7 @@ pub fn over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -
342341
343342/// Checks if two expressions evaluate to the same value, and don't contain any side effects.
344343pub fn eq_expr_value ( cx : & LateContext < ' _ > , left : & Expr < ' _ > , right : & Expr < ' _ > ) -> bool {
345- SpanlessEq :: new ( cx) . ignore_fn ( ) . eq_expr ( left, right)
344+ SpanlessEq :: new ( cx) . deny_side_effects ( ) . eq_expr ( left, right)
346345}
347346
348347/// Type used to hash an ast element. This is different from the `Hash` trait
0 commit comments