Skip to content

Suggest array indexing when tuple indexing on an array #54292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3344,12 +3344,24 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
}
ty::Array(ty, _) if ty.is_numeric() => {
let base = self.tcx.hir.node_to_pretty_string(base.id);
let msg = format!("attempting to use tuple indexing on an array; try");
let suggestion = format!("{}[{}]", base, field);
err.span_suggestion(field.span, &msg, suggestion);
},
ty::Array(_, len) => {
if let (Some(len), Ok(user_index)) = (
len.assert_usize(self.tcx),
field.as_str().parse::<u64>()
) {
let base = self.tcx.hir.node_to_pretty_string(base.id);
let help = "instead of using tuple indexing, use array indexing";
let suggestion = format!("{}[{}]", base, field);
let applicability = if len < user_index {
Applicability::MachineApplicable
} else {
Applicability::MaybeIncorrect
};
err.span_suggestion_with_applicability(
field.span, help, suggestion, applicability
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to use expr.span here instead of field.span.

);
}
}
ty::RawPtr(..) => {
let base = self.tcx.hir.node_to_pretty_string(base.id);
let msg = format!("`{}` is a native pointer; try dereferencing it", base);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-53712.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ fn main() {
let arr = [10, 20, 30, 40, 50];
arr.0;
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609]
//~| HELP attempting to use tuple indexing on an array; try
//~| HELP instead of using tuple indexing, use array indexing
//~| SUGGESTION arr[0]
}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-53712.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0609]: no field `0` on type `[{integer}; 5]`
--> $DIR/issue-53712.rs:5:9
|
LL | arr.0;
| ^ help: attempting to use tuple indexing on an array; try: `arr[0]`
| ^ help: instead of using tuple indexing, use array indexing: `arr[0]`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that the suggestion span is slightly incorrect. The suggestion span should cover the entire expression, otherwise when applying the suggestion with rustfix, for example, you'd end up with the following code

    arr.arr[0];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right you are!


error: aborting due to previous error

Expand Down