about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-06-19 08:41:58 +0000
committerbors <bors@rust-lang.org>2021-06-19 08:41:58 +0000
commit39260f6d4994db191f2fca9f12b4930eb3a3c122 (patch)
treeadc8379e2cca92a99798ad4a57cf6d0664250158
parent9cf05f3614ef00c0da82b0594704aff213ab7c34 (diff)
parent884336eade854919ee17d5e4c56ecc5f9ab6529b (diff)
downloadrust-39260f6d4994db191f2fca9f12b4930eb3a3c122.tar.gz
rust-39260f6d4994db191f2fca9f12b4930eb3a3c122.zip
Auto merge of #86426 - hi-rustin:rustin-patch-lint-warn, r=Aaron1011
Lint for unused borrows as part of UNUSED_MUST_USE

close https://github.com/rust-lang/rust/issues/76264

base on https://github.com/rust-lang/rust/pull/76894

r? `@RalfJung`
-rw-r--r--compiler/rustc_lint/src/unused.rs1
-rw-r--r--compiler/rustc_macros/src/hash_stable.rs4
-rw-r--r--library/alloc/tests/str.rs10
-rw-r--r--library/alloc/tests/vec.rs10
-rw-r--r--library/std/src/sys_common/wtf8/tests.rs6
-rw-r--r--src/test/ui/array-slice-vec/slice-panic-1.rs2
-rw-r--r--src/test/ui/array-slice-vec/slice-panic-2.rs2
-rw-r--r--src/test/ui/array-slice-vec/slice.rs16
-rw-r--r--src/test/ui/const-generics/issues/issue-61432.rs4
-rw-r--r--src/test/ui/dynamically-sized-types/dst-index.rs2
-rw-r--r--src/test/ui/generator/too-live-local-in-immovable-gen.rs2
-rw-r--r--src/test/ui/generator/too-live-local-in-immovable-gen.stderr2
-rw-r--r--src/test/ui/generator/yield-in-initializer.rs2
-rw-r--r--src/test/ui/issues/issue-43205.rs2
-rw-r--r--src/test/ui/issues/issue-5280.rs2
-rw-r--r--src/test/ui/issues/issue-54696.rs4
-rw-r--r--src/test/ui/lint/unused-borrows.rs33
-rw-r--r--src/test/ui/lint/unused-borrows.stderr44
-rw-r--r--src/tools/clippy/tests/ui/bytes_nth.fixed2
-rw-r--r--src/tools/clippy/tests/ui/bytes_nth.rs2
-rw-r--r--src/tools/clippy/tests/ui/bytes_nth.stderr6
-rw-r--r--src/tools/clippy/tests/ui/iter_count.fixed6
-rw-r--r--src/tools/clippy/tests/ui/iter_count.rs6
-rw-r--r--src/tools/clippy/tests/ui/iter_count.stderr18
24 files changed, 133 insertions, 55 deletions
diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs
index 44c2a550c30..c431c048ca0 100644
--- a/compiler/rustc_lint/src/unused.rs
+++ b/compiler/rustc_lint/src/unused.rs
@@ -154,6 +154,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
                 | hir::BinOpKind::Shl
                 | hir::BinOpKind::Shr => Some("bitwise operation"),
             },
+            hir::ExprKind::AddrOf(..) => Some("borrow"),
             hir::ExprKind::Unary(..) => Some("unary operation"),
             _ => None,
         };
diff --git a/compiler/rustc_macros/src/hash_stable.rs b/compiler/rustc_macros/src/hash_stable.rs
index 30569f20793..b916113a0e5 100644
--- a/compiler/rustc_macros/src/hash_stable.rs
+++ b/compiler/rustc_macros/src/hash_stable.rs
@@ -54,7 +54,7 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma
             quote! {}
         } else if let Some(project) = attrs.project {
             quote! {
-                &#bi.#project.hash_stable(__hcx, __hasher);
+                (&#bi.#project).hash_stable(__hcx, __hasher);
             }
         } else {
             quote! {
@@ -96,7 +96,7 @@ pub fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::To
             quote! {}
         } else if let Some(project) = attrs.project {
             quote! {
-                &#bi.#project.hash_stable(__hcx, __hasher);
+                (&#bi.#project).hash_stable(__hcx, __hasher);
             }
         } else {
             quote! {
diff --git a/library/alloc/tests/str.rs b/library/alloc/tests/str.rs
index 6df8d8c2f35..a1e819cf8f9 100644
--- a/library/alloc/tests/str.rs
+++ b/library/alloc/tests/str.rs
@@ -534,7 +534,7 @@ mod slice_index {
     #[test]
     #[should_panic]
     fn test_slice_fail() {
-        &"中华Việt Nam"[0..2];
+        let _ = &"中华Việt Nam"[0..2];
     }
 
     panic_cases! {
@@ -714,13 +714,13 @@ mod slice_index {
     #[test]
     #[should_panic(expected = "byte index 1024 is out of bounds of `Lorem ipsum dolor sit amet")]
     fn test_slice_fail_truncated_1() {
-        &LOREM_PARAGRAPH[..1024];
+        let _ = &LOREM_PARAGRAPH[..1024];
     }
     // check the truncation in the panic message
     #[test]
     #[should_panic(expected = "luctus, im`[...]")]
     fn test_slice_fail_truncated_2() {
-        &LOREM_PARAGRAPH[..1024];
+        let _ = &LOREM_PARAGRAPH[..1024];
     }
 }
 
@@ -735,7 +735,7 @@ fn test_str_slice_rangetoinclusive_ok() {
 #[should_panic]
 fn test_str_slice_rangetoinclusive_notok() {
     let s = "abcαβγ";
-    &s[..=3];
+    let _ = &s[..=3];
 }
 
 #[test]
@@ -751,7 +751,7 @@ fn test_str_slicemut_rangetoinclusive_ok() {
 fn test_str_slicemut_rangetoinclusive_notok() {
     let mut s = "abcαβγ".to_owned();
     let s: &mut str = &mut s;
-    &mut s[..=3];
+    let _ = &mut s[..=3];
 }
 
 #[test]
diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs
index c203cdafecb..3b723701680 100644
--- a/library/alloc/tests/vec.rs
+++ b/library/alloc/tests/vec.rs
@@ -542,35 +542,35 @@ fn test_index_out_of_bounds() {
 #[should_panic]
 fn test_slice_out_of_bounds_1() {
     let x = vec![1, 2, 3, 4, 5];
-    &x[!0..];
+    let _ = &x[!0..];
 }
 
 #[test]
 #[should_panic]
 fn test_slice_out_of_bounds_2() {
     let x = vec![1, 2, 3, 4, 5];
-    &x[..6];
+    let _ = &x[..6];
 }
 
 #[test]
 #[should_panic]
 fn test_slice_out_of_bounds_3() {
     let x = vec![1, 2, 3, 4, 5];
-    &x[!0..4];
+    let _ = &x[!0..4];
 }
 
 #[test]
 #[should_panic]
 fn test_slice_out_of_bounds_4() {
     let x = vec![1, 2, 3, 4, 5];
-    &x[1..6];
+    let _ = &x[1..6];
 }
 
 #[test]
 #[should_panic]
 fn test_slice_out_of_bounds_5() {
     let x = vec![1, 2, 3, 4, 5];
-    &x[3..2];
+    let _ = &x[3..2];
 }
 
 #[test]
diff --git a/library/std/src/sys_common/wtf8/tests.rs b/library/std/src/sys_common/wtf8/tests.rs
index 385e01f92fa..cd9c6ee3250 100644
--- a/library/std/src/sys_common/wtf8/tests.rs
+++ b/library/std/src/sys_common/wtf8/tests.rs
@@ -301,7 +301,7 @@ fn wtf8_slice() {
 #[test]
 #[should_panic]
 fn wtf8_slice_not_code_point_boundary() {
-    &Wtf8::from_str("aé 💩")[2..4];
+    let _ = &Wtf8::from_str("aé 💩")[2..4];
 }
 
 #[test]
@@ -312,7 +312,7 @@ fn wtf8_slice_from() {
 #[test]
 #[should_panic]
 fn wtf8_slice_from_not_code_point_boundary() {
-    &Wtf8::from_str("aé 💩")[2..];
+    let _ = &Wtf8::from_str("aé 💩")[2..];
 }
 
 #[test]
@@ -323,7 +323,7 @@ fn wtf8_slice_to() {
 #[test]
 #[should_panic]
 fn wtf8_slice_to_not_code_point_boundary() {
-    &Wtf8::from_str("aé 💩")[5..];
+    let _ = &Wtf8::from_str("aé 💩")[5..];
 }
 
 #[test]
diff --git a/src/test/ui/array-slice-vec/slice-panic-1.rs b/src/test/ui/array-slice-vec/slice-panic-1.rs
index 8b27d055e2b..4134c623778 100644
--- a/src/test/ui/array-slice-vec/slice-panic-1.rs
+++ b/src/test/ui/array-slice-vec/slice-panic-1.rs
@@ -17,7 +17,7 @@ impl Drop for Foo {
 
 fn foo() {
     let x: &[_] = &[Foo, Foo];
-    &x[3..4];
+    let _ = &x[3..4];
 }
 
 fn main() {
diff --git a/src/test/ui/array-slice-vec/slice-panic-2.rs b/src/test/ui/array-slice-vec/slice-panic-2.rs
index 2ee564cadb3..2f7178fb3e1 100644
--- a/src/test/ui/array-slice-vec/slice-panic-2.rs
+++ b/src/test/ui/array-slice-vec/slice-panic-2.rs
@@ -21,7 +21,7 @@ fn bar() -> usize {
 
 fn foo() {
     let x: &[_] = &[Foo, Foo];
-    &x[3..bar()];
+    let _ = &x[3..bar()];
 }
 
 fn main() {
diff --git a/src/test/ui/array-slice-vec/slice.rs b/src/test/ui/array-slice-vec/slice.rs
index 14e1ddf52eb..a514e202773 100644
--- a/src/test/ui/array-slice-vec/slice.rs
+++ b/src/test/ui/array-slice-vec/slice.rs
@@ -67,14 +67,14 @@ impl IndexMut<RangeFull> for Foo {
 
 fn main() {
     let mut x = Foo;
-    &x[..];
-    &x[Foo..];
-    &x[..Foo];
-    &x[Foo..Foo];
-    &mut x[..];
-    &mut x[Foo..];
-    &mut x[..Foo];
-    &mut x[Foo..Foo];
+    let _ = &x[..];
+    let _ = &x[Foo..];
+    let _ = &x[..Foo];
+    let _ = &x[Foo..Foo];
+    let _ = &mut x[..];
+    let _ = &mut x[Foo..];
+    let _ = &mut x[..Foo];
+    let _ = &mut x[Foo..Foo];
     unsafe {
         assert_eq!(COUNT, 8);
     }
diff --git a/src/test/ui/const-generics/issues/issue-61432.rs b/src/test/ui/const-generics/issues/issue-61432.rs
index 0e228126d77..97ab07dacce 100644
--- a/src/test/ui/const-generics/issues/issue-61432.rs
+++ b/src/test/ui/const-generics/issues/issue-61432.rs
@@ -6,9 +6,9 @@ fn promote<const N: i32>() {
     // works:
     //
     // let n = N;
-    // &n;
+    // let _ = &n;
 
-    &N;
+    let _ = &N;
 }
 
 fn main() {
diff --git a/src/test/ui/dynamically-sized-types/dst-index.rs b/src/test/ui/dynamically-sized-types/dst-index.rs
index 980d99a6d6c..8aa65bbfdc9 100644
--- a/src/test/ui/dynamically-sized-types/dst-index.rs
+++ b/src/test/ui/dynamically-sized-types/dst-index.rs
@@ -29,6 +29,6 @@ impl Index<usize> for T {
 
 fn main() {
     assert_eq!(&S[0], "hello");
-    &T[0];
+    let _ = &T[0];
     // let x = &x as &Debug;
 }
diff --git a/src/test/ui/generator/too-live-local-in-immovable-gen.rs b/src/test/ui/generator/too-live-local-in-immovable-gen.rs
index 7f118c88e5e..e0b856db7a5 100644
--- a/src/test/ui/generator/too-live-local-in-immovable-gen.rs
+++ b/src/test/ui/generator/too-live-local-in-immovable-gen.rs
@@ -15,7 +15,7 @@ fn main() {
                 yield ();
                 4i32
             };
-            &a;
+            let _ = &a;
         };
     }
 }
diff --git a/src/test/ui/generator/too-live-local-in-immovable-gen.stderr b/src/test/ui/generator/too-live-local-in-immovable-gen.stderr
index 88dacff7b55..72a2bd4ebc5 100644
--- a/src/test/ui/generator/too-live-local-in-immovable-gen.stderr
+++ b/src/test/ui/generator/too-live-local-in-immovable-gen.stderr
@@ -6,7 +6,7 @@ LL | |             // Tests that the generator transformation finds out that `a`
 LL | |             // during the yield expression. Type checking will also compute liveness
 LL | |             // and it should also find out that `a` is not live.
 ...  |
-LL | |             &a;
+LL | |             let _ = &a;
 LL | |         };
    | |__________^
    |
diff --git a/src/test/ui/generator/yield-in-initializer.rs b/src/test/ui/generator/yield-in-initializer.rs
index 2f8754c9571..0cab36e5f28 100644
--- a/src/test/ui/generator/yield-in-initializer.rs
+++ b/src/test/ui/generator/yield-in-initializer.rs
@@ -11,7 +11,7 @@ fn main() {
                 yield;
                 true
             };
-            &opt;
+            let _ = &opt;
         }
     };
 }
diff --git a/src/test/ui/issues/issue-43205.rs b/src/test/ui/issues/issue-43205.rs
index 894a61f3eff..f47d5a347bb 100644
--- a/src/test/ui/issues/issue-43205.rs
+++ b/src/test/ui/issues/issue-43205.rs
@@ -1,5 +1,5 @@
 // run-pass
 fn main() {
-   &&[()][0];
+   let _ = &&[()][0];
    println!("{:?}", &[(),()][1]);
 }
diff --git a/src/test/ui/issues/issue-5280.rs b/src/test/ui/issues/issue-5280.rs
index 3c97dad6b14..5c5ce6c987a 100644
--- a/src/test/ui/issues/issue-5280.rs
+++ b/src/test/ui/issues/issue-5280.rs
@@ -9,7 +9,7 @@ trait FontTableTagConversions {
 
 impl FontTableTagConversions for FontTableTag {
   fn tag_to_string(self) {
-    &self;
+      let _ = &self;
   }
 }
 
diff --git a/src/test/ui/issues/issue-54696.rs b/src/test/ui/issues/issue-54696.rs
index d8408ed8549..15355d30db6 100644
--- a/src/test/ui/issues/issue-54696.rs
+++ b/src/test/ui/issues/issue-54696.rs
@@ -2,7 +2,7 @@
 
 fn main() {
     // We shouldn't promote this
-    &(main as fn() == main as fn());
+    let _ = &(main as fn() == main as fn());
     // Also check nested case
-    &(&(main as fn()) == &(main as fn()));
+    let _ = &(&(main as fn()) == &(main as fn()));
 }
diff --git a/src/test/ui/lint/unused-borrows.rs b/src/test/ui/lint/unused-borrows.rs
new file mode 100644
index 00000000000..4518522ae00
--- /dev/null
+++ b/src/test/ui/lint/unused-borrows.rs
@@ -0,0 +1,33 @@
+#![deny(unused_must_use)]
+
+fn foo(_: i32) -> bool { todo!() }
+
+fn bar() -> &'static i32 {
+    &42;
+    //~^ unused
+
+    &mut foo(42);
+    //~^ unused
+
+    &&42;
+    //~^ unused
+
+    &&mut 42;
+    //~^ unused
+
+    &mut &42;
+    //~^ unused
+
+    let _result = foo(4)
+        && foo(2); // Misplaced semi-colon (perhaps due to reordering of lines)
+    && foo(42);
+    //~^ unused
+
+    let _ = &42; // ok
+
+    &42 // ok
+}
+
+fn main() {
+    let _ = bar();
+}
diff --git a/src/test/ui/lint/unused-borrows.stderr b/src/test/ui/lint/unused-borrows.stderr
new file mode 100644
index 00000000000..24899fe992b
--- /dev/null
+++ b/src/test/ui/lint/unused-borrows.stderr
@@ -0,0 +1,44 @@
+error: unused borrow that must be used
+  --> $DIR/unused-borrows.rs:6:5
+   |
+LL |     &42;
+   |     ^^^
+   |
+note: the lint level is defined here
+  --> $DIR/unused-borrows.rs:1:9
+   |
+LL | #![deny(unused_must_use)]
+   |         ^^^^^^^^^^^^^^^
+
+error: unused borrow that must be used
+  --> $DIR/unused-borrows.rs:9:5
+   |
+LL |     &mut foo(42);
+   |     ^^^^^^^^^^^^
+
+error: unused borrow that must be used
+  --> $DIR/unused-borrows.rs:12:5
+   |
+LL |     &&42;
+   |     ^^^^
+
+error: unused borrow that must be used
+  --> $DIR/unused-borrows.rs:15:5
+   |
+LL |     &&mut 42;
+   |     ^^^^^^^^
+
+error: unused borrow that must be used
+  --> $DIR/unused-borrows.rs:18:5
+   |
+LL |     &mut &42;
+   |     ^^^^^^^^
+
+error: unused borrow that must be used
+  --> $DIR/unused-borrows.rs:23:5
+   |
+LL |     && foo(42);
+   |     ^^^^^^^^^^
+
+error: aborting due to 6 previous errors
+
diff --git a/src/tools/clippy/tests/ui/bytes_nth.fixed b/src/tools/clippy/tests/ui/bytes_nth.fixed
index bf68a7bbbf1..46b7833f428 100644
--- a/src/tools/clippy/tests/ui/bytes_nth.fixed
+++ b/src/tools/clippy/tests/ui/bytes_nth.fixed
@@ -6,6 +6,6 @@
 fn main() {
     let s = String::from("String");
     s.as_bytes().get(3);
-    &s.as_bytes().get(3);
+    let _ = &s.as_bytes().get(3);
     s[..].as_bytes().get(3);
 }
diff --git a/src/tools/clippy/tests/ui/bytes_nth.rs b/src/tools/clippy/tests/ui/bytes_nth.rs
index 629812cc02c..c5e983d4d4e 100644
--- a/src/tools/clippy/tests/ui/bytes_nth.rs
+++ b/src/tools/clippy/tests/ui/bytes_nth.rs
@@ -6,6 +6,6 @@
 fn main() {
     let s = String::from("String");
     s.bytes().nth(3);
-    &s.bytes().nth(3);
+    let _ = &s.bytes().nth(3);
     s[..].bytes().nth(3);
 }
diff --git a/src/tools/clippy/tests/ui/bytes_nth.stderr b/src/tools/clippy/tests/ui/bytes_nth.stderr
index 9a5742928cd..536decf5e7f 100644
--- a/src/tools/clippy/tests/ui/bytes_nth.stderr
+++ b/src/tools/clippy/tests/ui/bytes_nth.stderr
@@ -7,10 +7,10 @@ LL |     s.bytes().nth(3);
    = note: `-D clippy::bytes-nth` implied by `-D warnings`
 
 error: called `.byte().nth()` on a `String`
-  --> $DIR/bytes_nth.rs:9:6
+  --> $DIR/bytes_nth.rs:9:14
    |
-LL |     &s.bytes().nth(3);
-   |      ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
+LL |     let _ = &s.bytes().nth(3);
+   |              ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
 
 error: called `.byte().nth()` on a `str`
   --> $DIR/bytes_nth.rs:10:5
diff --git a/src/tools/clippy/tests/ui/iter_count.fixed b/src/tools/clippy/tests/ui/iter_count.fixed
index b11dadda6c2..97c5929783d 100644
--- a/src/tools/clippy/tests/ui/iter_count.fixed
+++ b/src/tools/clippy/tests/ui/iter_count.fixed
@@ -50,7 +50,7 @@ fn main() {
     linked_list.push_back(1);
     binary_heap.push(1);
 
-    &vec[..].len();
+    let _ = &vec[..].len();
     vec.len();
     boxed_slice.len();
     vec_deque.len();
@@ -62,13 +62,13 @@ fn main() {
     binary_heap.len();
 
     vec.len();
-    &vec[..].len();
+    let _ = &vec[..].len();
     vec_deque.len();
     hash_map.len();
     b_tree_map.len();
     linked_list.len();
 
-    &vec[..].len();
+    let _ = &vec[..].len();
     vec.len();
     vec_deque.len();
     hash_set.len();
diff --git a/src/tools/clippy/tests/ui/iter_count.rs b/src/tools/clippy/tests/ui/iter_count.rs
index 7d49c6a3dbb..70bb734763f 100644
--- a/src/tools/clippy/tests/ui/iter_count.rs
+++ b/src/tools/clippy/tests/ui/iter_count.rs
@@ -50,7 +50,7 @@ fn main() {
     linked_list.push_back(1);
     binary_heap.push(1);
 
-    &vec[..].iter().count();
+    let _ = &vec[..].iter().count();
     vec.iter().count();
     boxed_slice.iter().count();
     vec_deque.iter().count();
@@ -62,13 +62,13 @@ fn main() {
     binary_heap.iter().count();
 
     vec.iter_mut().count();
-    &vec[..].iter_mut().count();
+    let _ = &vec[..].iter_mut().count();
     vec_deque.iter_mut().count();
     hash_map.iter_mut().count();
     b_tree_map.iter_mut().count();
     linked_list.iter_mut().count();
 
-    &vec[..].into_iter().count();
+    let _ = &vec[..].into_iter().count();
     vec.into_iter().count();
     vec_deque.into_iter().count();
     hash_set.into_iter().count();
diff --git a/src/tools/clippy/tests/ui/iter_count.stderr b/src/tools/clippy/tests/ui/iter_count.stderr
index f3fb98e65b9..1d2c22f9dfa 100644
--- a/src/tools/clippy/tests/ui/iter_count.stderr
+++ b/src/tools/clippy/tests/ui/iter_count.stderr
@@ -1,8 +1,8 @@
 error: called `.iter().count()` on a `slice`
-  --> $DIR/iter_count.rs:53:6
+  --> $DIR/iter_count.rs:53:14
    |
-LL |     &vec[..].iter().count();
-   |      ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()`
+LL |     let _ = &vec[..].iter().count();
+   |              ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()`
    |
    = note: `-D clippy::iter-count` implied by `-D warnings`
 
@@ -67,10 +67,10 @@ LL |     vec.iter_mut().count();
    |     ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.len()`
 
 error: called `.iter_mut().count()` on a `slice`
-  --> $DIR/iter_count.rs:65:6
+  --> $DIR/iter_count.rs:65:14
    |
-LL |     &vec[..].iter_mut().count();
-   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()`
+LL |     let _ = &vec[..].iter_mut().count();
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()`
 
 error: called `.iter_mut().count()` on a `VecDeque`
   --> $DIR/iter_count.rs:66:5
@@ -97,10 +97,10 @@ LL |     linked_list.iter_mut().count();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()`
 
 error: called `.into_iter().count()` on a `slice`
-  --> $DIR/iter_count.rs:71:6
+  --> $DIR/iter_count.rs:71:14
    |
-LL |     &vec[..].into_iter().count();
-   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()`
+LL |     let _ = &vec[..].into_iter().count();
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()`
 
 error: called `.into_iter().count()` on a `Vec`
   --> $DIR/iter_count.rs:72:5