Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 88abd7d

Browse files
committed
Lint for unused borrows as part of UNUSED_MUST_USE
1 parent 1a46283 commit 88abd7d

File tree

18 files changed

+113
-35
lines changed

18 files changed

+113
-35
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
154154
| hir::BinOpKind::Shl
155155
| hir::BinOpKind::Shr => Some("bitwise operation"),
156156
},
157+
hir::ExprKind::AddrOf(..) => Some("borrow"),
157158
hir::ExprKind::Unary(..) => Some("unary operation"),
158159
_ => None,
159160
};

compiler/rustc_macros/src/hash_stable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma
5454
quote! {}
5555
} else if let Some(project) = attrs.project {
5656
quote! {
57-
&#bi.#project.hash_stable(__hcx, __hasher);
57+
(&#bi.#project).hash_stable(__hcx, __hasher);
5858
}
5959
} else {
6060
quote! {
@@ -96,7 +96,7 @@ pub fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::To
9696
quote! {}
9797
} else if let Some(project) = attrs.project {
9898
quote! {
99-
&#bi.#project.hash_stable(__hcx, __hasher);
99+
(&#bi.#project).hash_stable(__hcx, __hasher);
100100
}
101101
} else {
102102
quote! {

library/alloc/tests/str.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ mod slice_index {
534534
#[test]
535535
#[should_panic]
536536
fn test_slice_fail() {
537-
&"中华Việt Nam"[0..2];
537+
let _ = &"中华Việt Nam"[0..2];
538538
}
539539

540540
panic_cases! {
@@ -714,13 +714,13 @@ mod slice_index {
714714
#[test]
715715
#[should_panic(expected = "byte index 1024 is out of bounds of `Lorem ipsum dolor sit amet")]
716716
fn test_slice_fail_truncated_1() {
717-
&LOREM_PARAGRAPH[..1024];
717+
let _ = &LOREM_PARAGRAPH[..1024];
718718
}
719719
// check the truncation in the panic message
720720
#[test]
721721
#[should_panic(expected = "luctus, im`[...]")]
722722
fn test_slice_fail_truncated_2() {
723-
&LOREM_PARAGRAPH[..1024];
723+
let _ = &LOREM_PARAGRAPH[..1024];
724724
}
725725
}
726726

@@ -735,7 +735,7 @@ fn test_str_slice_rangetoinclusive_ok() {
735735
#[should_panic]
736736
fn test_str_slice_rangetoinclusive_notok() {
737737
let s = "abcαβγ";
738-
&s[..=3];
738+
let _ = &s[..=3];
739739
}
740740

741741
#[test]
@@ -751,7 +751,7 @@ fn test_str_slicemut_rangetoinclusive_ok() {
751751
fn test_str_slicemut_rangetoinclusive_notok() {
752752
let mut s = "abcαβγ".to_owned();
753753
let s: &mut str = &mut s;
754-
&mut s[..=3];
754+
let _ = &mut s[..=3];
755755
}
756756

757757
#[test]

library/alloc/tests/vec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,35 +542,35 @@ fn test_index_out_of_bounds() {
542542
#[should_panic]
543543
fn test_slice_out_of_bounds_1() {
544544
let x = vec![1, 2, 3, 4, 5];
545-
&x[!0..];
545+
let _ = &x[!0..];
546546
}
547547

548548
#[test]
549549
#[should_panic]
550550
fn test_slice_out_of_bounds_2() {
551551
let x = vec![1, 2, 3, 4, 5];
552-
&x[..6];
552+
let _ = &x[..6];
553553
}
554554

555555
#[test]
556556
#[should_panic]
557557
fn test_slice_out_of_bounds_3() {
558558
let x = vec![1, 2, 3, 4, 5];
559-
&x[!0..4];
559+
let _ = &x[!0..4];
560560
}
561561

562562
#[test]
563563
#[should_panic]
564564
fn test_slice_out_of_bounds_4() {
565565
let x = vec![1, 2, 3, 4, 5];
566-
&x[1..6];
566+
let _ = &x[1..6];
567567
}
568568

569569
#[test]
570570
#[should_panic]
571571
fn test_slice_out_of_bounds_5() {
572572
let x = vec![1, 2, 3, 4, 5];
573-
&x[3..2];
573+
let _ = &x[3..2];
574574
}
575575

576576
#[test]

library/std/src/sys_common/wtf8/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ fn wtf8_slice() {
301301
#[test]
302302
#[should_panic]
303303
fn wtf8_slice_not_code_point_boundary() {
304-
&Wtf8::from_str("aé 💩")[2..4];
304+
let _ = &Wtf8::from_str("aé 💩")[2..4];
305305
}
306306

307307
#[test]
@@ -312,7 +312,7 @@ fn wtf8_slice_from() {
312312
#[test]
313313
#[should_panic]
314314
fn wtf8_slice_from_not_code_point_boundary() {
315-
&Wtf8::from_str("aé 💩")[2..];
315+
let _ = &Wtf8::from_str("aé 💩")[2..];
316316
}
317317

318318
#[test]
@@ -323,7 +323,7 @@ fn wtf8_slice_to() {
323323
#[test]
324324
#[should_panic]
325325
fn wtf8_slice_to_not_code_point_boundary() {
326-
&Wtf8::from_str("aé 💩")[5..];
326+
let _ = &Wtf8::from_str("aé 💩")[5..];
327327
}
328328

329329
#[test]

src/test/ui/array-slice-vec/slice-panic-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Drop for Foo {
1717

1818
fn foo() {
1919
let x: &[_] = &[Foo, Foo];
20-
&x[3..4];
20+
let _ = &x[3..4];
2121
}
2222

2323
fn main() {

src/test/ui/array-slice-vec/slice-panic-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn bar() -> usize {
2121

2222
fn foo() {
2323
let x: &[_] = &[Foo, Foo];
24-
&x[3..bar()];
24+
let _ = &x[3..bar()];
2525
}
2626

2727
fn main() {

src/test/ui/array-slice-vec/slice.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ impl IndexMut<RangeFull> for Foo {
6767

6868
fn main() {
6969
let mut x = Foo;
70-
&x[..];
71-
&x[Foo..];
72-
&x[..Foo];
73-
&x[Foo..Foo];
74-
&mut x[..];
75-
&mut x[Foo..];
76-
&mut x[..Foo];
77-
&mut x[Foo..Foo];
70+
let _ = &x[..];
71+
let _ = &x[Foo..];
72+
let _ = &x[..Foo];
73+
let _ = &x[Foo..Foo];
74+
let _ = &mut x[..];
75+
let _ = &mut x[Foo..];
76+
let _ = &mut x[..Foo];
77+
let _ = &mut x[Foo..Foo];
7878
unsafe {
7979
assert_eq!(COUNT, 8);
8080
}

src/test/ui/const-generics/issues/issue-61432.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ fn promote<const N: i32>() {
66
// works:
77
//
88
// let n = N;
9-
// &n;
9+
// let _ = &n;
1010

11-
&N;
11+
let _ = &N;
1212
}
1313

1414
fn main() {

src/test/ui/dynamically-sized-types/dst-index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ impl Index<usize> for T {
2929

3030
fn main() {
3131
assert_eq!(&S[0], "hello");
32-
&T[0];
32+
let _ = &T[0];
3333
// let x = &x as &Debug;
3434
}

0 commit comments

Comments
 (0)