Skip to content

Commit 30fc601

Browse files
committed
Tests for field is never read diagnostic
1 parent 07e0e2e commit 30fc601

4 files changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//! The field `guard` is never used directly, but it is still useful for its side effect when
2+
//! dropped. Since rustc doesn't consider a `Drop` impl as a use, we want to make sure we at least
3+
//! produce a helpful diagnostic that points the user to what they can do if they indeed intended to
4+
//! have a field that is only used for its `Drop` side effect.
5+
//!
6+
//! Issue: https://github.com/rust-lang/rust/issues/81658
7+
8+
#![deny(dead_code)]
9+
10+
use std::sync::{Mutex, MutexGuard};
11+
12+
/// Holds a locked value until it is dropped
13+
pub struct Locked<'a, T> {
14+
// Field is kept for its affect when dropped, but otherwise unused
15+
guard: MutexGuard<'a, T>, //~ ERROR field is never read
16+
}
17+
18+
impl<'a, T> Locked<'a, T> {
19+
pub fn new(value: &'a Mutex<T>) -> Self {
20+
Self {
21+
guard: value.lock().unwrap(),
22+
}
23+
}
24+
}
25+
26+
fn main() {
27+
let items = Mutex::new(vec![1, 2, 3]);
28+
29+
// Hold a lock on items while doing something else
30+
let result = {
31+
// The lock will be released at the end of this scope
32+
let _lock = Locked::new(&items);
33+
34+
do_something_else()
35+
};
36+
37+
println!("{}", result);
38+
}
39+
40+
fn do_something_else() -> i32 {
41+
1 + 1
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: field is never read: `guard`
2+
--> $DIR/drop-only-field-issue-81658.rs:15:5
3+
|
4+
LL | guard: MutexGuard<'a, T>,
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/drop-only-field-issue-81658.rs:8:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: aborting due to previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//! The field `items` is being "used" by FFI (implicitly through pointers). However, since rustc
2+
//! doesn't know how to detect that, we produce a message that says the field is unused. This can
3+
//! cause some confusion and we want to make sure our diagnostics help as much as they can.
4+
//!
5+
//! Issue: https://github.com/rust-lang/rust/issues/81658
6+
7+
#![deny(dead_code)]
8+
9+
/// A struct for holding on to data while it is being used in our FFI code
10+
pub struct FFIData<T> {
11+
/// These values cannot be dropped while the pointers to each item
12+
/// are still in use
13+
items: Option<Vec<T>>, //~ ERROR field is never read
14+
}
15+
16+
impl<T> FFIData<T> {
17+
pub fn new() -> Self {
18+
Self {items: None}
19+
}
20+
21+
/// Load items into this type and return pointers to each item that can
22+
/// be passed to FFI
23+
pub fn load(&mut self, items: Vec<T>) -> Vec<*const T> {
24+
let ptrs = items.iter().map(|item| item as *const _).collect();
25+
26+
self.items = Some(items);
27+
28+
ptrs
29+
}
30+
}
31+
32+
extern {
33+
/// The FFI code that uses items
34+
fn process_item(item: *const i32);
35+
}
36+
37+
fn main() {
38+
// Data cannot be dropped until the end of this scope or else the items
39+
// will be dropped before they are processed
40+
let mut data = FFIData::new();
41+
42+
let ptrs = data.load(vec![1, 2, 3, 4, 5]);
43+
44+
for ptr in ptrs {
45+
// Safety: This pointer is valid as long as the arena is in scope
46+
unsafe { process_item(ptr); }
47+
}
48+
49+
// Items will be safely freed at the end of this scope
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: field is never read: `items`
2+
--> $DIR/field-used-in-ffi-issue-81658.rs:13:5
3+
|
4+
LL | items: Option<Vec<T>>,
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/field-used-in-ffi-issue-81658.rs:7:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)