Skip to content

Commit

Permalink
Specify tuple element in lint message
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Jun 3, 2019
1 parent 058551c commit 81fa794
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/librustc_lint/unused.rs
Expand Up @@ -48,7 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
}

let ty = cx.tables.expr_ty(&expr);
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span);
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "");

let mut fn_warned = false;
let mut op_warned = false;
Expand Down Expand Up @@ -133,6 +133,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
ty: Ty<'tcx>,
expr: &hir::Expr,
span: Span,
descr_post_path: &str,
) -> bool {
if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(
cx.tcx.hir().get_module_parent_by_hir_id(expr.hir_id), ty)
Expand All @@ -141,7 +142,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
}

match ty.sty {
ty::Adt(def, _) => check_must_use_def(cx, def.did, span, "", ""),
ty::Adt(def, _) => check_must_use_def(cx, def.did, span, "", descr_post_path),
ty::Opaque(def, _) => {
let mut has_emitted = false;
for (predicate, _) in &cx.tcx.predicates_of(def).predicates {
Expand Down Expand Up @@ -178,7 +179,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
vec![]
};
for (i, ty) in tys.iter().map(|k| k.expect_ty()).enumerate() {
if check_must_use_ty(cx, ty, expr, *spans.get(i).unwrap_or(&span)) {
let descr_post_path = &format!(" in tuple element {}", i);
let span = *spans.get(i).unwrap_or(&span);
if check_must_use_ty(cx, ty, expr, span, descr_post_path) {
has_emitted = true;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/lint/must_use-tuple.rs
@@ -1,15 +1,15 @@
#![deny(unused_must_use)]

fn foo() -> Result<(), ()> {
Ok::<(), ()>(())
fn foo() -> (Result<(), ()>, ()) {
(Ok::<(), ()>(()), ())
}

fn main() {
(Ok::<(), ()>(()),); //~ ERROR unused `std::result::Result` that must be used
(Ok::<(), ()>(()),); //~ ERROR unused `std::result::Result`

(Ok::<(), ()>(()), 0, Ok::<(), ()>(()), 5);
//~^ ERROR unused `std::result::Result` that must be used
//~^^ ERROR unused `std::result::Result` that must be used
//~^ ERROR unused `std::result::Result`
//~^^ ERROR unused `std::result::Result`

foo(); //~ ERROR unused `std::result::Result` that must be used
foo(); //~ ERROR unused `std::result::Result`
}
8 changes: 4 additions & 4 deletions src/test/ui/lint/must_use-tuple.stderr
@@ -1,4 +1,4 @@
error: unused `std::result::Result` that must be used
error: unused `std::result::Result` in tuple element 0 that must be used
--> $DIR/must_use-tuple.rs:8:6
|
LL | (Ok::<(), ()>(()),);
Expand All @@ -11,23 +11,23 @@ LL | #![deny(unused_must_use)]
| ^^^^^^^^^^^^^^^
= note: this `Result` may be an `Err` variant, which should be handled

error: unused `std::result::Result` that must be used
error: unused `std::result::Result` in tuple element 0 that must be used
--> $DIR/must_use-tuple.rs:10:6
|
LL | (Ok::<(), ()>(()), 0, Ok::<(), ()>(()), 5);
| ^^^^^^^^^^^^^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled

error: unused `std::result::Result` that must be used
error: unused `std::result::Result` in tuple element 2 that must be used
--> $DIR/must_use-tuple.rs:10:27
|
LL | (Ok::<(), ()>(()), 0, Ok::<(), ()>(()), 5);
| ^^^^^^^^^^^^^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled

error: unused `std::result::Result` that must be used
error: unused `std::result::Result` in tuple element 0 that must be used
--> $DIR/must_use-tuple.rs:14:5
|
LL | foo();
Expand Down

0 comments on commit 81fa794

Please sign in to comment.