Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn must_use in tuple #29815

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ impl LintPass for UnusedResults {
}

impl LateLintPass for UnusedResults {
fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
let expr = match s.node {
fn check_stmt(&mut self, cx: &LateContext, stmt: &hir::Stmt) {
let expr = match stmt.node {
hir::StmtSemi(ref expr, _) => &**expr,
_ => return
};
Expand All @@ -131,37 +131,48 @@ impl LateLintPass for UnusedResults {
}

let t = cx.tcx.expr_ty(&expr);
let warned = match t.sty {
match t.sty {
ty::TyTuple(ref tys) if tys.is_empty() => return,
ty::TyBool => return,
ty::TyStruct(def, _) |
ty::TyEnum(def, _) => {
let attrs = cx.tcx.get_attrs(def.did);
check_must_use(cx, &attrs[..], s.span)
_ => ()
}
let must_use = check_must_use(cx, t);
if let Some(s) = must_use {
let mut msg = "unused result which must be used".to_string();
if !s.is_empty() {
msg.push_str(": ");
msg.push_str(&s);
}
_ => false,
};
if !warned {
cx.span_lint(UNUSED_RESULTS, s.span, "unused result");
cx.span_lint(UNUSED_MUST_USE, stmt.span, &msg);
} else {
cx.span_lint(UNUSED_RESULTS, stmt.span, "unused result");
}

fn check_must_use(cx: &LateContext, attrs: &[ast::Attribute], sp: Span) -> bool {
for attr in attrs {
if attr.check_name("must_use") {
let mut msg = "unused result which must be used".to_string();
// check for #[must_use="..."]
match attr.value_str() {
None => {}
Some(s) => {
msg.push_str(": ");
msg.push_str(&s);
fn check_must_use(cx: &LateContext, ty: ty::Ty) -> Option<String> {
match ty.sty {
ty::TyStruct(def, _) |
ty::TyEnum(def, _) => {
for attr in cx.tcx.get_attrs(def.did).iter() {
if attr.check_name("must_use") {
let s = match attr.value_str() {
None => "".to_string(),
Some(s) => s.to_string()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to_owned to to_string

};
return Some(s);
}
}
cx.span_lint(UNUSED_MUST_USE, sp, &msg);
return true;
}
ty::TyTuple(ref tys) => {
for &ty in tys {
let result = check_must_use(cx, ty);
if result.is_some() {
return result;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, something like this might work:

tys.iter()
   .filter_map(|ty| check_must_use(cx, ty))
   .next()

}
_ => ()
}
false
None
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/compile-fail/unused-result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ fn main() {
let _ = foo::<isize>();
let _ = foo::<MustUse>();
let _ = foo::<MustUseMsg>();

foo::<(MustUse, ())>(); //~ ERROR: unused result which must be used
foo::<(MustUseMsg, ())>(); //~ ERROR: unused result which must be used: some message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add checks that when we do use the result, then it is not an error?

}