Skip to content

Commit cf06e03

Browse files
authored
Rollup merge of rust-lang#54292 - memoryruins:issue-53712, r=estebank
Suggest array indexing when tuple indexing on an array Closes rust-lang#53712 r? @varkor cc @estebank
2 parents ea6cfe3 + 73fdc81 commit cf06e03

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,6 +3347,24 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33473347
}
33483348
};
33493349
}
3350+
ty::Array(_, len) => {
3351+
if let (Some(len), Ok(user_index)) = (
3352+
len.assert_usize(self.tcx),
3353+
field.as_str().parse::<u64>()
3354+
) {
3355+
let base = self.tcx.hir.node_to_pretty_string(base.id);
3356+
let help = "instead of using tuple indexing, use array indexing";
3357+
let suggestion = format!("{}[{}]", base, field);
3358+
let applicability = if len < user_index {
3359+
Applicability::MachineApplicable
3360+
} else {
3361+
Applicability::MaybeIncorrect
3362+
};
3363+
err.span_suggestion_with_applicability(
3364+
expr.span, help, suggestion, applicability
3365+
);
3366+
}
3367+
}
33503368
ty::RawPtr(..) => {
33513369
let base = self.tcx.hir.node_to_pretty_string(base.id);
33523370
let msg = format!("`{}` is a native pointer; try dereferencing it", base);

src/test/ui/issues/issue-53712.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// issue #53712: make the error generated by using tuple indexing on an array more specific
2+
3+
fn main() {
4+
let arr = [10, 20, 30, 40, 50];
5+
arr.0;
6+
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609]
7+
//~| HELP instead of using tuple indexing, use array indexing
8+
//~| SUGGESTION arr[0]
9+
}

src/test/ui/issues/issue-53712.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0609]: no field `0` on type `[{integer}; 5]`
2+
--> $DIR/issue-53712.rs:5:9
3+
|
4+
LL | arr.0;
5+
| ----^
6+
| |
7+
| help: instead of using tuple indexing, use array indexing: `arr[0]`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0609`.

0 commit comments

Comments
 (0)