Tweak slice and as_deref suggestion span
Use multispan suggestion.
This commit is contained in:
parent
ff92ab0903
commit
8ea1066fe6
@ -2499,7 +2499,6 @@ fn error_expected_array_or_slice(
|
|||||||
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
|
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
|
||||||
&& let Some(span) = ti.span
|
&& let Some(span) = ti.span
|
||||||
&& let Some(_) = ti.origin_expr
|
&& let Some(_) = ti.origin_expr
|
||||||
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
|
|
||||||
{
|
{
|
||||||
let resolved_ty = self.resolve_vars_if_possible(ti.expected);
|
let resolved_ty = self.resolve_vars_if_possible(ti.expected);
|
||||||
let (is_slice_or_array_or_vector, resolved_ty) =
|
let (is_slice_or_array_or_vector, resolved_ty) =
|
||||||
@ -2510,10 +2509,10 @@ fn error_expected_array_or_slice(
|
|||||||
|| self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
|
|| self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
|
||||||
{
|
{
|
||||||
// Slicing won't work here, but `.as_deref()` might (issue #91328).
|
// Slicing won't work here, but `.as_deref()` might (issue #91328).
|
||||||
err.span_suggestion(
|
err.span_suggestion_verbose(
|
||||||
span,
|
span.shrink_to_hi(),
|
||||||
"consider using `as_deref` here",
|
"consider using `as_deref` here",
|
||||||
format!("{snippet}.as_deref()"),
|
".as_deref()",
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -2522,10 +2521,10 @@ fn error_expected_array_or_slice(
|
|||||||
|
|
||||||
let is_top_level = current_depth <= 1;
|
let is_top_level = current_depth <= 1;
|
||||||
if is_slice_or_array_or_vector && is_top_level {
|
if is_slice_or_array_or_vector && is_top_level {
|
||||||
err.span_suggestion(
|
err.span_suggestion_verbose(
|
||||||
span,
|
span.shrink_to_hi(),
|
||||||
"consider slicing here",
|
"consider slicing here",
|
||||||
format!("{snippet}[..]"),
|
"[..]",
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,12 @@ error[E0529]: expected an array or slice, found `Vec<{integer}>`
|
|||||||
--> $DIR/let-else-slicing-error.rs:6:9
|
--> $DIR/let-else-slicing-error.rs:6:9
|
||||||
|
|
|
|
||||||
LL | let [x, y] = nums else {
|
LL | let [x, y] = nums else {
|
||||||
| ^^^^^^ ---- help: consider slicing here: `nums[..]`
|
| ^^^^^^ pattern cannot match with input type `Vec<{integer}>`
|
||||||
| |
|
|
|
||||||
| pattern cannot match with input type `Vec<{integer}>`
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | let [x, y] = nums[..] else {
|
||||||
|
| ++++
|
||||||
|
|
||||||
error: aborting due to 1 previous error
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
@ -17,18 +17,24 @@ LL + [v] => {},
|
|||||||
error[E0529]: expected an array or slice, found `Vec<i32>`
|
error[E0529]: expected an array or slice, found `Vec<i32>`
|
||||||
--> $DIR/match-ergonomics.rs:8:9
|
--> $DIR/match-ergonomics.rs:8:9
|
||||||
|
|
|
|
||||||
LL | match x {
|
|
||||||
| - help: consider slicing here: `x[..]`
|
|
||||||
LL | [&v] => {},
|
LL | [&v] => {},
|
||||||
| ^^^^ pattern cannot match with input type `Vec<i32>`
|
| ^^^^ pattern cannot match with input type `Vec<i32>`
|
||||||
|
|
|
||||||
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | match x[..] {
|
||||||
|
| ++++
|
||||||
|
|
||||||
error[E0529]: expected an array or slice, found `Vec<i32>`
|
error[E0529]: expected an array or slice, found `Vec<i32>`
|
||||||
--> $DIR/match-ergonomics.rs:20:9
|
--> $DIR/match-ergonomics.rs:20:9
|
||||||
|
|
|
|
||||||
LL | match x {
|
|
||||||
| - help: consider slicing here: `x[..]`
|
|
||||||
LL | [v] => {},
|
LL | [v] => {},
|
||||||
| ^^^ pattern cannot match with input type `Vec<i32>`
|
| ^^^ pattern cannot match with input type `Vec<i32>`
|
||||||
|
|
|
||||||
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | match x[..] {
|
||||||
|
| ++++
|
||||||
|
|
||||||
error[E0308]: mismatched types
|
error[E0308]: mismatched types
|
||||||
--> $DIR/match-ergonomics.rs:29:9
|
--> $DIR/match-ergonomics.rs:29:9
|
||||||
|
@ -2,42 +2,56 @@ error[E0529]: expected an array or slice, found `Vec<i32>`
|
|||||||
--> $DIR/pattern-slice-vec.rs:8:12
|
--> $DIR/pattern-slice-vec.rs:8:12
|
||||||
|
|
|
|
||||||
LL | if let [_, _, _] = foo() {}
|
LL | if let [_, _, _] = foo() {}
|
||||||
| ^^^^^^^^^ ----- help: consider slicing here: `foo()[..]`
|
| ^^^^^^^^^ pattern cannot match with input type `Vec<i32>`
|
||||||
| |
|
|
|
||||||
| pattern cannot match with input type `Vec<i32>`
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | if let [_, _, _] = foo()[..] {}
|
||||||
|
| ++++
|
||||||
|
|
||||||
error[E0529]: expected an array or slice, found `Vec<i32>`
|
error[E0529]: expected an array or slice, found `Vec<i32>`
|
||||||
--> $DIR/pattern-slice-vec.rs:12:12
|
--> $DIR/pattern-slice-vec.rs:12:12
|
||||||
|
|
|
|
||||||
LL | if let [] = &foo() {}
|
LL | if let [] = &foo() {}
|
||||||
| ^^ ------ help: consider slicing here: `&foo()[..]`
|
| ^^ pattern cannot match with input type `Vec<i32>`
|
||||||
| |
|
|
|
||||||
| pattern cannot match with input type `Vec<i32>`
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | if let [] = &foo()[..] {}
|
||||||
|
| ++++
|
||||||
|
|
||||||
error[E0529]: expected an array or slice, found `Vec<i32>`
|
error[E0529]: expected an array or slice, found `Vec<i32>`
|
||||||
--> $DIR/pattern-slice-vec.rs:16:12
|
--> $DIR/pattern-slice-vec.rs:16:12
|
||||||
|
|
|
|
||||||
LL | if let [] = foo() {}
|
LL | if let [] = foo() {}
|
||||||
| ^^ ----- help: consider slicing here: `foo()[..]`
|
| ^^ pattern cannot match with input type `Vec<i32>`
|
||||||
| |
|
|
|
||||||
| pattern cannot match with input type `Vec<i32>`
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | if let [] = foo()[..] {}
|
||||||
|
| ++++
|
||||||
|
|
||||||
error[E0529]: expected an array or slice, found `Vec<_>`
|
error[E0529]: expected an array or slice, found `Vec<_>`
|
||||||
--> $DIR/pattern-slice-vec.rs:23:9
|
--> $DIR/pattern-slice-vec.rs:23:9
|
||||||
|
|
|
|
||||||
LL | match &v {
|
|
||||||
| -- help: consider slicing here: `&v[..]`
|
|
||||||
LL |
|
|
||||||
LL | [5] => {}
|
LL | [5] => {}
|
||||||
| ^^^ pattern cannot match with input type `Vec<_>`
|
| ^^^ pattern cannot match with input type `Vec<_>`
|
||||||
|
|
|
||||||
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | match &v[..] {
|
||||||
|
| ++++
|
||||||
|
|
||||||
error[E0529]: expected an array or slice, found `Vec<{integer}>`
|
error[E0529]: expected an array or slice, found `Vec<{integer}>`
|
||||||
--> $DIR/pattern-slice-vec.rs:28:9
|
--> $DIR/pattern-slice-vec.rs:28:9
|
||||||
|
|
|
|
||||||
LL | let [..] = vec![1, 2, 3];
|
LL | let [..] = vec![1, 2, 3];
|
||||||
| ^^^^ ------------- help: consider slicing here: `vec![1, 2, 3][..]`
|
| ^^^^ pattern cannot match with input type `Vec<{integer}>`
|
||||||
| |
|
|
|
||||||
| pattern cannot match with input type `Vec<{integer}>`
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | let [..] = vec![1, 2, 3][..];
|
||||||
|
| ++++
|
||||||
|
|
||||||
error: aborting due to 5 previous errors
|
error: aborting due to 5 previous errors
|
||||||
|
|
||||||
|
@ -2,9 +2,12 @@ error[E0529]: expected an array or slice, found `Vec<Struct>`
|
|||||||
--> $DIR/suppress-consider-slicing-issue-120605.rs:7:16
|
--> $DIR/suppress-consider-slicing-issue-120605.rs:7:16
|
||||||
|
|
|
|
||||||
LL | if let [Struct { a: [] }] = &self.a {
|
LL | if let [Struct { a: [] }] = &self.a {
|
||||||
| ^^^^^^^^^^^^^^^^^^ ------- help: consider slicing here: `&self.a[..]`
|
| ^^^^^^^^^^^^^^^^^^ pattern cannot match with input type `Vec<Struct>`
|
||||||
| |
|
|
|
||||||
| pattern cannot match with input type `Vec<Struct>`
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | if let [Struct { a: [] }] = &self.a[..] {
|
||||||
|
| ++++
|
||||||
|
|
||||||
error[E0529]: expected an array or slice, found `Vec<Struct>`
|
error[E0529]: expected an array or slice, found `Vec<Struct>`
|
||||||
--> $DIR/suppress-consider-slicing-issue-120605.rs:7:29
|
--> $DIR/suppress-consider-slicing-issue-120605.rs:7:29
|
||||||
|
@ -5,5 +5,5 @@ fn main() {
|
|||||||
arr.0;
|
arr.0;
|
||||||
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609]
|
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609]
|
||||||
//~| HELP instead of using tuple indexing, use array indexing
|
//~| HELP instead of using tuple indexing, use array indexing
|
||||||
//~| SUGGESTION arr[0]
|
//~| SUGGESTION [
|
||||||
}
|
}
|
||||||
|
@ -1,38 +1,46 @@
|
|||||||
error[E0529]: expected an array or slice, found `Vec<i32>`
|
error[E0529]: expected an array or slice, found `Vec<i32>`
|
||||||
--> $DIR/issue-91328.rs:10:12
|
--> $DIR/issue-91328.rs:10:12
|
||||||
|
|
|
|
||||||
LL | match r {
|
|
||||||
| - help: consider using `as_deref` here: `r.as_deref()`
|
|
||||||
LL |
|
|
||||||
LL | Ok([a, b]) => a + b,
|
LL | Ok([a, b]) => a + b,
|
||||||
| ^^^^^^ pattern cannot match with input type `Vec<i32>`
|
| ^^^^^^ pattern cannot match with input type `Vec<i32>`
|
||||||
|
|
|
||||||
|
help: consider using `as_deref` here
|
||||||
|
|
|
||||||
|
LL | match r.as_deref() {
|
||||||
|
| +++++++++++
|
||||||
|
|
||||||
error[E0529]: expected an array or slice, found `Vec<i32>`
|
error[E0529]: expected an array or slice, found `Vec<i32>`
|
||||||
--> $DIR/issue-91328.rs:20:14
|
--> $DIR/issue-91328.rs:20:14
|
||||||
|
|
|
|
||||||
LL | match o {
|
|
||||||
| - help: consider using `as_deref` here: `o.as_deref()`
|
|
||||||
LL |
|
|
||||||
LL | Some([a, b]) => a + b,
|
LL | Some([a, b]) => a + b,
|
||||||
| ^^^^^^ pattern cannot match with input type `Vec<i32>`
|
| ^^^^^^ pattern cannot match with input type `Vec<i32>`
|
||||||
|
|
|
||||||
|
help: consider using `as_deref` here
|
||||||
|
|
|
||||||
|
LL | match o.as_deref() {
|
||||||
|
| +++++++++++
|
||||||
|
|
||||||
error[E0529]: expected an array or slice, found `Vec<i32>`
|
error[E0529]: expected an array or slice, found `Vec<i32>`
|
||||||
--> $DIR/issue-91328.rs:30:9
|
--> $DIR/issue-91328.rs:30:9
|
||||||
|
|
|
|
||||||
LL | match v {
|
|
||||||
| - help: consider slicing here: `v[..]`
|
|
||||||
LL |
|
|
||||||
LL | [a, b] => a + b,
|
LL | [a, b] => a + b,
|
||||||
| ^^^^^^ pattern cannot match with input type `Vec<i32>`
|
| ^^^^^^ pattern cannot match with input type `Vec<i32>`
|
||||||
|
|
|
||||||
|
help: consider slicing here
|
||||||
|
|
|
||||||
|
LL | match v[..] {
|
||||||
|
| ++++
|
||||||
|
|
||||||
error[E0529]: expected an array or slice, found `Box<[i32; 2]>`
|
error[E0529]: expected an array or slice, found `Box<[i32; 2]>`
|
||||||
--> $DIR/issue-91328.rs:40:14
|
--> $DIR/issue-91328.rs:40:14
|
||||||
|
|
|
|
||||||
LL | match a {
|
|
||||||
| - help: consider using `as_deref` here: `a.as_deref()`
|
|
||||||
LL |
|
|
||||||
LL | Some([a, b]) => a + b,
|
LL | Some([a, b]) => a + b,
|
||||||
| ^^^^^^ pattern cannot match with input type `Box<[i32; 2]>`
|
| ^^^^^^ pattern cannot match with input type `Box<[i32; 2]>`
|
||||||
|
|
|
||||||
|
help: consider using `as_deref` here
|
||||||
|
|
|
||||||
|
LL | match a.as_deref() {
|
||||||
|
| +++++++++++
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user