Skip to content

Commit 39260f6

Browse files
committed
Auto merge of #86426 - hi-rustin:rustin-patch-lint-warn, r=Aaron1011
Lint for unused borrows as part of UNUSED_MUST_USE close #76264 base on #76894 r? `@RalfJung`
2 parents 9cf05f3 + 884336e commit 39260f6

24 files changed

+133
-55
lines changed

compiler/rustc_lint/src/unused.rs

+1
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

+2-2
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

+5-5
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

+5-5
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

+3-3
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

+1-1
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

+1-1
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

+8-8
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

+2-2
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

+1-1
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
}

src/test/ui/generator/too-live-local-in-immovable-gen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
yield ();
1616
4i32
1717
};
18-
&a;
18+
let _ = &a;
1919
};
2020
}
2121
}

src/test/ui/generator/too-live-local-in-immovable-gen.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | | // Tests that the generator transformation finds out that `a`
66
LL | | // during the yield expression. Type checking will also compute liveness
77
LL | | // and it should also find out that `a` is not live.
88
... |
9-
LL | | &a;
9+
LL | | let _ = &a;
1010
LL | | };
1111
| |__________^
1212
|

src/test/ui/generator/yield-in-initializer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
yield;
1212
true
1313
};
14-
&opt;
14+
let _ = &opt;
1515
}
1616
};
1717
}

src/test/ui/issues/issue-43205.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-pass
22
fn main() {
3-
&&[()][0];
3+
let _ = &&[()][0];
44
println!("{:?}", &[(),()][1]);
55
}

src/test/ui/issues/issue-5280.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ trait FontTableTagConversions {
99

1010
impl FontTableTagConversions for FontTableTag {
1111
fn tag_to_string(self) {
12-
&self;
12+
let _ = &self;
1313
}
1414
}
1515

src/test/ui/issues/issue-54696.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
fn main() {
44
// We shouldn't promote this
5-
&(main as fn() == main as fn());
5+
let _ = &(main as fn() == main as fn());
66
// Also check nested case
7-
&(&(main as fn()) == &(main as fn()));
7+
let _ = &(&(main as fn()) == &(main as fn()));
88
}

src/test/ui/lint/unused-borrows.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![deny(unused_must_use)]
2+
3+
fn foo(_: i32) -> bool { todo!() }
4+
5+
fn bar() -> &'static i32 {
6+
&42;
7+
//~^ unused
8+
9+
&mut foo(42);
10+
//~^ unused
11+
12+
&&42;
13+
//~^ unused
14+
15+
&&mut 42;
16+
//~^ unused
17+
18+
&mut &42;
19+
//~^ unused
20+
21+
let _result = foo(4)
22+
&& foo(2); // Misplaced semi-colon (perhaps due to reordering of lines)
23+
&& foo(42);
24+
//~^ unused
25+
26+
let _ = &42; // ok
27+
28+
&42 // ok
29+
}
30+
31+
fn main() {
32+
let _ = bar();
33+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error: unused borrow that must be used
2+
--> $DIR/unused-borrows.rs:6:5
3+
|
4+
LL | &42;
5+
| ^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-borrows.rs:1:9
9+
|
10+
LL | #![deny(unused_must_use)]
11+
| ^^^^^^^^^^^^^^^
12+
13+
error: unused borrow that must be used
14+
--> $DIR/unused-borrows.rs:9:5
15+
|
16+
LL | &mut foo(42);
17+
| ^^^^^^^^^^^^
18+
19+
error: unused borrow that must be used
20+
--> $DIR/unused-borrows.rs:12:5
21+
|
22+
LL | &&42;
23+
| ^^^^
24+
25+
error: unused borrow that must be used
26+
--> $DIR/unused-borrows.rs:15:5
27+
|
28+
LL | &&mut 42;
29+
| ^^^^^^^^
30+
31+
error: unused borrow that must be used
32+
--> $DIR/unused-borrows.rs:18:5
33+
|
34+
LL | &mut &42;
35+
| ^^^^^^^^
36+
37+
error: unused borrow that must be used
38+
--> $DIR/unused-borrows.rs:23:5
39+
|
40+
LL | && foo(42);
41+
| ^^^^^^^^^^
42+
43+
error: aborting due to 6 previous errors
44+

src/tools/clippy/tests/ui/bytes_nth.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
fn main() {
77
let s = String::from("String");
88
s.as_bytes().get(3);
9-
&s.as_bytes().get(3);
9+
let _ = &s.as_bytes().get(3);
1010
s[..].as_bytes().get(3);
1111
}

src/tools/clippy/tests/ui/bytes_nth.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
fn main() {
77
let s = String::from("String");
88
s.bytes().nth(3);
9-
&s.bytes().nth(3);
9+
let _ = &s.bytes().nth(3);
1010
s[..].bytes().nth(3);
1111
}

src/tools/clippy/tests/ui/bytes_nth.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ LL | s.bytes().nth(3);
77
= note: `-D clippy::bytes-nth` implied by `-D warnings`
88

99
error: called `.byte().nth()` on a `String`
10-
--> $DIR/bytes_nth.rs:9:6
10+
--> $DIR/bytes_nth.rs:9:14
1111
|
12-
LL | &s.bytes().nth(3);
13-
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
12+
LL | let _ = &s.bytes().nth(3);
13+
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
1414

1515
error: called `.byte().nth()` on a `str`
1616
--> $DIR/bytes_nth.rs:10:5

src/tools/clippy/tests/ui/iter_count.fixed

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn main() {
5050
linked_list.push_back(1);
5151
binary_heap.push(1);
5252

53-
&vec[..].len();
53+
let _ = &vec[..].len();
5454
vec.len();
5555
boxed_slice.len();
5656
vec_deque.len();
@@ -62,13 +62,13 @@ fn main() {
6262
binary_heap.len();
6363

6464
vec.len();
65-
&vec[..].len();
65+
let _ = &vec[..].len();
6666
vec_deque.len();
6767
hash_map.len();
6868
b_tree_map.len();
6969
linked_list.len();
7070

71-
&vec[..].len();
71+
let _ = &vec[..].len();
7272
vec.len();
7373
vec_deque.len();
7474
hash_set.len();

0 commit comments

Comments
 (0)