Skip to content

Commit

Permalink
Ignore private must_use fields
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Oct 8, 2019
1 parent 57a0449 commit 8bcf64a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
len: usize,
err: bool, // HACK: Report an error rather than a lint, for crater testing.
) -> bool {
if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(
cx.tcx.hir().get_module_parent(expr.hir_id), ty)
{
let module = cx.tcx.hir().get_module_parent(expr.hir_id);

if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(module, ty) {
return true;
}

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

for variant in &def.variants {
for (i, field) in variant.fields.iter().enumerate() {
let descr_post
= &format!(" in field `{}`", field.ident.as_str());
let ty = cx.tcx.type_of(field.did).subst(cx.tcx, subst);
let (expr, span) = if let Some(&field) = fields.get(i) {
(field, field.span)
} else {
(expr, span)
};
has_emitted |= check_must_use_ty(
cx, ty, expr, span, descr_pre, descr_post, len, true);
let is_visible = def.is_enum() ||
field.vis.is_accessible_from(module, cx.tcx);
if is_visible {
let descr_post
= &format!(" in field `{}`", field.ident.as_str());
let ty = cx.tcx.type_of(field.did).subst(cx.tcx, subst);
let (expr, span) = if let Some(&field) = fields.get(i) {
(field, field.span)
} else {
(expr, span)
};
has_emitted |= check_must_use_ty(
cx, ty, expr, span, descr_pre, descr_post, len, true);
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/lint/auxiliary/private-nested-unused_must_use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[must_use]
pub struct S;

pub struct T;

pub struct B {
s: S,
pub t: T,
}

pub struct C {
pub s: S,
pub t: T,
}

impl B {
pub fn new() -> B {
B { s: S, t: T }
}
}

impl C {
pub fn new() -> C {
C { s: S, t: T }
}
}
12 changes: 12 additions & 0 deletions src/test/ui/lint/mod-nested-unused_must_use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// aux-build:private-nested-unused_must_use.rs

#![deny(unused_must_use)]

extern crate private_nested_unused_must_use;

use self::private_nested_unused_must_use::{B, C};

fn main() {
B::new(); // ok: ignores private `must_use` type
C::new(); //~ ERROR unused `private_nested_unused_must_use::S` in field `s` that must be used
}
8 changes: 8 additions & 0 deletions src/test/ui/lint/mod-nested-unused_must_use.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unused `private_nested_unused_must_use::S` in field `s` that must be used
--> $DIR/mod-nested-unused_must_use.rs:11:5
|
LL | C::new();
| ^^^^^^^^^

error: aborting due to previous error

0 comments on commit 8bcf64a

Please sign in to comment.