about summary refs log tree commit diff
diff options
context:
space:
mode:
authornahuakang <kangnahua@gmail.com>2021-02-21 17:10:07 +0100
committerYoshitomo Nakanishi <yurayura.rounin.3@gmail.com>2021-03-02 18:14:20 +0900
commit7cfdef6de19a0aaddcc3018baf1031ae54c419f6 (patch)
treef28c2e995ac1e62e8f6caa8e059d4844a6820ce1
parent7158c944a4f033f6feb57fbcdbf3453333d892b7 (diff)
downloadrust-7cfdef6de19a0aaddcc3018baf1031ae54c419f6.tar.gz
rust-7cfdef6de19a0aaddcc3018baf1031ae54c419f6.zip
Move MinifyingSugg into manual_memcpy
-rw-r--r--clippy_lints/src/loops/manual_memcpy.rs72
-rw-r--r--clippy_lints/src/loops/mod.rs73
2 files changed, 72 insertions, 73 deletions
diff --git a/clippy_lints/src/loops/manual_memcpy.rs b/clippy_lints/src/loops/manual_memcpy.rs
index 85b3dfaece7..13a434aa210 100644
--- a/clippy_lints/src/loops/manual_memcpy.rs
+++ b/clippy_lints/src/loops/manual_memcpy.rs
@@ -1,4 +1,4 @@
-use super::{get_span_of_entire_for_loop, IncrementVisitor, InitializeVisitor, MinifyingSugg};
+use super::{get_span_of_entire_for_loop, IncrementVisitor, InitializeVisitor};
 use crate::utils::sugg::Sugg;
 use crate::utils::{
     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>(
     )
 }
 
+/// a wrapper of `Sugg`. Besides what `Sugg` do, this removes unnecessary `0`;
+/// and also, it avoids subtracting a variable from the same one by replacing it with `0`.
+/// it exists for the convenience of the overloaded operators while normal functions can do the
+/// same.
+#[derive(Clone)]
+struct MinifyingSugg<'a>(Sugg<'a>);
+
+impl<'a> MinifyingSugg<'a> {
+    fn as_str(&self) -> &str {
+        let Sugg::NonParen(s) | Sugg::MaybeParen(s) | Sugg::BinOp(_, s) = &self.0;
+        s.as_ref()
+    }
+
+    fn into_sugg(self) -> Sugg<'a> {
+        self.0
+    }
+}
+
+impl<'a> From<Sugg<'a>> for MinifyingSugg<'a> {
+    fn from(sugg: Sugg<'a>) -> Self {
+        Self(sugg)
+    }
+}
+
+impl std::ops::Add for &MinifyingSugg<'static> {
+    type Output = MinifyingSugg<'static>;
+    fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
+        match (self.as_str(), rhs.as_str()) {
+            ("0", _) => rhs.clone(),
+            (_, "0") => self.clone(),
+            (_, _) => (&self.0 + &rhs.0).into(),
+        }
+    }
+}
+
+impl std::ops::Sub for &MinifyingSugg<'static> {
+    type Output = MinifyingSugg<'static>;
+    fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
+        match (self.as_str(), rhs.as_str()) {
+            (_, "0") => self.clone(),
+            ("0", _) => (-rhs.0.clone()).into(),
+            (x, y) if x == y => sugg::ZERO.into(),
+            (_, _) => (&self.0 - &rhs.0).into(),
+        }
+    }
+}
+
+impl std::ops::Add<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
+    type Output = MinifyingSugg<'static>;
+    fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
+        match (self.as_str(), rhs.as_str()) {
+            ("0", _) => rhs.clone(),
+            (_, "0") => self,
+            (_, _) => (self.0 + &rhs.0).into(),
+        }
+    }
+}
+
+impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
+    type Output = MinifyingSugg<'static>;
+    fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
+        match (self.as_str(), rhs.as_str()) {
+            (_, "0") => self,
+            ("0", _) => (-rhs.0.clone()).into(),
+            (x, y) if x == y => sugg::ZERO.into(),
+            (_, _) => (self.0 - &rhs.0).into(),
+        }
+    }
+}
+
 /// a wrapper around `MinifyingSugg`, which carries a operator like currying
 /// so that the suggested code become more efficient (e.g. `foo + -bar` `foo - bar`).
 struct Offset {
diff --git a/clippy_lints/src/loops/mod.rs b/clippy_lints/src/loops/mod.rs
index 9076e48584a..c0191e43dea 100644
--- a/clippy_lints/src/loops/mod.rs
+++ b/clippy_lints/src/loops/mod.rs
@@ -15,8 +15,7 @@ mod utils;
 mod while_let_loop;
 mod while_let_on_iterator;
 
-use crate::utils::sugg::Sugg;
-use crate::utils::{higher, sugg};
+use crate::utils::higher;
 use rustc_hir::{Expr, ExprKind, LoopSource, Pat};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -596,73 +595,3 @@ fn check_for_loop<'tcx>(
     same_item_push::detect_same_item_push(cx, pat, arg, body, expr);
     manual_flatten::check_manual_flatten(cx, pat, arg, body, span);
 }
-
-/// a wrapper of `Sugg`. Besides what `Sugg` do, this removes unnecessary `0`;
-/// and also, it avoids subtracting a variable from the same one by replacing it with `0`.
-/// it exists for the convenience of the overloaded operators while normal functions can do the
-/// same.
-#[derive(Clone)]
-struct MinifyingSugg<'a>(Sugg<'a>);
-
-impl<'a> MinifyingSugg<'a> {
-    fn as_str(&self) -> &str {
-        let Sugg::NonParen(s) | Sugg::MaybeParen(s) | Sugg::BinOp(_, s) = &self.0;
-        s.as_ref()
-    }
-
-    fn into_sugg(self) -> Sugg<'a> {
-        self.0
-    }
-}
-
-impl<'a> From<Sugg<'a>> for MinifyingSugg<'a> {
-    fn from(sugg: Sugg<'a>) -> Self {
-        Self(sugg)
-    }
-}
-
-impl std::ops::Add for &MinifyingSugg<'static> {
-    type Output = MinifyingSugg<'static>;
-    fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
-        match (self.as_str(), rhs.as_str()) {
-            ("0", _) => rhs.clone(),
-            (_, "0") => self.clone(),
-            (_, _) => (&self.0 + &rhs.0).into(),
-        }
-    }
-}
-
-impl std::ops::Sub for &MinifyingSugg<'static> {
-    type Output = MinifyingSugg<'static>;
-    fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
-        match (self.as_str(), rhs.as_str()) {
-            (_, "0") => self.clone(),
-            ("0", _) => (-rhs.0.clone()).into(),
-            (x, y) if x == y => sugg::ZERO.into(),
-            (_, _) => (&self.0 - &rhs.0).into(),
-        }
-    }
-}
-
-impl std::ops::Add<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
-    type Output = MinifyingSugg<'static>;
-    fn add(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
-        match (self.as_str(), rhs.as_str()) {
-            ("0", _) => rhs.clone(),
-            (_, "0") => self,
-            (_, _) => (self.0 + &rhs.0).into(),
-        }
-    }
-}
-
-impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
-    type Output = MinifyingSugg<'static>;
-    fn sub(self, rhs: &MinifyingSugg<'static>) -> MinifyingSugg<'static> {
-        match (self.as_str(), rhs.as_str()) {
-            (_, "0") => self,
-            ("0", _) => (-rhs.0.clone()).into(),
-            (x, y) if x == y => sugg::ZERO.into(),
-            (_, _) => (self.0 - &rhs.0).into(),
-        }
-    }
-}