Skip to content

Commit 9725f00

Browse files
committed
Use the From trait to make MinifyingSugg
1 parent 99aceeb commit 9725f00

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed

clippy_lints/src/loops.rs

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -817,22 +817,22 @@ struct Offset {
817817
}
818818

819819
impl Offset {
820-
fn negative(value: MinifyingSugg<'static>) -> Self {
820+
fn negative(value: Sugg<'static>) -> Self {
821821
Self {
822-
value,
822+
value: value.into(),
823823
sign: OffsetSign::Negative,
824824
}
825825
}
826826

827-
fn positive(value: MinifyingSugg<'static>) -> Self {
827+
fn positive(value: Sugg<'static>) -> Self {
828828
Self {
829-
value,
829+
value: value.into(),
830830
sign: OffsetSign::Positive,
831831
}
832832
}
833833

834834
fn empty() -> Self {
835-
Self::positive(MinifyingSugg::non_paren("0"))
835+
Self::positive(sugg::ZERO)
836836
}
837837
}
838838

@@ -844,30 +844,22 @@ fn apply_offset(lhs: &MinifyingSugg<'static>, rhs: &Offset) -> MinifyingSugg<'st
844844
}
845845

846846
#[derive(Clone)]
847-
struct MinifyingSugg<'a>(sugg::Sugg<'a>);
848-
849-
impl std::fmt::Display for MinifyingSugg<'_> {
850-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
851-
std::fmt::Display::fmt(&self.0, f)
852-
}
853-
}
847+
struct MinifyingSugg<'a>(Sugg<'a>);
854848

855849
impl<'a> MinifyingSugg<'a> {
856850
fn as_str(&self) -> &str {
857-
let sugg::Sugg::NonParen(s) | sugg::Sugg::MaybeParen(s) | sugg::Sugg::BinOp(_, s) = &self.0;
851+
let Sugg::NonParen(s) | Sugg::MaybeParen(s) | Sugg::BinOp(_, s) = &self.0;
858852
s.as_ref()
859853
}
860854

861-
fn hir(cx: &LateContext<'_>, expr: &Expr<'_>, default: &'a str) -> Self {
862-
Self(sugg::Sugg::hir(cx, expr, default))
863-
}
864-
865-
fn maybe_par(self) -> Self {
866-
Self(self.0.maybe_par())
855+
fn into_sugg(self) -> Sugg<'a> {
856+
self.0
867857
}
858+
}
868859

869-
fn non_paren(str: impl Into<std::borrow::Cow<'a, str>>) -> Self {
870-
Self(sugg::Sugg::NonParen(str.into()))
860+
impl<'a> From<Sugg<'a>> for MinifyingSugg<'a> {
861+
fn from(sugg: Sugg<'a>) -> Self {
862+
Self(sugg)
871863
}
872864
}
873865

@@ -877,7 +869,7 @@ impl std::ops::Add for &MinifyingSugg<'static> {
877869
match (self.as_str(), rhs.as_str()) {
878870
("0", _) => rhs.clone(),
879871
(_, "0") => self.clone(),
880-
(_, _) => MinifyingSugg(&self.0 + &rhs.0),
872+
(_, _) => (&self.0 + &rhs.0).into(),
881873
}
882874
}
883875
}
@@ -887,9 +879,9 @@ impl std::ops::Sub for &MinifyingSugg<'static> {
887879
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
888880
match (self.as_str(), rhs.as_str()) {
889881
(_, "0") => self.clone(),
890-
("0", _) => MinifyingSugg(-(rhs.0.clone())),
891-
(x, y) if x == y => MinifyingSugg::non_paren("0"),
892-
(_, _) => MinifyingSugg(&self.0 - &rhs.0),
882+
("0", _) => (-rhs.0.clone()).into(),
883+
(x, y) if x == y => sugg::ZERO.into(),
884+
(_, _) => (&self.0 - &rhs.0).into(),
893885
}
894886
}
895887
}
@@ -900,7 +892,7 @@ impl std::ops::Add<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
900892
match (self.as_str(), rhs.as_str()) {
901893
("0", _) => rhs.clone(),
902894
(_, "0") => self,
903-
(_, _) => MinifyingSugg(self.0 + &rhs.0),
895+
(_, _) => (self.0 + &rhs.0).into(),
904896
}
905897
}
906898
}
@@ -910,9 +902,9 @@ impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
910902
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
911903
match (self.as_str(), rhs.as_str()) {
912904
(_, "0") => self,
913-
("0", _) => MinifyingSugg(-(rhs.0.clone())),
914-
(x, y) if x == y => MinifyingSugg::non_paren("0"),
915-
(_, _) => MinifyingSugg(self.0 - &rhs.0),
905+
("0", _) => (-rhs.0.clone()).into(),
906+
(x, y) if x == y => sugg::ZERO.into(),
907+
(_, _) => (self.0 - &rhs.0).into(),
916908
}
917909
}
918910
}
@@ -969,19 +961,15 @@ fn get_details_from_idx<'tcx>(
969961
})
970962
}
971963

972-
fn get_offset<'tcx>(
973-
cx: &LateContext<'tcx>,
974-
e: &Expr<'_>,
975-
starts: &[Start<'tcx>],
976-
) -> Option<MinifyingSugg<'static>> {
964+
fn get_offset<'tcx>(cx: &LateContext<'tcx>, e: &Expr<'_>, starts: &[Start<'tcx>]) -> Option<Sugg<'static>> {
977965
match &e.kind {
978966
ExprKind::Lit(l) => match l.node {
979-
ast::LitKind::Int(x, _ty) => Some(MinifyingSugg::non_paren(x.to_string())),
967+
ast::LitKind::Int(x, _ty) => Some(Sugg::NonParen(x.to_string().into())),
980968
_ => None,
981969
},
982970
ExprKind::Path(..) if get_start(cx, e, starts).is_none() => {
983971
// `e` is always non paren as it's a `Path`
984-
Some(MinifyingSugg::non_paren(snippet(cx, e.span, "???")))
972+
Some(Sugg::NonParen(snippet(cx, e.span, "???")))
985973
},
986974
_ => None,
987975
}
@@ -1072,7 +1060,7 @@ fn build_manual_memcpy_suggestion<'tcx>(
10721060
) -> String {
10731061
fn print_offset(offset: MinifyingSugg<'static>) -> MinifyingSugg<'static> {
10741062
if offset.as_str() == "0" {
1075-
MinifyingSugg::non_paren("")
1063+
sugg::EMPTY.into()
10761064
} else {
10771065
offset
10781066
}
@@ -1088,44 +1076,46 @@ fn build_manual_memcpy_suggestion<'tcx>(
10881076
if var_def_id(cx, arg) == var_def_id(cx, base);
10891077
then {
10901078
if sugg.as_str() == end_str {
1091-
MinifyingSugg::non_paren("")
1079+
sugg::EMPTY.into()
10921080
} else {
10931081
sugg
10941082
}
10951083
} else {
10961084
match limits {
10971085
ast::RangeLimits::Closed => {
1098-
sugg + &MinifyingSugg::non_paren("1")
1086+
sugg + &sugg::ONE.into()
10991087
},
11001088
ast::RangeLimits::HalfOpen => sugg,
11011089
}
11021090
}
11031091
}
11041092
};
11051093

1106-
let start_str = MinifyingSugg::hir(cx, start, "");
1107-
let end_str = MinifyingSugg::hir(cx, end, "");
1094+
let start_str = Sugg::hir(cx, start, "").into();
1095+
let end_str: MinifyingSugg<'_> = Sugg::hir(cx, end, "").into();
11081096

11091097
let print_offset_and_limit = |idx_expr: &IndexExpr<'_>| match idx_expr.idx {
11101098
StartKind::Range => (
1111-
print_offset(apply_offset(&start_str, &idx_expr.idx_offset)),
1099+
print_offset(apply_offset(&start_str, &idx_expr.idx_offset)).into_sugg(),
11121100
print_limit(
11131101
end,
11141102
end_str.as_str(),
11151103
idx_expr.base,
11161104
apply_offset(&end_str, &idx_expr.idx_offset),
1117-
),
1105+
)
1106+
.into_sugg(),
11181107
),
11191108
StartKind::Counter { initializer } => {
1120-
let counter_start = MinifyingSugg::hir(cx, initializer, "");
1109+
let counter_start = Sugg::hir(cx, initializer, "").into();
11211110
(
1122-
print_offset(apply_offset(&counter_start, &idx_expr.idx_offset)),
1111+
print_offset(apply_offset(&counter_start, &idx_expr.idx_offset)).into_sugg(),
11231112
print_limit(
11241113
end,
11251114
end_str.as_str(),
11261115
idx_expr.base,
11271116
apply_offset(&end_str, &idx_expr.idx_offset) + &counter_start - &start_str,
1128-
),
1117+
)
1118+
.into_sugg(),
11291119
)
11301120
},
11311121
};
@@ -1136,7 +1126,7 @@ fn build_manual_memcpy_suggestion<'tcx>(
11361126
let dst_base_str = snippet(cx, dst.base.span, "???");
11371127
let src_base_str = snippet(cx, src.base.span, "???");
11381128

1139-
let dst = if dst_offset.as_str() == "" && dst_limit.as_str() == "" {
1129+
let dst = if dst_offset == sugg::EMPTY && dst_limit == sugg::EMPTY {
11401130
dst_base_str
11411131
} else {
11421132
format!(

clippy_lints/src/utils/sugg.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::fmt::Display;
1616
use std::ops::{Add, Neg, Not, Sub};
1717

1818
/// A helper type to build suggestion correctly handling parenthesis.
19-
#[derive(Clone)]
19+
#[derive(Clone, PartialEq)]
2020
pub enum Sugg<'a> {
2121
/// An expression that never needs parenthesis such as `1337` or `[0; 42]`.
2222
NonParen(Cow<'a, str>),
@@ -27,8 +27,12 @@ pub enum Sugg<'a> {
2727
BinOp(AssocOp, Cow<'a, str>),
2828
}
2929

30+
/// Literal constant `0`, for convenience.
31+
pub const ZERO: Sugg<'static> = Sugg::NonParen(Cow::Borrowed("0"));
3032
/// Literal constant `1`, for convenience.
3133
pub const ONE: Sugg<'static> = Sugg::NonParen(Cow::Borrowed("1"));
34+
/// a constant represents an empty string, for convenience.
35+
pub const EMPTY: Sugg<'static> = Sugg::NonParen(Cow::Borrowed(""));
3236

3337
impl Display for Sugg<'_> {
3438
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {

0 commit comments

Comments
 (0)