Skip to content

Commit 8ea1066

Browse files
committed
Tweak slice and as_deref suggestion span
Use multispan suggestion.
1 parent ff92ab0 commit 8ea1066

File tree

7 files changed

+78
-45
lines changed

7 files changed

+78
-45
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -2499,7 +2499,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24992499
.any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
25002500
&& let Some(span) = ti.span
25012501
&& let Some(_) = ti.origin_expr
2502-
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
25032502
{
25042503
let resolved_ty = self.resolve_vars_if_possible(ti.expected);
25052504
let (is_slice_or_array_or_vector, resolved_ty) =
@@ -2510,10 +2509,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25102509
|| self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
25112510
{
25122511
// Slicing won't work here, but `.as_deref()` might (issue #91328).
2513-
err.span_suggestion(
2514-
span,
2512+
err.span_suggestion_verbose(
2513+
span.shrink_to_hi(),
25152514
"consider using `as_deref` here",
2516-
format!("{snippet}.as_deref()"),
2515+
".as_deref()",
25172516
Applicability::MaybeIncorrect,
25182517
);
25192518
}
@@ -2522,10 +2521,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25222521

25232522
let is_top_level = current_depth <= 1;
25242523
if is_slice_or_array_or_vector && is_top_level {
2525-
err.span_suggestion(
2526-
span,
2524+
err.span_suggestion_verbose(
2525+
span.shrink_to_hi(),
25272526
"consider slicing here",
2528-
format!("{snippet}[..]"),
2527+
"[..]",
25292528
Applicability::MachineApplicable,
25302529
);
25312530
}

tests/ui/let-else/let-else-slicing-error.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0529]: expected an array or slice, found `Vec<{integer}>`
22
--> $DIR/let-else-slicing-error.rs:6:9
33
|
44
LL | let [x, y] = nums else {
5-
| ^^^^^^ ---- help: consider slicing here: `nums[..]`
6-
| |
7-
| pattern cannot match with input type `Vec<{integer}>`
5+
| ^^^^^^ pattern cannot match with input type `Vec<{integer}>`
6+
|
7+
help: consider slicing here
8+
|
9+
LL | let [x, y] = nums[..] else {
10+
| ++++
811

912
error: aborting due to 1 previous error
1013

tests/ui/suggestions/match-ergonomics.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,24 @@ LL + [v] => {},
1717
error[E0529]: expected an array or slice, found `Vec<i32>`
1818
--> $DIR/match-ergonomics.rs:8:9
1919
|
20-
LL | match x {
21-
| - help: consider slicing here: `x[..]`
2220
LL | [&v] => {},
2321
| ^^^^ pattern cannot match with input type `Vec<i32>`
22+
|
23+
help: consider slicing here
24+
|
25+
LL | match x[..] {
26+
| ++++
2427

2528
error[E0529]: expected an array or slice, found `Vec<i32>`
2629
--> $DIR/match-ergonomics.rs:20:9
2730
|
28-
LL | match x {
29-
| - help: consider slicing here: `x[..]`
3031
LL | [v] => {},
3132
| ^^^ pattern cannot match with input type `Vec<i32>`
33+
|
34+
help: consider slicing here
35+
|
36+
LL | match x[..] {
37+
| ++++
3238

3339
error[E0308]: mismatched types
3440
--> $DIR/match-ergonomics.rs:29:9

tests/ui/suggestions/pattern-slice-vec.stderr

+29-15
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,56 @@ error[E0529]: expected an array or slice, found `Vec<i32>`
22
--> $DIR/pattern-slice-vec.rs:8:12
33
|
44
LL | if let [_, _, _] = foo() {}
5-
| ^^^^^^^^^ ----- help: consider slicing here: `foo()[..]`
6-
| |
7-
| pattern cannot match with input type `Vec<i32>`
5+
| ^^^^^^^^^ pattern cannot match with input type `Vec<i32>`
6+
|
7+
help: consider slicing here
8+
|
9+
LL | if let [_, _, _] = foo()[..] {}
10+
| ++++
811

912
error[E0529]: expected an array or slice, found `Vec<i32>`
1013
--> $DIR/pattern-slice-vec.rs:12:12
1114
|
1215
LL | if let [] = &foo() {}
13-
| ^^ ------ help: consider slicing here: `&foo()[..]`
14-
| |
15-
| pattern cannot match with input type `Vec<i32>`
16+
| ^^ pattern cannot match with input type `Vec<i32>`
17+
|
18+
help: consider slicing here
19+
|
20+
LL | if let [] = &foo()[..] {}
21+
| ++++
1622

1723
error[E0529]: expected an array or slice, found `Vec<i32>`
1824
--> $DIR/pattern-slice-vec.rs:16:12
1925
|
2026
LL | if let [] = foo() {}
21-
| ^^ ----- help: consider slicing here: `foo()[..]`
22-
| |
23-
| pattern cannot match with input type `Vec<i32>`
27+
| ^^ pattern cannot match with input type `Vec<i32>`
28+
|
29+
help: consider slicing here
30+
|
31+
LL | if let [] = foo()[..] {}
32+
| ++++
2433

2534
error[E0529]: expected an array or slice, found `Vec<_>`
2635
--> $DIR/pattern-slice-vec.rs:23:9
2736
|
28-
LL | match &v {
29-
| -- help: consider slicing here: `&v[..]`
30-
LL |
3137
LL | [5] => {}
3238
| ^^^ pattern cannot match with input type `Vec<_>`
39+
|
40+
help: consider slicing here
41+
|
42+
LL | match &v[..] {
43+
| ++++
3344

3445
error[E0529]: expected an array or slice, found `Vec<{integer}>`
3546
--> $DIR/pattern-slice-vec.rs:28:9
3647
|
3748
LL | let [..] = vec![1, 2, 3];
38-
| ^^^^ ------------- help: consider slicing here: `vec![1, 2, 3][..]`
39-
| |
40-
| pattern cannot match with input type `Vec<{integer}>`
49+
| ^^^^ pattern cannot match with input type `Vec<{integer}>`
50+
|
51+
help: consider slicing here
52+
|
53+
LL | let [..] = vec![1, 2, 3][..];
54+
| ++++
4155

4256
error: aborting due to 5 previous errors
4357

tests/ui/suggestions/suppress-consider-slicing-issue-120605.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0529]: expected an array or slice, found `Vec<Struct>`
22
--> $DIR/suppress-consider-slicing-issue-120605.rs:7:16
33
|
44
LL | if let [Struct { a: [] }] = &self.a {
5-
| ^^^^^^^^^^^^^^^^^^ ------- help: consider slicing here: `&self.a[..]`
6-
| |
7-
| pattern cannot match with input type `Vec<Struct>`
5+
| ^^^^^^^^^^^^^^^^^^ pattern cannot match with input type `Vec<Struct>`
6+
|
7+
help: consider slicing here
8+
|
9+
LL | if let [Struct { a: [] }] = &self.a[..] {
10+
| ++++
811

912
error[E0529]: expected an array or slice, found `Vec<Struct>`
1013
--> $DIR/suppress-consider-slicing-issue-120605.rs:7:29

tests/ui/typeck/issue-53712.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ fn main() {
55
arr.0;
66
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609]
77
//~| HELP instead of using tuple indexing, use array indexing
8-
//~| SUGGESTION arr[0]
8+
//~| SUGGESTION [
99
}

tests/ui/typeck/issue-91328.stderr

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
11
error[E0529]: expected an array or slice, found `Vec<i32>`
22
--> $DIR/issue-91328.rs:10:12
33
|
4-
LL | match r {
5-
| - help: consider using `as_deref` here: `r.as_deref()`
6-
LL |
74
LL | Ok([a, b]) => a + b,
85
| ^^^^^^ pattern cannot match with input type `Vec<i32>`
6+
|
7+
help: consider using `as_deref` here
8+
|
9+
LL | match r.as_deref() {
10+
| +++++++++++
911

1012
error[E0529]: expected an array or slice, found `Vec<i32>`
1113
--> $DIR/issue-91328.rs:20:14
1214
|
13-
LL | match o {
14-
| - help: consider using `as_deref` here: `o.as_deref()`
15-
LL |
1615
LL | Some([a, b]) => a + b,
1716
| ^^^^^^ pattern cannot match with input type `Vec<i32>`
17+
|
18+
help: consider using `as_deref` here
19+
|
20+
LL | match o.as_deref() {
21+
| +++++++++++
1822

1923
error[E0529]: expected an array or slice, found `Vec<i32>`
2024
--> $DIR/issue-91328.rs:30:9
2125
|
22-
LL | match v {
23-
| - help: consider slicing here: `v[..]`
24-
LL |
2526
LL | [a, b] => a + b,
2627
| ^^^^^^ pattern cannot match with input type `Vec<i32>`
28+
|
29+
help: consider slicing here
30+
|
31+
LL | match v[..] {
32+
| ++++
2733

2834
error[E0529]: expected an array or slice, found `Box<[i32; 2]>`
2935
--> $DIR/issue-91328.rs:40:14
3036
|
31-
LL | match a {
32-
| - help: consider using `as_deref` here: `a.as_deref()`
33-
LL |
3437
LL | Some([a, b]) => a + b,
3538
| ^^^^^^ pattern cannot match with input type `Box<[i32; 2]>`
39+
|
40+
help: consider using `as_deref` here
41+
|
42+
LL | match a.as_deref() {
43+
| +++++++++++
3644

3745
error: aborting due to 4 previous errors
3846

0 commit comments

Comments
 (0)