Skip to content

Commit 8bcf64a

Browse files
committed
Ignore private must_use fields
1 parent 57a0449 commit 8bcf64a

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

src/librustc_lint/unused.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
140140
len: usize,
141141
err: bool, // HACK: Report an error rather than a lint, for crater testing.
142142
) -> bool {
143-
if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(
144-
cx.tcx.hir().get_module_parent(expr.hir_id), ty)
145-
{
143+
let module = cx.tcx.hir().get_module_parent(expr.hir_id);
144+
145+
if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(module, ty) {
146146
return true;
147147
}
148148

@@ -174,16 +174,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
174174

175175
for variant in &def.variants {
176176
for (i, field) in variant.fields.iter().enumerate() {
177-
let descr_post
178-
= &format!(" in field `{}`", field.ident.as_str());
179-
let ty = cx.tcx.type_of(field.did).subst(cx.tcx, subst);
180-
let (expr, span) = if let Some(&field) = fields.get(i) {
181-
(field, field.span)
182-
} else {
183-
(expr, span)
184-
};
185-
has_emitted |= check_must_use_ty(
186-
cx, ty, expr, span, descr_pre, descr_post, len, true);
177+
let is_visible = def.is_enum() ||
178+
field.vis.is_accessible_from(module, cx.tcx);
179+
if is_visible {
180+
let descr_post
181+
= &format!(" in field `{}`", field.ident.as_str());
182+
let ty = cx.tcx.type_of(field.did).subst(cx.tcx, subst);
183+
let (expr, span) = if let Some(&field) = fields.get(i) {
184+
(field, field.span)
185+
} else {
186+
(expr, span)
187+
};
188+
has_emitted |= check_must_use_ty(
189+
cx, ty, expr, span, descr_pre, descr_post, len, true);
190+
}
187191
}
188192
}
189193
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#[must_use]
2+
pub struct S;
3+
4+
pub struct T;
5+
6+
pub struct B {
7+
s: S,
8+
pub t: T,
9+
}
10+
11+
pub struct C {
12+
pub s: S,
13+
pub t: T,
14+
}
15+
16+
impl B {
17+
pub fn new() -> B {
18+
B { s: S, t: T }
19+
}
20+
}
21+
22+
impl C {
23+
pub fn new() -> C {
24+
C { s: S, t: T }
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// aux-build:private-nested-unused_must_use.rs
2+
3+
#![deny(unused_must_use)]
4+
5+
extern crate private_nested_unused_must_use;
6+
7+
use self::private_nested_unused_must_use::{B, C};
8+
9+
fn main() {
10+
B::new(); // ok: ignores private `must_use` type
11+
C::new(); //~ ERROR unused `private_nested_unused_must_use::S` in field `s` that must be used
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unused `private_nested_unused_must_use::S` in field `s` that must be used
2+
--> $DIR/mod-nested-unused_must_use.rs:11:5
3+
|
4+
LL | C::new();
5+
| ^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)