Specify tuple element in lint message

This commit is contained in:
varkor 2019-05-28 20:12:48 +01:00
parent 058551c4fd
commit 81fa794af9
3 changed files with 16 additions and 13 deletions

View File

@ -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;
@ -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)
@ -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 {
@ -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;
}
}

View File

@ -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`
}

View File

@ -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::<(), ()>(()),);
@ -11,7 +11,7 @@ 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);
@ -19,7 +19,7 @@ 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);
@ -27,7 +27,7 @@ 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();