]> git.lizzy.rs Git - rust.git/blobdiff - src/tools/clippy/tests/ui/needless_borrow.rs
Auto merge of #103450 - cjgillot:elision-nodedup, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / tests / ui / needless_borrow.rs
index d41251e8f6aac604f0ebd1e9bdc0db9474f2ba91..c93711ac8e28490feb37a078d14f66da07e904a7 100644 (file)
@@ -3,7 +3,11 @@
 
 #[warn(clippy::all, clippy::needless_borrow)]
 #[allow(unused_variables)]
-#[allow(clippy::uninlined_format_args, clippy::unnecessary_mut_passed)]
+#[allow(
+    clippy::uninlined_format_args,
+    clippy::unnecessary_mut_passed,
+    clippy::unnecessary_to_owned
+)]
 fn main() {
     let a = 5;
     let ref_a = &a;
@@ -134,6 +138,7 @@ fn from(s: &S) -> Self {
     multiple_constraints(&[[""]]);
     multiple_constraints_normalizes_to_same(&X, X);
     let _ = Some("").unwrap_or(&"");
+    let _ = std::fs::write("x", &"".to_string());
 
     only_sized(&""); // Don't lint. `Sized` is only bound
     let _ = std::any::Any::type_id(&""); // Don't lint. `Any` is only bound
@@ -276,6 +281,7 @@ fn takes_iter(_: impl Iterator) {}
     fn dont_warn(mut x: Iter) {
         takes_iter(&mut x);
     }
+    #[allow(unused_mut)]
     fn warn(mut x: &mut Iter) {
         takes_iter(&mut x)
     }
@@ -327,3 +333,55 @@ union Ocean {
         ManuallyDrop::drop(&mut ocean.coral);
     }
 }
+
+#[allow(dead_code)]
+fn closure_test() {
+    let env = "env".to_owned();
+    let arg = "arg".to_owned();
+    let f = |arg| {
+        let loc = "loc".to_owned();
+        let _ = std::fs::write("x", &env); // Don't lint. In environment
+        let _ = std::fs::write("x", &arg);
+        let _ = std::fs::write("x", &loc);
+    };
+    let _ = std::fs::write("x", &env); // Don't lint. Borrowed by `f`
+    f(arg);
+}
+
+#[allow(dead_code)]
+mod significant_drop {
+    #[derive(Debug)]
+    struct X;
+
+    #[derive(Debug)]
+    struct Y;
+
+    impl Drop for Y {
+        fn drop(&mut self) {}
+    }
+
+    fn foo(x: X, y: Y) {
+        debug(&x);
+        debug(&y); // Don't lint. Has significant drop
+    }
+
+    fn debug(_: impl std::fmt::Debug) {}
+}
+
+#[allow(dead_code)]
+mod used_exactly_once {
+    fn foo(x: String) {
+        use_x(&x);
+    }
+    fn use_x(_: impl AsRef<str>) {}
+}
+
+#[allow(dead_code)]
+mod used_more_than_once {
+    fn foo(x: String) {
+        use_x(&x);
+        use_x_again(&x);
+    }
+    fn use_x(_: impl AsRef<str>) {}
+    fn use_x_again(_: impl AsRef<str>) {}
+}