Skip to content

Commit 0fbcd7a

Browse files
committed
Use operator overloading instead of direct calls of make_binop
1 parent e0a77a0 commit 0fbcd7a

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

clippy_lints/src/loops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ impl std::ops::Sub for &MinifyingSugg<'static> {
844844
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
845845
match (self.as_str(), rhs.as_str()) {
846846
(_, "0") => self.clone(),
847-
("0", _) => MinifyingSugg(sugg::make_unop("-", rhs.0.clone())),
847+
("0", _) => MinifyingSugg(-(rhs.0.clone())),
848848
(x, y) if x == y => MinifyingSugg::non_paren("0"),
849849
(_, _) => MinifyingSugg(&self.0 - &rhs.0),
850850
}
@@ -867,7 +867,7 @@ impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
867867
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
868868
match (self.as_str(), rhs.as_str()) {
869869
(_, "0") => self,
870-
("0", _) => MinifyingSugg(sugg::make_unop("-", rhs.0.clone())),
870+
("0", _) => MinifyingSugg(-(rhs.0.clone())),
871871
(x, y) if x == y => MinifyingSugg::non_paren("0"),
872872
(_, _) => MinifyingSugg(self.0 - &rhs.0),
873873
}

clippy_lints/src/utils/sugg.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +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, Not, Sub};
16+
use std::ops::{Add, Neg, Not, Sub};
1717

1818
/// A helper type to build suggestion correctly handling parenthesis.
1919
pub enum Sugg<'a> {
@@ -350,6 +350,13 @@ impl Sub for &Sugg<'_> {
350350
forward_binop_impls_to_ref!(impl Add, add for Sugg<'_>, type Output = Sugg<'static>);
351351
forward_binop_impls_to_ref!(impl Sub, sub for Sugg<'_>, type Output = Sugg<'static>);
352352

353+
impl Neg for Sugg<'_> {
354+
type Output = Sugg<'static>;
355+
fn neg(self) -> Sugg<'static> {
356+
make_unop("-", self)
357+
}
358+
}
359+
353360
impl Not for Sugg<'_> {
354361
type Output = Sugg<'static>;
355362
fn not(self) -> Sugg<'static> {

0 commit comments

Comments
 (0)