Skip to content

Commit 3f1e51b

Browse files
committed
Rename negate to sign and make it strong types then make art1 &str
1 parent 7dd0f34 commit 3f1e51b

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

clippy_lints/src/loops.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -786,20 +786,29 @@ fn same_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr<'_>, var: HirId) -
786786
}
787787
}
788788

789+
#[derive(Clone, Copy)]
790+
enum OffsetSign {
791+
Positive,
792+
Negative,
793+
}
794+
789795
struct Offset {
790796
value: String,
791-
negate: bool,
797+
sign: OffsetSign,
792798
}
793799

794800
impl Offset {
795-
fn negative(s: String) -> Self {
796-
Self { value: s, negate: true }
801+
fn negative(value: String) -> Self {
802+
Self {
803+
value,
804+
sign: OffsetSign::Negative,
805+
}
797806
}
798807

799-
fn positive(s: String) -> Self {
808+
fn positive(value: String) -> Self {
800809
Self {
801-
value: s,
802-
negate: false,
810+
value,
811+
sign: OffsetSign::Positive,
803812
}
804813
}
805814
}
@@ -949,31 +958,23 @@ fn detect_manual_memcpy<'a, 'tcx>(
949958
{
950959
// the var must be a single name
951960
if let PatKind::Binding(_, canonical_id, _, _) = pat.kind {
952-
let print_sum = |arg1: &Offset, arg2: &Offset| -> String {
953-
match (&arg1.value[..], arg1.negate, &arg2.value[..], arg2.negate) {
954-
("0", _, "0", _) => "0".into(),
955-
("0", _, x, false) | (x, false, "0", _) => x.into(),
956-
("0", _, x, true) => format!("-{}", x),
957-
(x, false, y, false) => format!("({} + {})", x, y),
958-
(x, false, y, true) => {
961+
let print_sum = |arg1: &str, arg2: &Offset| -> String {
962+
match (arg1, &arg2.value[..], arg2.sign) {
963+
("0", "0", _) => "0".into(),
964+
("0", x, OffsetSign::Positive) | (x, "0", _) => x.into(),
965+
("0", x, OffsetSign::Negative) => format!("-{}", x),
966+
(x, y, OffsetSign::Positive) => format!("({} + {})", x, y),
967+
(x, y, OffsetSign::Negative) => {
959968
if x == y {
960969
"0".into()
961970
} else {
962971
format!("({} - {})", x, y)
963972
}
964973
},
965-
(x, true, y, false) => {
966-
if x == y {
967-
"0".into()
968-
} else {
969-
format!("({} - {})", y, x)
970-
}
971-
},
972-
(x, true, y, true) => format!("-({} + {})", x, y),
973974
}
974975
};
975976

976-
let print_offset = |start_str: &Offset, inline_offset: &Offset| -> String {
977+
let print_offset = |start_str: &str, inline_offset: &Offset| -> String {
977978
let offset = print_sum(start_str, inline_offset);
978979
if offset.as_str() == "0" {
979980
"".into()
@@ -990,10 +991,9 @@ fn detect_manual_memcpy<'a, 'tcx>(
990991
if let Some(arg) = len_args.get(0);
991992
if snippet(cx, arg.span, "??") == var_name;
992993
then {
993-
if offset.negate {
994-
format!("({} - {})", snippet(cx, end.span, "<src>.len()"), offset.value)
995-
} else {
996-
String::new()
994+
match offset.sign {
995+
OffsetSign::Negative => format!("({} - {})", snippet(cx, end.span, "<src>.len()"), offset.value),
996+
OffsetSign::Positive => "".into(),
997997
}
998998
} else {
999999
let end_str = match limits {
@@ -1004,7 +1004,7 @@ fn detect_manual_memcpy<'a, 'tcx>(
10041004
ast::RangeLimits::HalfOpen => format!("{}", snippet(cx, end.span, "..")),
10051005
};
10061006

1007-
print_sum(&Offset::positive(end_str), &offset)
1007+
print_sum(&end_str, &offset)
10081008
}
10091009
}
10101010
};
@@ -1016,7 +1016,7 @@ fn detect_manual_memcpy<'a, 'tcx>(
10161016
let big_sugg = manual_copies
10171017
.into_iter()
10181018
.map(|(dst_var, src_var)| {
1019-
let start_str = Offset::positive(snippet(cx, start.span, "").to_string());
1019+
let start_str = snippet(cx, start.span, "").to_string();
10201020
let dst_offset = print_offset(&start_str, &dst_var.offset);
10211021
let dst_limit = print_limit(end, dst_var.offset, &dst_var.var_name);
10221022
let src_offset = print_offset(&start_str, &src_var.offset);

0 commit comments

Comments
 (0)