about summary refs log tree commit diff
diff options
context:
space:
mode:
authorrail <12975677+rail-rain@users.noreply.github.com>2020-06-23 18:51:05 +1200
committerrail <12975677+rail-rain@users.noreply.github.com>2020-09-25 09:02:05 +1200
commiteb3ffe6ed2999e9d3385be0ff981e30082ea0d2c (patch)
tree58d3e229f534a24d5400f5537b7b51d16927a449
parent4ea4a972500a8ddecfc737d51eec960324dcb02f (diff)
downloadrust-eb3ffe6ed2999e9d3385be0ff981e30082ea0d2c.tar.gz
rust-eb3ffe6ed2999e9d3385be0ff981e30082ea0d2c.zip
make use of macros in operator overloading
-rw-r--r--clippy_lints/src/utils/sugg.rs55
1 files changed, 30 insertions, 25 deletions
diff --git a/clippy_lints/src/utils/sugg.rs b/clippy_lints/src/utils/sugg.rs
index 50d48650a09..aada4122e78 100644
--- a/clippy_lints/src/utils/sugg.rs
+++ b/clippy_lints/src/utils/sugg.rs
@@ -13,6 +13,7 @@ use rustc_span::{BytePos, Pos};
 use std::borrow::Cow;
 use std::convert::TryInto;
 use std::fmt::Display;
+use std::ops::{Add, Sub, Not};
 
 /// A helper type to build suggestion correctly handling parenthesis.
 pub enum Sugg<'a> {
@@ -307,49 +308,53 @@ impl<'a> Sugg<'a> {
     }
 }
 
-impl std::ops::Add for Sugg<'_> {
-    type Output = Sugg<'static>;
-    fn add(self, rhs: Sugg<'_>) -> Sugg<'static> {
-        make_binop(ast::BinOpKind::Add, &self, &rhs)
-    }
-}
+// Copied from the rust standart library, and then edited
+macro_rules! forward_binop_impls_to_ref {
+    (impl $imp:ident, $method:ident for $t:ty, type Output = $o:ty) => {
+        impl $imp<$t> for &$t {
+            type Output = $o;
 
-impl std::ops::Sub for Sugg<'_> {
-    type Output = Sugg<'static>;
-    fn sub(self, rhs: Sugg<'_>) -> Sugg<'static> {
-        make_binop(ast::BinOpKind::Sub, &self, &rhs)
-    }
-}
+            fn $method(self, other: $t) -> $o {
+                $imp::$method(self, &other)
+            }
+        }
 
-impl std::ops::Add<&Sugg<'_>> for Sugg<'_> {
-    type Output = Sugg<'static>;
-    fn add(self, rhs: &Sugg<'_>) -> Sugg<'static> {
-        make_binop(ast::BinOpKind::Add, &self, rhs)
-    }
-}
+        impl $imp<&$t> for $t {
+            type Output = $o;
 
-impl std::ops::Sub<&Sugg<'_>> for Sugg<'_> {
-    type Output = Sugg<'static>;
-    fn sub(self, rhs: &Sugg<'_>) -> Sugg<'static> {
-        make_binop(ast::BinOpKind::Sub, &self, rhs)
+            fn $method(self, other: &$t) -> $o {
+                $imp::$method(&self, other)
+            }
+        }
+
+        impl $imp for $t {
+            type Output = $o;
+
+            fn $method(self, other: $t) -> $o {
+                $imp::$method(&self, &other)
+            }
+        }
     }
 }
 
-impl std::ops::Add for &Sugg<'_> {
+impl Add for &Sugg<'_> {
     type Output = Sugg<'static>;
     fn add(self, rhs: &Sugg<'_>) -> Sugg<'static> {
         make_binop(ast::BinOpKind::Add, self, rhs)
     }
 }
 
-impl std::ops::Sub for &Sugg<'_> {
+impl Sub for &Sugg<'_> {
     type Output = Sugg<'static>;
     fn sub(self, rhs: &Sugg<'_>) -> Sugg<'static> {
         make_binop(ast::BinOpKind::Sub, self, rhs)
     }
 }
 
-impl std::ops::Not for Sugg<'_> {
+forward_binop_impls_to_ref!(impl Add, add for Sugg<'_>, type Output = Sugg<'static>);
+forward_binop_impls_to_ref!(impl Sub, sub for Sugg<'_>, type Output = Sugg<'static>);
+
+impl Not for Sugg<'_> {
     type Output = Sugg<'static>;
     fn not(self) -> Sugg<'static> {
         make_unop("!", self)