Skip to content

Commit eb3ffe6

Browse files
committed
make use of macros in operator overloading
1 parent 4ea4a97 commit eb3ffe6

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

clippy_lints/src/utils/sugg.rs

+30-25
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_span::{BytePos, Pos};
1313
use std::borrow::Cow;
1414
use std::convert::TryInto;
1515
use std::fmt::Display;
16+
use std::ops::{Add, Sub, Not};
1617

1718
/// A helper type to build suggestion correctly handling parenthesis.
1819
pub enum Sugg<'a> {
@@ -307,49 +308,53 @@ impl<'a> Sugg<'a> {
307308
}
308309
}
309310

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;
316316

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+
}
323321

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;
330324

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+
}
335337
}
336338
}
337339

338-
impl std::ops::Add for &Sugg<'_> {
340+
impl Add for &Sugg<'_> {
339341
type Output = Sugg<'static>;
340342
fn add(self, rhs: &Sugg<'_>) -> Sugg<'static> {
341343
make_binop(ast::BinOpKind::Add, self, rhs)
342344
}
343345
}
344346

345-
impl std::ops::Sub for &Sugg<'_> {
347+
impl Sub for &Sugg<'_> {
346348
type Output = Sugg<'static>;
347349
fn sub(self, rhs: &Sugg<'_>) -> Sugg<'static> {
348350
make_binop(ast::BinOpKind::Sub, self, rhs)
349351
}
350352
}
351353

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<'_> {
353358
type Output = Sugg<'static>;
354359
fn not(self) -> Sugg<'static> {
355360
make_unop("!", self)

0 commit comments

Comments
 (0)