Skip to content

Commit 4418738

Browse files
committed
Use operator overloading instead of direct calls of make_binop
1 parent 174065f commit 4418738

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

clippy_lints/src/loops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ impl std::ops::Sub for &MinifyingSugg<'static> {
875875
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
876876
match (self.as_str(), rhs.as_str()) {
877877
(_, "0") => self.clone(),
878-
("0", _) => MinifyingSugg(sugg::make_unop("-", rhs.0.clone())),
878+
("0", _) => MinifyingSugg(-(rhs.0.clone())),
879879
(x, y) if x == y => MinifyingSugg::non_paren("0"),
880880
(_, _) => MinifyingSugg(&self.0 - &rhs.0),
881881
}
@@ -898,7 +898,7 @@ impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
898898
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
899899
match (self.as_str(), rhs.as_str()) {
900900
(_, "0") => self,
901-
("0", _) => MinifyingSugg(sugg::make_unop("-", rhs.0.clone())),
901+
("0", _) => MinifyingSugg(-(rhs.0.clone())),
902902
(x, y) if x == y => MinifyingSugg::non_paren("0"),
903903
(_, _) => MinifyingSugg(self.0 - &rhs.0),
904904
}

clippy_lints/src/utils/sugg.rs

+8-1
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> {
@@ -354,6 +354,13 @@ impl Sub for &Sugg<'_> {
354354
forward_binop_impls_to_ref!(impl Add, add for Sugg<'_>, type Output = Sugg<'static>);
355355
forward_binop_impls_to_ref!(impl Sub, sub for Sugg<'_>, type Output = Sugg<'static>);
356356

357+
impl Neg for Sugg<'_> {
358+
type Output = Sugg<'static>;
359+
fn neg(self) -> Sugg<'static> {
360+
make_unop("-", self)
361+
}
362+
}
363+
357364
impl Not for Sugg<'_> {
358365
type Output = Sugg<'static>;
359366
fn not(self) -> Sugg<'static> {

0 commit comments

Comments
 (0)