Skip to content

Commit c779f4e

Browse files
authored
Rollup merge of rust-lang#62228 - varkor:must_use-trait-in-box, r=Centril
Extend the #[must_use] lint to boxed types Fixes rust-lang#55506 (comment) (cc @Nemo157). This should have been included as part of rust-lang#55663, but was overlooked.
2 parents 1683bb7 + 400fd60 commit c779f4e

File tree

7 files changed

+67
-10
lines changed

7 files changed

+67
-10
lines changed

src/librustc_lint/unused.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
4848
}
4949

5050
let ty = cx.tables.expr_ty(&expr);
51-
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "");
51+
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "", "");
5252

5353
let mut fn_warned = false;
5454
let mut op_warned = false;
@@ -133,6 +133,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
133133
ty: Ty<'tcx>,
134134
expr: &hir::Expr,
135135
span: Span,
136+
descr_pre_path: &str,
136137
descr_post_path: &str,
137138
) -> bool {
138139
if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(
@@ -142,14 +143,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
142143
}
143144

144145
match ty.sty {
145-
ty::Adt(def, _) => check_must_use_def(cx, def.did, span, "", descr_post_path),
146+
ty::Adt(..) if ty.is_box() => {
147+
let boxed_ty = ty.boxed_ty();
148+
let descr_pre_path = &format!("{}boxed ", descr_pre_path);
149+
check_must_use_ty(cx, boxed_ty, expr, span, descr_pre_path, descr_post_path)
150+
}
151+
ty::Adt(def, _) => {
152+
check_must_use_def(cx, def.did, span, descr_pre_path, descr_post_path)
153+
}
146154
ty::Opaque(def, _) => {
147155
let mut has_emitted = false;
148156
for (predicate, _) in &cx.tcx.predicates_of(def).predicates {
149157
if let ty::Predicate::Trait(ref poly_trait_predicate) = predicate {
150158
let trait_ref = poly_trait_predicate.skip_binder().trait_ref;
151159
let def_id = trait_ref.def_id;
152-
if check_must_use_def(cx, def_id, span, "implementer of ", "") {
160+
let descr_pre = &format!("{}implementer of ", descr_pre_path);
161+
if check_must_use_def(cx, def_id, span, descr_pre, descr_post_path) {
153162
has_emitted = true;
154163
break;
155164
}
@@ -162,7 +171,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
162171
for predicate in binder.skip_binder().iter() {
163172
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate {
164173
let def_id = trait_ref.def_id;
165-
if check_must_use_def(cx, def_id, span, "", " trait object") {
174+
let descr_post = &format!(" trait object{}", descr_post_path);
175+
if check_must_use_def(cx, def_id, span, descr_pre_path, descr_post) {
166176
has_emitted = true;
167177
break;
168178
}
@@ -181,7 +191,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
181191
for (i, ty) in tys.iter().map(|k| k.expect_ty()).enumerate() {
182192
let descr_post_path = &format!(" in tuple element {}", i);
183193
let span = *spans.get(i).unwrap_or(&span);
184-
if check_must_use_ty(cx, ty, expr, span, descr_post_path) {
194+
if check_must_use_ty(cx, ty, expr, span, descr_pre_path, descr_post_path) {
185195
has_emitted = true;
186196
}
187197
}

src/libstd/panicking.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ pub fn set_hook(hook: Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send>) {
103103
HOOK_LOCK.write_unlock();
104104

105105
if let Hook::Custom(ptr) = old_hook {
106-
Box::from_raw(ptr);
106+
#[allow(unused_must_use)] {
107+
Box::from_raw(ptr);
108+
}
107109
}
108110
}
109111
}

src/test/run-pass/issues/issue-30530.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ pub enum Handler {
1212
}
1313

1414
fn main() {
15-
take(Handler::Default, Box::new(main));
15+
#[allow(unused_must_use)] {
16+
take(Handler::Default, Box::new(main));
17+
}
1618
}
1719

1820
#[inline(never)]

src/test/run-pass/panics/panic-handler-flail-wildly.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// run-pass
2+
23
#![allow(stable_features)]
4+
#![allow(unused_must_use)]
35

46
// ignore-emscripten no threads support
57

src/test/ui/lint/must_use-trait.rs

+17
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ fn get_critical() -> impl NotSoCritical + Critical + DecidedlyUnimportant {
1717
Anon {}
1818
}
1919

20+
fn get_boxed_critical() -> Box<dyn Critical> {
21+
Box::new(Anon {})
22+
}
23+
24+
fn get_nested_boxed_critical() -> Box<Box<dyn Critical>> {
25+
Box::new(Box::new(Anon {}))
26+
}
27+
28+
fn get_critical_tuple() -> (u32, Box<dyn Critical>, impl Critical, ()) {
29+
(0, get_boxed_critical(), get_critical(), ())
30+
}
31+
2032
fn main() {
2133
get_critical(); //~ ERROR unused implementer of `Critical` that must be used
34+
get_boxed_critical(); //~ ERROR unused boxed `Critical` trait object that must be used
35+
get_nested_boxed_critical();
36+
//~^ ERROR unused boxed boxed `Critical` trait object that must be used
37+
get_critical_tuple(); //~ ERROR unused boxed `Critical` trait object in tuple element 1
38+
//~^ ERROR unused implementer of `Critical` in tuple element 2
2239
}

src/test/ui/lint/must_use-trait.stderr

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unused implementer of `Critical` that must be used
2-
--> $DIR/must_use-trait.rs:21:5
2+
--> $DIR/must_use-trait.rs:33:5
33
|
44
LL | get_critical();
55
| ^^^^^^^^^^^^^^^
@@ -10,5 +10,29 @@ note: lint level defined here
1010
LL | #![deny(unused_must_use)]
1111
| ^^^^^^^^^^^^^^^
1212

13-
error: aborting due to previous error
13+
error: unused boxed `Critical` trait object that must be used
14+
--> $DIR/must_use-trait.rs:34:5
15+
|
16+
LL | get_boxed_critical();
17+
| ^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: unused boxed boxed `Critical` trait object that must be used
20+
--> $DIR/must_use-trait.rs:35:5
21+
|
22+
LL | get_nested_boxed_critical();
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: unused boxed `Critical` trait object in tuple element 1 that must be used
26+
--> $DIR/must_use-trait.rs:37:5
27+
|
28+
LL | get_critical_tuple();
29+
| ^^^^^^^^^^^^^^^^^^^^^
30+
31+
error: unused implementer of `Critical` in tuple element 2 that must be used
32+
--> $DIR/must_use-trait.rs:37:5
33+
|
34+
LL | get_critical_tuple();
35+
| ^^^^^^^^^^^^^^^^^^^^^
36+
37+
error: aborting due to 5 previous errors
1438

0 commit comments

Comments
 (0)