Lint for unused borrows as part of UNUSED_MUST_USE
This commit is contained in:
parent
1a462831ad
commit
88abd7d81d
18 changed files with 113 additions and 35 deletions
|
@ -154,6 +154,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
|
||||||
| hir::BinOpKind::Shl
|
| hir::BinOpKind::Shl
|
||||||
| hir::BinOpKind::Shr => Some("bitwise operation"),
|
| hir::BinOpKind::Shr => Some("bitwise operation"),
|
||||||
},
|
},
|
||||||
|
hir::ExprKind::AddrOf(..) => Some("borrow"),
|
||||||
hir::ExprKind::Unary(..) => Some("unary operation"),
|
hir::ExprKind::Unary(..) => Some("unary operation"),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
|
@ -54,7 +54,7 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma
|
||||||
quote! {}
|
quote! {}
|
||||||
} else if let Some(project) = attrs.project {
|
} else if let Some(project) = attrs.project {
|
||||||
quote! {
|
quote! {
|
||||||
&#bi.#project.hash_stable(__hcx, __hasher);
|
(&#bi.#project).hash_stable(__hcx, __hasher);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
|
@ -96,7 +96,7 @@ pub fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::To
|
||||||
quote! {}
|
quote! {}
|
||||||
} else if let Some(project) = attrs.project {
|
} else if let Some(project) = attrs.project {
|
||||||
quote! {
|
quote! {
|
||||||
&#bi.#project.hash_stable(__hcx, __hasher);
|
(&#bi.#project).hash_stable(__hcx, __hasher);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
|
|
|
@ -534,7 +534,7 @@ mod slice_index {
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_slice_fail() {
|
fn test_slice_fail() {
|
||||||
&"中华Việt Nam"[0..2];
|
let _ = &"中华Việt Nam"[0..2];
|
||||||
}
|
}
|
||||||
|
|
||||||
panic_cases! {
|
panic_cases! {
|
||||||
|
@ -714,13 +714,13 @@ mod slice_index {
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "byte index 1024 is out of bounds of `Lorem ipsum dolor sit amet")]
|
#[should_panic(expected = "byte index 1024 is out of bounds of `Lorem ipsum dolor sit amet")]
|
||||||
fn test_slice_fail_truncated_1() {
|
fn test_slice_fail_truncated_1() {
|
||||||
&LOREM_PARAGRAPH[..1024];
|
let _ = &LOREM_PARAGRAPH[..1024];
|
||||||
}
|
}
|
||||||
// check the truncation in the panic message
|
// check the truncation in the panic message
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "luctus, im`[...]")]
|
#[should_panic(expected = "luctus, im`[...]")]
|
||||||
fn test_slice_fail_truncated_2() {
|
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]
|
#[should_panic]
|
||||||
fn test_str_slice_rangetoinclusive_notok() {
|
fn test_str_slice_rangetoinclusive_notok() {
|
||||||
let s = "abcαβγ";
|
let s = "abcαβγ";
|
||||||
&s[..=3];
|
let _ = &s[..=3];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -751,7 +751,7 @@ fn test_str_slicemut_rangetoinclusive_ok() {
|
||||||
fn test_str_slicemut_rangetoinclusive_notok() {
|
fn test_str_slicemut_rangetoinclusive_notok() {
|
||||||
let mut s = "abcαβγ".to_owned();
|
let mut s = "abcαβγ".to_owned();
|
||||||
let s: &mut str = &mut s;
|
let s: &mut str = &mut s;
|
||||||
&mut s[..=3];
|
let _ = &mut s[..=3];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -542,35 +542,35 @@ fn test_index_out_of_bounds() {
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_slice_out_of_bounds_1() {
|
fn test_slice_out_of_bounds_1() {
|
||||||
let x = vec![1, 2, 3, 4, 5];
|
let x = vec![1, 2, 3, 4, 5];
|
||||||
&x[!0..];
|
let _ = &x[!0..];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_slice_out_of_bounds_2() {
|
fn test_slice_out_of_bounds_2() {
|
||||||
let x = vec![1, 2, 3, 4, 5];
|
let x = vec![1, 2, 3, 4, 5];
|
||||||
&x[..6];
|
let _ = &x[..6];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_slice_out_of_bounds_3() {
|
fn test_slice_out_of_bounds_3() {
|
||||||
let x = vec![1, 2, 3, 4, 5];
|
let x = vec![1, 2, 3, 4, 5];
|
||||||
&x[!0..4];
|
let _ = &x[!0..4];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_slice_out_of_bounds_4() {
|
fn test_slice_out_of_bounds_4() {
|
||||||
let x = vec![1, 2, 3, 4, 5];
|
let x = vec![1, 2, 3, 4, 5];
|
||||||
&x[1..6];
|
let _ = &x[1..6];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_slice_out_of_bounds_5() {
|
fn test_slice_out_of_bounds_5() {
|
||||||
let x = vec![1, 2, 3, 4, 5];
|
let x = vec![1, 2, 3, 4, 5];
|
||||||
&x[3..2];
|
let _ = &x[3..2];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -301,7 +301,7 @@ fn wtf8_slice() {
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn wtf8_slice_not_code_point_boundary() {
|
fn wtf8_slice_not_code_point_boundary() {
|
||||||
&Wtf8::from_str("aé 💩")[2..4];
|
let _ = &Wtf8::from_str("aé 💩")[2..4];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -312,7 +312,7 @@ fn wtf8_slice_from() {
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn wtf8_slice_from_not_code_point_boundary() {
|
fn wtf8_slice_from_not_code_point_boundary() {
|
||||||
&Wtf8::from_str("aé 💩")[2..];
|
let _ = &Wtf8::from_str("aé 💩")[2..];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -323,7 +323,7 @@ fn wtf8_slice_to() {
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn wtf8_slice_to_not_code_point_boundary() {
|
fn wtf8_slice_to_not_code_point_boundary() {
|
||||||
&Wtf8::from_str("aé 💩")[5..];
|
let _ = &Wtf8::from_str("aé 💩")[5..];
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -17,7 +17,7 @@ impl Drop for Foo {
|
||||||
|
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let x: &[_] = &[Foo, Foo];
|
let x: &[_] = &[Foo, Foo];
|
||||||
&x[3..4];
|
let _ = &x[3..4];
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -21,7 +21,7 @@ fn bar() -> usize {
|
||||||
|
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let x: &[_] = &[Foo, Foo];
|
let x: &[_] = &[Foo, Foo];
|
||||||
&x[3..bar()];
|
let _ = &x[3..bar()];
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -67,14 +67,14 @@ impl IndexMut<RangeFull> for Foo {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut x = Foo;
|
let mut x = Foo;
|
||||||
&x[..];
|
let _ = &x[..];
|
||||||
&x[Foo..];
|
let _ = &x[Foo..];
|
||||||
&x[..Foo];
|
let _ = &x[..Foo];
|
||||||
&x[Foo..Foo];
|
let _ = &x[Foo..Foo];
|
||||||
&mut x[..];
|
let _ = &mut x[..];
|
||||||
&mut x[Foo..];
|
let _ = &mut x[Foo..];
|
||||||
&mut x[..Foo];
|
let _ = &mut x[..Foo];
|
||||||
&mut x[Foo..Foo];
|
let _ = &mut x[Foo..Foo];
|
||||||
unsafe {
|
unsafe {
|
||||||
assert_eq!(COUNT, 8);
|
assert_eq!(COUNT, 8);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,9 +6,9 @@ fn promote<const N: i32>() {
|
||||||
// works:
|
// works:
|
||||||
//
|
//
|
||||||
// let n = N;
|
// let n = N;
|
||||||
// &n;
|
// let _ = &n;
|
||||||
|
|
||||||
&N;
|
let _ = &N;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -29,6 +29,6 @@ impl Index<usize> for T {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(&S[0], "hello");
|
assert_eq!(&S[0], "hello");
|
||||||
&T[0];
|
let _ = &T[0];
|
||||||
// let x = &x as &Debug;
|
// let x = &x as &Debug;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ fn main() {
|
||||||
yield ();
|
yield ();
|
||||||
4i32
|
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 | | // during the yield expression. Type checking will also compute liveness
|
||||||
LL | | // and it should also find out that `a` is not live.
|
LL | | // and it should also find out that `a` is not live.
|
||||||
... |
|
... |
|
||||||
LL | | &a;
|
LL | | let _ = &a;
|
||||||
LL | | };
|
LL | | };
|
||||||
| |__________^
|
| |__________^
|
||||||
|
|
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ fn main() {
|
||||||
yield;
|
yield;
|
||||||
true
|
true
|
||||||
};
|
};
|
||||||
&opt;
|
let _ = &opt;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// run-pass
|
// run-pass
|
||||||
fn main() {
|
fn main() {
|
||||||
&&[()][0];
|
let _ = &&[()][0];
|
||||||
println!("{:?}", &[(),()][1]);
|
println!("{:?}", &[(),()][1]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ trait FontTableTagConversions {
|
||||||
|
|
||||||
impl FontTableTagConversions for FontTableTag {
|
impl FontTableTagConversions for FontTableTag {
|
||||||
fn tag_to_string(self) {
|
fn tag_to_string(self) {
|
||||||
&self;
|
let _ = &self;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// We shouldn't promote this
|
// We shouldn't promote this
|
||||||
&(main as fn() == main as fn());
|
let _ = &(main as fn() == main as fn());
|
||||||
// Also check nested case
|
// 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