Skip to content

Commit dff1bc1

Browse files
committed
Set diagnostic applicability based on array length
1 parent 30556d5 commit dff1bc1

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,12 +3344,24 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33443344
}
33453345
};
33463346
}
3347-
ty::Array(ty, _) if ty.is_numeric() => {
3348-
let base = self.tcx.hir.node_to_pretty_string(base.id);
3349-
let msg = format!("attempting to use tuple indexing on an array; try");
3350-
let suggestion = format!("{}[{}]", base, field);
3351-
err.span_suggestion(field.span, &msg, suggestion);
3352-
},
3347+
ty::Array(_, len) => {
3348+
if let (Some(len), Ok(user_index)) = (
3349+
len.assert_usize(self.tcx),
3350+
field.as_str().parse::<u64>()
3351+
) {
3352+
let base = self.tcx.hir.node_to_pretty_string(base.id);
3353+
let help = "instead of using tuple indexing, use array indexing";
3354+
let suggestion = format!("{}[{}]", base, field);
3355+
let applicability = if len < user_index {
3356+
Applicability::MachineApplicable
3357+
} else {
3358+
Applicability::MaybeIncorrect
3359+
};
3360+
err.span_suggestion_with_applicability(
3361+
field.span, help, suggestion, applicability
3362+
);
3363+
}
3364+
}
33533365
ty::RawPtr(..) => {
33543366
let base = self.tcx.hir.node_to_pretty_string(base.id);
33553367
let msg = format!("`{}` is a native pointer; try dereferencing it", base);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ fn main() {
44
let arr = [10, 20, 30, 40, 50];
55
arr.0;
66
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609]
7-
//~| HELP attempting to use tuple indexing on an array; try
7+
//~| HELP instead of using tuple indexing, use array indexing
88
//~| SUGGESTION arr[0]
99
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0609]: no field `0` on type `[{integer}; 5]`
22
--> $DIR/issue-53712.rs:5:9
33
|
44
LL | arr.0;
5-
| ^ help: attempting to use tuple indexing on an array; try: `arr[0]`
5+
| ^ help: instead of using tuple indexing, use array indexing: `arr[0]`
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)