Rollup merge of #101492 - TaKO8Ki:suggest-adding-array-length-to-ref-to-array, r=oli-obk
Suggest adding array lengths to references to arrays if possible ref: https://github.com/rust-lang/rust/pull/100590#pullrequestreview-1096851146
This commit is contained in:
commit
bef48f9314
5 changed files with 120 additions and 29 deletions
|
@ -2401,6 +2401,14 @@ impl<'hir> Ty<'hir> {
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn peel_refs(&self) -> &Self {
|
||||||
|
let mut final_ty = self;
|
||||||
|
while let TyKind::Rptr(_, MutTy { ty, .. }) = &final_ty.kind {
|
||||||
|
final_ty = &ty;
|
||||||
|
}
|
||||||
|
final_ty
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Not represented directly in the AST; referred to by name through a `ty_path`.
|
/// Not represented directly in the AST; referred to by name through a `ty_path`.
|
||||||
|
|
|
@ -1305,31 +1305,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn suggest_array_len(&self, expr: &'tcx hir::Expr<'tcx>, array_len: u64) {
|
fn suggest_array_len(&self, expr: &'tcx hir::Expr<'tcx>, array_len: u64) {
|
||||||
if let Some(parent_hir_id) = self.tcx.hir().find_parent_node(expr.hir_id) {
|
let parent_node = self.tcx.hir().parent_iter(expr.hir_id).find(|(_, node)| {
|
||||||
let ty = match self.tcx.hir().find(parent_hir_id) {
|
!matches!(node, hir::Node::Expr(hir::Expr { kind: hir::ExprKind::AddrOf(..), .. }))
|
||||||
Some(
|
});
|
||||||
hir::Node::Local(hir::Local { ty: Some(ty), .. })
|
let Some((_,
|
||||||
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _), .. }),
|
hir::Node::Local(hir::Local { ty: Some(ty), .. })
|
||||||
) => Some(ty),
|
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _), .. }))
|
||||||
_ => None,
|
) = parent_node else {
|
||||||
};
|
return
|
||||||
if let Some(ty) = ty
|
};
|
||||||
&& let hir::TyKind::Array(_, length) = ty.kind
|
if let hir::TyKind::Array(_, length) = ty.peel_refs().kind
|
||||||
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length
|
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length
|
||||||
&& let Some(span) = self.tcx.hir().opt_span(hir_id)
|
&& let Some(span) = self.tcx.hir().opt_span(hir_id)
|
||||||
{
|
{
|
||||||
match self.tcx.sess.diagnostic().steal_diagnostic(span, StashKey::UnderscoreForArrayLengths) {
|
match self.tcx.sess.diagnostic().steal_diagnostic(span, StashKey::UnderscoreForArrayLengths) {
|
||||||
Some(mut err) => {
|
Some(mut err) => {
|
||||||
err.span_suggestion(
|
err.span_suggestion(
|
||||||
span,
|
span,
|
||||||
"consider specifying the array length",
|
"consider specifying the array length",
|
||||||
array_len,
|
array_len,
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
err.emit();
|
err.emit();
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
}
|
||||||
|
None => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,22 @@ fn main() {
|
||||||
const Foo: [i32; 3] = [1, 2, 3];
|
const Foo: [i32; 3] = [1, 2, 3];
|
||||||
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
//~| ERROR using `_` for array lengths is unstable
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
|
const REF_FOO: &[u8; 1] = &[1];
|
||||||
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
let foo: [i32; 3] = [1, 2, 3];
|
let foo: [i32; 3] = [1, 2, 3];
|
||||||
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
//~| ERROR using `_` for array lengths is unstable
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
let bar: [i32; 3] = [0; 3];
|
let bar: [i32; 3] = [0; 3];
|
||||||
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
//~| ERROR using `_` for array lengths is unstable
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
|
let ref_foo: &[i32; 3] = &[1, 2, 3];
|
||||||
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
|
let ref_bar: &[i32; 3] = &[0; 3];
|
||||||
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
|
let multiple_ref_foo: &&[i32; 3] = &&[1, 2, 3];
|
||||||
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,22 @@ fn main() {
|
||||||
const Foo: [i32; _] = [1, 2, 3];
|
const Foo: [i32; _] = [1, 2, 3];
|
||||||
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
//~| ERROR using `_` for array lengths is unstable
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
|
const REF_FOO: &[u8; _] = &[1];
|
||||||
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
let foo: [i32; _] = [1, 2, 3];
|
let foo: [i32; _] = [1, 2, 3];
|
||||||
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
//~| ERROR using `_` for array lengths is unstable
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
let bar: [i32; _] = [0; 3];
|
let bar: [i32; _] = [0; 3];
|
||||||
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
//~| ERROR using `_` for array lengths is unstable
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
|
let ref_foo: &[i32; _] = &[1, 2, 3];
|
||||||
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
|
let ref_bar: &[i32; _] = &[0; 3];
|
||||||
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
|
let multiple_ref_foo: &&[i32; _] = &&[1, 2, 3];
|
||||||
|
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
//~| ERROR using `_` for array lengths is unstable
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,45 @@
|
||||||
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
--> $DIR/suggest-array-length.rs:8:20
|
--> $DIR/suggest-array-length.rs:11:20
|
||||||
|
|
|
|
||||||
LL | let foo: [i32; _] = [1, 2, 3];
|
LL | let foo: [i32; _] = [1, 2, 3];
|
||||||
| ^ `_` not allowed here
|
| ^ `_` not allowed here
|
||||||
|
|
||||||
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
--> $DIR/suggest-array-length.rs:11:20
|
--> $DIR/suggest-array-length.rs:14:20
|
||||||
|
|
|
|
||||||
LL | let bar: [i32; _] = [0; 3];
|
LL | let bar: [i32; _] = [0; 3];
|
||||||
| ^ `_` not allowed here
|
| ^ `_` not allowed here
|
||||||
|
|
||||||
|
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
--> $DIR/suggest-array-length.rs:17:25
|
||||||
|
|
|
||||||
|
LL | let ref_foo: &[i32; _] = &[1, 2, 3];
|
||||||
|
| ^ `_` not allowed here
|
||||||
|
|
||||||
|
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
--> $DIR/suggest-array-length.rs:20:25
|
||||||
|
|
|
||||||
|
LL | let ref_bar: &[i32; _] = &[0; 3];
|
||||||
|
| ^ `_` not allowed here
|
||||||
|
|
||||||
|
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
--> $DIR/suggest-array-length.rs:23:35
|
||||||
|
|
|
||||||
|
LL | let multiple_ref_foo: &&[i32; _] = &&[1, 2, 3];
|
||||||
|
| ^ `_` not allowed here
|
||||||
|
|
||||||
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
--> $DIR/suggest-array-length.rs:5:22
|
--> $DIR/suggest-array-length.rs:5:22
|
||||||
|
|
|
|
||||||
LL | const Foo: [i32; _] = [1, 2, 3];
|
LL | const Foo: [i32; _] = [1, 2, 3];
|
||||||
| ^ `_` not allowed here
|
| ^ `_` not allowed here
|
||||||
|
|
||||||
|
error: in expressions, `_` can only be used on the left-hand side of an assignment
|
||||||
|
--> $DIR/suggest-array-length.rs:8:26
|
||||||
|
|
|
||||||
|
LL | const REF_FOO: &[u8; _] = &[1];
|
||||||
|
| ^ `_` not allowed here
|
||||||
|
|
||||||
error[E0658]: using `_` for array lengths is unstable
|
error[E0658]: using `_` for array lengths is unstable
|
||||||
--> $DIR/suggest-array-length.rs:5:22
|
--> $DIR/suggest-array-length.rs:5:22
|
||||||
|
|
|
|
||||||
|
@ -26,7 +50,16 @@ LL | const Foo: [i32; _] = [1, 2, 3];
|
||||||
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
||||||
|
|
||||||
error[E0658]: using `_` for array lengths is unstable
|
error[E0658]: using `_` for array lengths is unstable
|
||||||
--> $DIR/suggest-array-length.rs:8:20
|
--> $DIR/suggest-array-length.rs:8:26
|
||||||
|
|
|
||||||
|
LL | const REF_FOO: &[u8; _] = &[1];
|
||||||
|
| ^ help: consider specifying the array length: `1`
|
||||||
|
|
|
||||||
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
|
||||||
|
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
||||||
|
|
||||||
|
error[E0658]: using `_` for array lengths is unstable
|
||||||
|
--> $DIR/suggest-array-length.rs:11:20
|
||||||
|
|
|
|
||||||
LL | let foo: [i32; _] = [1, 2, 3];
|
LL | let foo: [i32; _] = [1, 2, 3];
|
||||||
| ^ help: consider specifying the array length: `3`
|
| ^ help: consider specifying the array length: `3`
|
||||||
|
@ -35,7 +68,7 @@ LL | let foo: [i32; _] = [1, 2, 3];
|
||||||
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
||||||
|
|
||||||
error[E0658]: using `_` for array lengths is unstable
|
error[E0658]: using `_` for array lengths is unstable
|
||||||
--> $DIR/suggest-array-length.rs:11:20
|
--> $DIR/suggest-array-length.rs:14:20
|
||||||
|
|
|
|
||||||
LL | let bar: [i32; _] = [0; 3];
|
LL | let bar: [i32; _] = [0; 3];
|
||||||
| ^ help: consider specifying the array length: `3`
|
| ^ help: consider specifying the array length: `3`
|
||||||
|
@ -43,6 +76,33 @@ LL | let bar: [i32; _] = [0; 3];
|
||||||
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
|
||||||
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
||||||
|
|
||||||
error: aborting due to 6 previous errors
|
error[E0658]: using `_` for array lengths is unstable
|
||||||
|
--> $DIR/suggest-array-length.rs:17:25
|
||||||
|
|
|
||||||
|
LL | let ref_foo: &[i32; _] = &[1, 2, 3];
|
||||||
|
| ^ help: consider specifying the array length: `3`
|
||||||
|
|
|
||||||
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
|
||||||
|
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
||||||
|
|
||||||
|
error[E0658]: using `_` for array lengths is unstable
|
||||||
|
--> $DIR/suggest-array-length.rs:20:25
|
||||||
|
|
|
||||||
|
LL | let ref_bar: &[i32; _] = &[0; 3];
|
||||||
|
| ^ help: consider specifying the array length: `3`
|
||||||
|
|
|
||||||
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
|
||||||
|
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
||||||
|
|
||||||
|
error[E0658]: using `_` for array lengths is unstable
|
||||||
|
--> $DIR/suggest-array-length.rs:23:35
|
||||||
|
|
|
||||||
|
LL | let multiple_ref_foo: &&[i32; _] = &&[1, 2, 3];
|
||||||
|
| ^ help: consider specifying the array length: `3`
|
||||||
|
|
|
||||||
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
|
||||||
|
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
|
||||||
|
|
||||||
|
error: aborting due to 14 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0658`.
|
For more information about this error, try `rustc --explain E0658`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue