Tweak slice and as_deref suggestion span

Use multispan suggestion.
This commit is contained in:
Esteban Küber 2024-07-04 00:22:26 +00:00
parent ff92ab0903
commit 8ea1066fe6
7 changed files with 78 additions and 45 deletions

View File

@ -2499,7 +2499,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
&& let Some(span) = ti.span
&& 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 (is_slice_or_array_or_vector, resolved_ty) =
@ -2510,10 +2509,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|| self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
{
// Slicing won't work here, but `.as_deref()` might (issue #91328).
err.span_suggestion(
span,
err.span_suggestion_verbose(
span.shrink_to_hi(),
"consider using `as_deref` here",
format!("{snippet}.as_deref()"),
".as_deref()",
Applicability::MaybeIncorrect,
);
}
@ -2522,10 +2521,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let is_top_level = current_depth <= 1;
if is_slice_or_array_or_vector && is_top_level {
err.span_suggestion(
span,
err.span_suggestion_verbose(
span.shrink_to_hi(),
"consider slicing here",
format!("{snippet}[..]"),
"[..]",
Applicability::MachineApplicable,
);
}

View File

@ -2,9 +2,12 @@ error[E0529]: expected an array or slice, found `Vec<{integer}>`
--> $DIR/let-else-slicing-error.rs:6:9
|
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

View File

@ -17,18 +17,24 @@ LL + [v] => {},
error[E0529]: expected an array or slice, found `Vec<i32>`
--> $DIR/match-ergonomics.rs:8:9
|
LL | match x {
| - help: consider slicing here: `x[..]`
LL | [&v] => {},
| ^^^^ 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>`
--> $DIR/match-ergonomics.rs:20:9
|
LL | match x {
| - help: consider slicing here: `x[..]`
LL | [v] => {},
| ^^^ pattern cannot match with input type `Vec<i32>`
|
help: consider slicing here
|
LL | match x[..] {
| ++++
error[E0308]: mismatched types
--> $DIR/match-ergonomics.rs:29:9

View File

@ -2,42 +2,56 @@ error[E0529]: expected an array or slice, found `Vec<i32>`
--> $DIR/pattern-slice-vec.rs:8:12
|
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>`
--> $DIR/pattern-slice-vec.rs:12:12
|
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>`
--> $DIR/pattern-slice-vec.rs:16:12
|
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<_>`
--> $DIR/pattern-slice-vec.rs:23:9
|
LL | match &v {
| -- help: consider slicing here: `&v[..]`
LL |
LL | [5] => {}
| ^^^ pattern cannot match with input type `Vec<_>`
|
help: consider slicing here
|
LL | match &v[..] {
| ++++
error[E0529]: expected an array or slice, found `Vec<{integer}>`
--> $DIR/pattern-slice-vec.rs:28:9
|
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

View File

@ -2,9 +2,12 @@ error[E0529]: expected an array or slice, found `Vec<Struct>`
--> $DIR/suppress-consider-slicing-issue-120605.rs:7:16
|
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>`
--> $DIR/suppress-consider-slicing-issue-120605.rs:7:29

View File

@ -5,5 +5,5 @@ fn main() {
arr.0;
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609]
//~| HELP instead of using tuple indexing, use array indexing
//~| SUGGESTION arr[0]
//~| SUGGESTION [
}

View File

@ -1,38 +1,46 @@
error[E0529]: expected an array or slice, found `Vec<i32>`
--> $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,
| ^^^^^^ 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>`
--> $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,
| ^^^^^^ 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>`
--> $DIR/issue-91328.rs:30:9
|
LL | match v {
| - help: consider slicing here: `v[..]`
LL |
LL | [a, b] => a + b,
| ^^^^^^ 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]>`
--> $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,
| ^^^^^^ 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