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`
This commit is contained in:
commit
39260f6d49
24 changed files with 133 additions and 55 deletions
|
|
@ -17,7 +17,7 @@ impl Drop for Foo {
|
|||
|
||||
fn foo() {
|
||||
let x: &[_] = &[Foo, Foo];
|
||||
&x[3..4];
|
||||
let _ = &x[3..4];
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ fn bar() -> usize {
|
|||
|
||||
fn foo() {
|
||||
let x: &[_] = &[Foo, Foo];
|
||||
&x[3..bar()];
|
||||
let _ = &x[3..bar()];
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ fn promote<const N: i32>() {
|
|||
// works:
|
||||
//
|
||||
// let n = N;
|
||||
// &n;
|
||||
// let _ = &n;
|
||||
|
||||
&N;
|
||||
let _ = &N;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ fn main() {
|
|||
yield ();
|
||||
4i32
|
||||
};
|
||||
&a;
|
||||
let _ = &a;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 | | };
|
||||
| |__________^
|
||||
|
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ fn main() {
|
|||
yield;
|
||||
true
|
||||
};
|
||||
&opt;
|
||||
let _ = &opt;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-pass
|
||||
fn main() {
|
||||
&&[()][0];
|
||||
let _ = &&[()][0];
|
||||
println!("{:?}", &[(),()][1]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ trait FontTableTagConversions {
|
|||
|
||||
impl FontTableTagConversions for FontTableTag {
|
||||
fn tag_to_string(self) {
|
||||
&self;
|
||||
let _ = &self;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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()));
|
||||
}
|
||||
|
|
|
|||
33
src/test/ui/lint/unused-borrows.rs
Normal file
33
src/test/ui/lint/unused-borrows.rs
Normal file
|
|
@ -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();
|
||||
}
|
||||
44
src/test/ui/lint/unused-borrows.stderr
Normal file
44
src/test/ui/lint/unused-borrows.stderr
Normal file
|
|
@ -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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue