@@ -13,6 +13,7 @@ use rustc_span::{BytePos, Pos};
13
13
use std:: borrow:: Cow ;
14
14
use std:: convert:: TryInto ;
15
15
use std:: fmt:: Display ;
16
+ use std:: ops:: { Add , Sub , Not } ;
16
17
17
18
/// A helper type to build suggestion correctly handling parenthesis.
18
19
pub enum Sugg < ' a > {
@@ -307,49 +308,53 @@ impl<'a> Sugg<'a> {
307
308
}
308
309
}
309
310
310
- impl std:: ops:: Add for Sugg < ' _ > {
311
- type Output = Sugg < ' static > ;
312
- fn add ( self , rhs : Sugg < ' _ > ) -> Sugg < ' static > {
313
- make_binop ( ast:: BinOpKind :: Add , & self , & rhs)
314
- }
315
- }
311
+ // Copied from the rust standart library, and then edited
312
+ macro_rules! forward_binop_impls_to_ref {
313
+ ( impl $imp: ident, $method: ident for $t: ty, type Output = $o: ty) => {
314
+ impl $imp<$t> for & $t {
315
+ type Output = $o;
316
316
317
- impl std:: ops:: Sub for Sugg < ' _ > {
318
- type Output = Sugg < ' static > ;
319
- fn sub ( self , rhs : Sugg < ' _ > ) -> Sugg < ' static > {
320
- make_binop ( ast:: BinOpKind :: Sub , & self , & rhs)
321
- }
322
- }
317
+ fn $method( self , other: $t) -> $o {
318
+ $imp:: $method( self , & other)
319
+ }
320
+ }
323
321
324
- impl std:: ops:: Add < & Sugg < ' _ > > for Sugg < ' _ > {
325
- type Output = Sugg < ' static > ;
326
- fn add ( self , rhs : & Sugg < ' _ > ) -> Sugg < ' static > {
327
- make_binop ( ast:: BinOpKind :: Add , & self , rhs)
328
- }
329
- }
322
+ impl $imp<& $t> for $t {
323
+ type Output = $o;
330
324
331
- impl std:: ops:: Sub < & Sugg < ' _ > > for Sugg < ' _ > {
332
- type Output = Sugg < ' static > ;
333
- fn sub ( self , rhs : & Sugg < ' _ > ) -> Sugg < ' static > {
334
- make_binop ( ast:: BinOpKind :: Sub , & self , rhs)
325
+ fn $method( self , other: & $t) -> $o {
326
+ $imp:: $method( & self , other)
327
+ }
328
+ }
329
+
330
+ impl $imp for $t {
331
+ type Output = $o;
332
+
333
+ fn $method( self , other: $t) -> $o {
334
+ $imp:: $method( & self , & other)
335
+ }
336
+ }
335
337
}
336
338
}
337
339
338
- impl std :: ops :: Add for & Sugg < ' _ > {
340
+ impl Add for & Sugg < ' _ > {
339
341
type Output = Sugg < ' static > ;
340
342
fn add ( self , rhs : & Sugg < ' _ > ) -> Sugg < ' static > {
341
343
make_binop ( ast:: BinOpKind :: Add , self , rhs)
342
344
}
343
345
}
344
346
345
- impl std :: ops :: Sub for & Sugg < ' _ > {
347
+ impl Sub for & Sugg < ' _ > {
346
348
type Output = Sugg < ' static > ;
347
349
fn sub ( self , rhs : & Sugg < ' _ > ) -> Sugg < ' static > {
348
350
make_binop ( ast:: BinOpKind :: Sub , self , rhs)
349
351
}
350
352
}
351
353
352
- impl std:: ops:: Not for Sugg < ' _ > {
354
+ forward_binop_impls_to_ref ! ( impl Add , add for Sugg <' _>, type Output = Sugg <' static >) ;
355
+ forward_binop_impls_to_ref ! ( impl Sub , sub for Sugg <' _>, type Output = Sugg <' static >) ;
356
+
357
+ impl Not for Sugg < ' _ > {
353
358
type Output = Sugg < ' static > ;
354
359
fn not ( self ) -> Sugg < ' static > {
355
360
make_unop ( "!" , self )
0 commit comments