Skip to content

Commit 81fa794

Browse files
committed
Specify tuple element in lint message
1 parent 058551c commit 81fa794

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/librustc_lint/unused.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
4848
}
4949

5050
let ty = cx.tables.expr_ty(&expr);
51-
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span);
51+
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "");
5252

5353
let mut fn_warned = false;
5454
let mut op_warned = false;
@@ -133,6 +133,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
133133
ty: Ty<'tcx>,
134134
expr: &hir::Expr,
135135
span: Span,
136+
descr_post_path: &str,
136137
) -> bool {
137138
if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(
138139
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 {
141142
}
142143

143144
match ty.sty {
144-
ty::Adt(def, _) => check_must_use_def(cx, def.did, span, "", ""),
145+
ty::Adt(def, _) => check_must_use_def(cx, def.did, span, "", descr_post_path),
145146
ty::Opaque(def, _) => {
146147
let mut has_emitted = false;
147148
for (predicate, _) in &cx.tcx.predicates_of(def).predicates {
@@ -178,7 +179,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
178179
vec![]
179180
};
180181
for (i, ty) in tys.iter().map(|k| k.expect_ty()).enumerate() {
181-
if check_must_use_ty(cx, ty, expr, *spans.get(i).unwrap_or(&span)) {
182+
let descr_post_path = &format!(" in tuple element {}", i);
183+
let span = *spans.get(i).unwrap_or(&span);
184+
if check_must_use_ty(cx, ty, expr, span, descr_post_path) {
182185
has_emitted = true;
183186
}
184187
}

src/test/ui/lint/must_use-tuple.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#![deny(unused_must_use)]
22

3-
fn foo() -> Result<(), ()> {
4-
Ok::<(), ()>(())
3+
fn foo() -> (Result<(), ()>, ()) {
4+
(Ok::<(), ()>(()), ())
55
}
66

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

1010
(Ok::<(), ()>(()), 0, Ok::<(), ()>(()), 5);
11-
//~^ ERROR unused `std::result::Result` that must be used
12-
//~^^ ERROR unused `std::result::Result` that must be used
11+
//~^ ERROR unused `std::result::Result`
12+
//~^^ ERROR unused `std::result::Result`
1313

14-
foo(); //~ ERROR unused `std::result::Result` that must be used
14+
foo(); //~ ERROR unused `std::result::Result`
1515
}

src/test/ui/lint/must_use-tuple.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: unused `std::result::Result` that must be used
1+
error: unused `std::result::Result` in tuple element 0 that must be used
22
--> $DIR/must_use-tuple.rs:8:6
33
|
44
LL | (Ok::<(), ()>(()),);
@@ -11,23 +11,23 @@ LL | #![deny(unused_must_use)]
1111
| ^^^^^^^^^^^^^^^
1212
= note: this `Result` may be an `Err` variant, which should be handled
1313

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

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

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

0 commit comments

Comments
 (0)