Skip to content

Commit 7cfdef6

Browse files
nahuakangY-Nak
authored andcommitted
Move MinifyingSugg into manual_memcpy
1 parent 7158c94 commit 7cfdef6

File tree

2 files changed

+72
-73
lines changed

2 files changed

+72
-73
lines changed

clippy_lints/src/loops/manual_memcpy.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{get_span_of_entire_for_loop, IncrementVisitor, InitializeVisitor, MinifyingSugg};
1+
use super::{get_span_of_entire_for_loop, IncrementVisitor, InitializeVisitor};
22
use crate::utils::sugg::Sugg;
33
use crate::utils::{
44
get_enclosing_block, higher, is_type_diagnostic_item, path_to_local, snippet, span_lint_and_sugg, sugg,
@@ -194,6 +194,76 @@ fn build_manual_memcpy_suggestion<'tcx>(
194194
)
195195
}
196196

197+
/// a wrapper of `Sugg`. Besides what `Sugg` do, this removes unnecessary `0`;
198+
/// and also, it avoids subtracting a variable from the same one by replacing it with `0`.
199+
/// it exists for the convenience of the overloaded operators while normal functions can do the
200+
/// same.
201+
#[derive(Clone)]
202+
struct MinifyingSugg<'a>(Sugg<'a>);
203+
204+
impl<'a> MinifyingSugg<'a> {
205+
fn as_str(&self) -> &str {
206+
let Sugg::NonParen(s) | Sugg::MaybeParen(s) | Sugg::BinOp(_, s) = &self.0;
207+
s.as_ref()
208+
}
209+
210+
fn into_sugg(self) -> Sugg<'a> {
211+
self.0
212+
}
213+
}
214+
215+
impl<'a> From<Sugg<'a>> for MinifyingSugg<'a> {
216+
fn from(sugg: Sugg<'a>) -> Self {
217+
Self(sugg)
218+
}
219+
}
220+
221+
impl std::ops::Add for &MinifyingSugg<'static> {
222+
type Output = MinifyingSugg<'static>;
223+
fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
224+
match (self.as_str(), rhs.as_str()) {
225+
("0", _) => rhs.clone(),
226+
(_, "0") => self.clone(),
227+
(_, _) => (&self.0 + &rhs.0).into(),
228+
}
229+
}
230+
}
231+
232+
impl std::ops::Sub for &MinifyingSugg<'static> {
233+
type Output = MinifyingSugg<'static>;
234+
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
235+
match (self.as_str(), rhs.as_str()) {
236+
(_, "0") => self.clone(),
237+
("0", _) => (-rhs.0.clone()).into(),
238+
(x, y) if x == y => sugg::ZERO.into(),
239+
(_, _) => (&self.0 - &rhs.0).into(),
240+
}
241+
}
242+
}
243+
244+
impl std::ops::Add<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
245+
type Output = MinifyingSugg<'static>;
246+
fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
247+
match (self.as_str(), rhs.as_str()) {
248+
("0", _) => rhs.clone(),
249+
(_, "0") => self,
250+
(_, _) => (self.0 + &rhs.0).into(),
251+
}
252+
}
253+
}
254+
255+
impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
256+
type Output = MinifyingSugg<'static>;
257+
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
258+
match (self.as_str(), rhs.as_str()) {
259+
(_, "0") => self,
260+
("0", _) => (-rhs.0.clone()).into(),
261+
(x, y) if x == y => sugg::ZERO.into(),
262+
(_, _) => (self.0 - &rhs.0).into(),
263+
}
264+
}
265+
}
266+
197267
/// a wrapper around `MinifyingSugg`, which carries a operator like currying
198268
/// so that the suggested code become more efficient (e.g. `foo + -bar` `foo - bar`).
199269
struct Offset {

clippy_lints/src/loops/mod.rs

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ mod utils;
1515
mod while_let_loop;
1616
mod while_let_on_iterator;
1717

18-
use crate::utils::sugg::Sugg;
19-
use crate::utils::{higher, sugg};
18+
use crate::utils::higher;
2019
use rustc_hir::{Expr, ExprKind, LoopSource, Pat};
2120
use rustc_lint::{LateContext, LateLintPass};
2221
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -596,73 +595,3 @@ fn check_for_loop<'tcx>(
596595
same_item_push::detect_same_item_push(cx, pat, arg, body, expr);
597596
manual_flatten::check_manual_flatten(cx, pat, arg, body, span);
598597
}
599-
600-
/// a wrapper of `Sugg`. Besides what `Sugg` do, this removes unnecessary `0`;
601-
/// and also, it avoids subtracting a variable from the same one by replacing it with `0`.
602-
/// it exists for the convenience of the overloaded operators while normal functions can do the
603-
/// same.
604-
#[derive(Clone)]
605-
struct MinifyingSugg<'a>(Sugg<'a>);
606-
607-
impl<'a> MinifyingSugg<'a> {
608-
fn as_str(&self) -> &str {
609-
let Sugg::NonParen(s) | Sugg::MaybeParen(s) | Sugg::BinOp(_, s) = &self.0;
610-
s.as_ref()
611-
}
612-
613-
fn into_sugg(self) -> Sugg<'a> {
614-
self.0
615-
}
616-
}
617-
618-
impl<'a> From<Sugg<'a>> for MinifyingSugg<'a> {
619-
fn from(sugg: Sugg<'a>) -> Self {
620-
Self(sugg)
621-
}
622-
}
623-
624-
impl std::ops::Add for &MinifyingSugg<'static> {
625-
type Output = MinifyingSugg<'static>;
626-
fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
627-
match (self.as_str(), rhs.as_str()) {
628-
("0", _) => rhs.clone(),
629-
(_, "0") => self.clone(),
630-
(_, _) => (&self.0 + &rhs.0).into(),
631-
}
632-
}
633-
}
634-
635-
impl std::ops::Sub for &MinifyingSugg<'static> {
636-
type Output = MinifyingSugg<'static>;
637-
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
638-
match (self.as_str(), rhs.as_str()) {
639-
(_, "0") => self.clone(),
640-
("0", _) => (-rhs.0.clone()).into(),
641-
(x, y) if x == y => sugg::ZERO.into(),
642-
(_, _) => (&self.0 - &rhs.0).into(),
643-
}
644-
}
645-
}
646-
647-
impl std::ops::Add<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
648-
type Output = MinifyingSugg<'static>;
649-
fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
650-
match (self.as_str(), rhs.as_str()) {
651-
("0", _) => rhs.clone(),
652-
(_, "0") => self,
653-
(_, _) => (self.0 + &rhs.0).into(),
654-
}
655-
}
656-
}
657-
658-
impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
659-
type Output = MinifyingSugg<'static>;
660-
fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
661-
match (self.as_str(), rhs.as_str()) {
662-
(_, "0") => self,
663-
("0", _) => (-rhs.0.clone()).into(),
664-
(x, y) if x == y => sugg::ZERO.into(),
665-
(_, _) => (self.0 - &rhs.0).into(),
666-
}
667-
}
668-
}

0 commit comments

Comments
 (0)