Skip to content

Commit 3883841

Browse files
committed
document MinifyingSugg and Offset
...and also swap their position
1 parent ec94bd6 commit 3883841

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

clippy_lints/src/loops.rs

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -805,44 +805,10 @@ fn same_var<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, var: HirId) -> bool {
805805
}
806806
}
807807

808-
#[derive(Clone, Copy)]
809-
enum OffsetSign {
810-
Positive,
811-
Negative,
812-
}
813-
814-
struct Offset {
815-
value: MinifyingSugg<'static>,
816-
sign: OffsetSign,
817-
}
818-
819-
impl Offset {
820-
fn negative(value: Sugg<'static>) -> Self {
821-
Self {
822-
value: value.into(),
823-
sign: OffsetSign::Negative,
824-
}
825-
}
826-
827-
fn positive(value: Sugg<'static>) -> Self {
828-
Self {
829-
value: value.into(),
830-
sign: OffsetSign::Positive,
831-
}
832-
}
833-
834-
fn empty() -> Self {
835-
Self::positive(sugg::ZERO)
836-
}
837-
}
838-
839-
fn apply_offset(lhs: &MinifyingSugg<'static>, rhs: &Offset) -> MinifyingSugg<'static> {
840-
match rhs.sign {
841-
OffsetSign::Positive => lhs + &rhs.value,
842-
OffsetSign::Negative => lhs - &rhs.value,
843-
}
844-
}
845-
808+
/// a wrapper of `Sugg`. Besides what `Sugg` do, this removes unnecessary `0`;
809+
/// and also, it avoids subtracting a variable from the same one by replacing it with `0`.
810+
/// it exists for the convenience of the overloaded operators while normal functions can do the
811+
/// same.
846812
#[derive(Clone)]
847813
struct MinifyingSugg<'a>(Sugg<'a>);
848814

@@ -909,6 +875,46 @@ impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
909875
}
910876
}
911877

878+
/// a wrapper around `MinifyingSugg`, which carries a operator like currying
879+
/// so that the suggested code become more efficient (e.g. `foo + -bar` `foo - bar`).
880+
struct Offset {
881+
value: MinifyingSugg<'static>,
882+
sign: OffsetSign,
883+
}
884+
885+
#[derive(Clone, Copy)]
886+
enum OffsetSign {
887+
Positive,
888+
Negative,
889+
}
890+
891+
impl Offset {
892+
fn negative(value: Sugg<'static>) -> Self {
893+
Self {
894+
value: value.into(),
895+
sign: OffsetSign::Negative,
896+
}
897+
}
898+
899+
fn positive(value: Sugg<'static>) -> Self {
900+
Self {
901+
value: value.into(),
902+
sign: OffsetSign::Positive,
903+
}
904+
}
905+
906+
fn empty() -> Self {
907+
Self::positive(sugg::ZERO)
908+
}
909+
}
910+
911+
fn apply_offset(lhs: &MinifyingSugg<'static>, rhs: &Offset) -> MinifyingSugg<'static> {
912+
match rhs.sign {
913+
OffsetSign::Positive => lhs + &rhs.value,
914+
OffsetSign::Negative => lhs - &rhs.value,
915+
}
916+
}
917+
912918
#[derive(Debug, Clone, Copy)]
913919
enum StartKind<'hir> {
914920
Range,

0 commit comments

Comments
 (0)