Skip to content

Commit 160905b

Browse files
Trim suggestion part before generating highlights
1 parent 0a7ab1d commit 160905b

36 files changed

+106
-113
lines changed

compiler/rustc_errors/src/emitter.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -2216,12 +2216,7 @@ impl HumanEmitter {
22162216
if let DisplaySuggestion::Diff | DisplaySuggestion::Underline | DisplaySuggestion::Add =
22172217
show_code_change
22182218
{
2219-
for mut part in parts {
2220-
// If this is a replacement of, e.g. `"a"` into `"ab"`, adjust the
2221-
// suggestion and snippet to look as if we just suggested to add
2222-
// `"b"`, which is typically much easier for the user to understand.
2223-
part.trim_trivial_replacements(sm);
2224-
2219+
for part in parts {
22252220
let snippet = if let Ok(snippet) = sm.span_to_snippet(part.span) {
22262221
snippet
22272222
} else {

compiler/rustc_errors/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,12 @@ impl CodeSuggestion {
403403
// or deleted code in order to point at the correct column *after* substitution.
404404
let mut acc = 0;
405405
let mut only_capitalization = false;
406-
for part in &substitution.parts {
406+
for part in &mut substitution.parts {
407+
// If this is a replacement of, e.g. `"a"` into `"ab"`, adjust the
408+
// suggestion and snippet to look as if we just suggested to add
409+
// `"b"`, which is typically much easier for the user to understand.
410+
part.trim_trivial_replacements(sm);
411+
407412
only_capitalization |= is_case_difference(sm, &part.snippet, part.span);
408413
let cur_lo = sm.lookup_char_pos(part.span.lo());
409414
if prev_hi.line == cur_lo.line {

src/tools/clippy/tests/ui/async_yields_async.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ LL | | };
1414
= help: to override `-D warnings` add `#[allow(clippy::async_yields_async)]`
1515
help: consider awaiting this value
1616
|
17-
LL ~ async {
18-
LL + 3
19-
LL + }.await
17+
LL | async {
18+
LL | 3
19+
LL ~ }.await
2020
|
2121

2222
error: an async construct yields a type which is itself awaitable
@@ -46,9 +46,9 @@ LL | | };
4646
|
4747
help: consider awaiting this value
4848
|
49-
LL ~ async {
50-
LL + 3
51-
LL + }.await
49+
LL | async {
50+
LL | 3
51+
LL ~ }.await
5252
|
5353

5454
error: an async construct yields a type which is itself awaitable

src/tools/clippy/tests/ui/fn_to_numeric_cast_any.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ LL | f as usize
152152
help: did you mean to invoke the function?
153153
|
154154
LL | f() as usize
155-
|
155+
| ++
156156

157157
error: casting function pointer `T::static_method` to `usize`
158158
--> tests/ui/fn_to_numeric_cast_any.rs:62:5
@@ -163,7 +163,7 @@ LL | T::static_method as usize
163163
help: did you mean to invoke the function?
164164
|
165165
LL | T::static_method() as usize
166-
|
166+
| ++
167167

168168
error: casting function pointer `(clos as fn(u32) -> u32)` to `usize`
169169
--> tests/ui/fn_to_numeric_cast_any.rs:69:13

src/tools/clippy/tests/ui/implicit_return.stderr

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | true
99
help: add `return` as shown
1010
|
1111
LL | return true
12-
|
12+
| ++++++
1313

1414
error: missing `return` statement
1515
--> tests/ui/implicit_return.rs:19:15
@@ -122,7 +122,7 @@ LL | format!("test {}", "test")
122122
help: add `return` as shown
123123
|
124124
LL | return format!("test {}", "test")
125-
|
125+
| ++++++
126126

127127
error: missing `return` statement
128128
--> tests/ui/implicit_return.rs:90:5
@@ -133,7 +133,7 @@ LL | m!(true, false)
133133
help: add `return` as shown
134134
|
135135
LL | return m!(true, false)
136-
|
136+
| ++++++
137137

138138
error: missing `return` statement
139139
--> tests/ui/implicit_return.rs:96:13
@@ -169,10 +169,8 @@ LL | | }
169169
|
170170
help: add `return` as shown
171171
|
172-
LL ~ return loop {
173-
LL + m!(true);
174-
LL + }
175-
|
172+
LL | return loop {
173+
| ++++++
176174

177175
error: missing `return` statement
178176
--> tests/ui/implicit_return.rs:130:5
@@ -183,7 +181,7 @@ LL | true
183181
help: add `return` as shown
184182
|
185183
LL | return true
186-
|
184+
| ++++++
187185

188186
error: aborting due to 16 previous errors
189187

src/tools/clippy/tests/ui/manual_flatten.stderr

+3-5
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,9 @@ LL | | }
196196
| |_________^
197197
help: try
198198
|
199-
LL ~ for n in vec![
200-
LL +
201-
LL + Some(1),
202-
LL + Some(2),
203-
LL + Some(3)
199+
LL | for n in vec![
200+
...
201+
LL | Some(3)
204202
LL ~ ].iter().flatten() {
205203
|
206204

src/tools/clippy/tests/ui/too_long_first_doc_paragraph-fix.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | | /// 200 characters so I needed to write something longeeeeeeer.
1212
= help: to override `-D warnings` add `#[allow(clippy::too_long_first_doc_paragraph)]`
1313
help: add an empty line
1414
|
15-
LL ~ /// A very short summary.
15+
LL | /// A very short summary.
1616
LL + ///
1717
|
1818

src/tools/clippy/tests/ui/too_long_first_doc_paragraph.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ LL | | //! 200 characters so I needed to write something longeeeeeeer.
1212
= help: to override `-D warnings` add `#[allow(clippy::too_long_first_doc_paragraph)]`
1313
help: add an empty line
1414
|
15-
LL ~ //! A very short summary.
16-
LL + //!
15+
LL | //! A very short summary.
16+
LL ~ //!
1717
LL ~ //! A much longer explanation that goes into a lot more detail about
1818
|
1919

tests/ui/coverage-attr/bad-attr-ice.feat.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LL | #[coverage]
77
help: the following are the possible correct uses
88
|
99
LL | #[coverage(off)]
10-
|
10+
| +++++
1111
LL | #[coverage(on)]
12-
|
12+
| ++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/coverage-attr/bad-attr-ice.nofeat.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LL | #[coverage]
77
help: the following are the possible correct uses
88
|
99
LL | #[coverage(off)]
10-
|
10+
| +++++
1111
LL | #[coverage(on)]
12-
|
12+
| ++++
1313

1414
error[E0658]: the `#[coverage]` attribute is an experimental feature
1515
--> $DIR/bad-attr-ice.rs:11:1

tests/ui/coverage-attr/word-only.stderr

+24-24
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LL | #[coverage]
77
help: the following are the possible correct uses
88
|
99
LL | #[coverage(off)]
10-
|
10+
| +++++
1111
LL | #[coverage(on)]
12-
|
12+
| ++++
1313

1414
error: malformed `coverage` attribute input
1515
--> $DIR/word-only.rs:17:5
@@ -20,9 +20,9 @@ LL | #![coverage]
2020
help: the following are the possible correct uses
2121
|
2222
LL | #![coverage(off)]
23-
|
23+
| +++++
2424
LL | #![coverage(on)]
25-
|
25+
| ++++
2626

2727
error: malformed `coverage` attribute input
2828
--> $DIR/word-only.rs:21:1
@@ -33,9 +33,9 @@ LL | #[coverage]
3333
help: the following are the possible correct uses
3434
|
3535
LL | #[coverage(off)]
36-
|
36+
| +++++
3737
LL | #[coverage(on)]
38-
|
38+
| ++++
3939

4040
error: malformed `coverage` attribute input
4141
--> $DIR/word-only.rs:29:5
@@ -46,9 +46,9 @@ LL | #[coverage]
4646
help: the following are the possible correct uses
4747
|
4848
LL | #[coverage(off)]
49-
|
49+
| +++++
5050
LL | #[coverage(on)]
51-
|
51+
| ++++
5252

5353
error: malformed `coverage` attribute input
5454
--> $DIR/word-only.rs:26:1
@@ -59,9 +59,9 @@ LL | #[coverage]
5959
help: the following are the possible correct uses
6060
|
6161
LL | #[coverage(off)]
62-
|
62+
| +++++
6363
LL | #[coverage(on)]
64-
|
64+
| ++++
6565

6666
error: malformed `coverage` attribute input
6767
--> $DIR/word-only.rs:39:5
@@ -72,9 +72,9 @@ LL | #[coverage]
7272
help: the following are the possible correct uses
7373
|
7474
LL | #[coverage(off)]
75-
|
75+
| +++++
7676
LL | #[coverage(on)]
77-
|
77+
| ++++
7878

7979
error: malformed `coverage` attribute input
8080
--> $DIR/word-only.rs:44:5
@@ -85,9 +85,9 @@ LL | #[coverage]
8585
help: the following are the possible correct uses
8686
|
8787
LL | #[coverage(off)]
88-
|
88+
| +++++
8989
LL | #[coverage(on)]
90-
|
90+
| ++++
9191

9292
error: malformed `coverage` attribute input
9393
--> $DIR/word-only.rs:35:1
@@ -98,9 +98,9 @@ LL | #[coverage]
9898
help: the following are the possible correct uses
9999
|
100100
LL | #[coverage(off)]
101-
|
101+
| +++++
102102
LL | #[coverage(on)]
103-
|
103+
| ++++
104104

105105
error: malformed `coverage` attribute input
106106
--> $DIR/word-only.rs:53:5
@@ -111,9 +111,9 @@ LL | #[coverage]
111111
help: the following are the possible correct uses
112112
|
113113
LL | #[coverage(off)]
114-
|
114+
| +++++
115115
LL | #[coverage(on)]
116-
|
116+
| ++++
117117

118118
error: malformed `coverage` attribute input
119119
--> $DIR/word-only.rs:58:5
@@ -124,9 +124,9 @@ LL | #[coverage]
124124
help: the following are the possible correct uses
125125
|
126126
LL | #[coverage(off)]
127-
|
127+
| +++++
128128
LL | #[coverage(on)]
129-
|
129+
| ++++
130130

131131
error: malformed `coverage` attribute input
132132
--> $DIR/word-only.rs:50:1
@@ -137,9 +137,9 @@ LL | #[coverage]
137137
help: the following are the possible correct uses
138138
|
139139
LL | #[coverage(off)]
140-
|
140+
| +++++
141141
LL | #[coverage(on)]
142-
|
142+
| ++++
143143

144144
error: malformed `coverage` attribute input
145145
--> $DIR/word-only.rs:64:1
@@ -150,9 +150,9 @@ LL | #[coverage]
150150
help: the following are the possible correct uses
151151
|
152152
LL | #[coverage(off)]
153-
|
153+
| +++++
154154
LL | #[coverage(on)]
155-
|
155+
| ++++
156156

157157
error[E0788]: coverage attribute not allowed here
158158
--> $DIR/word-only.rs:21:1

tests/ui/error-codes/E0259.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | extern crate test as alloc;
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | extern crate test as other_alloc;
14-
|
14+
| ++++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/error-codes/E0260.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | mod alloc {
1111
help: you can use `as` to change the binding name of the import
1212
|
1313
LL | extern crate alloc as other_alloc;
14-
|
14+
| ++++++++++++++
1515

1616
error: aborting due to 1 previous error
1717

tests/ui/errors/issue-89280-emitter-overflow-splice-lines.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ LL | | )) {}
1212
= note: `#[warn(anonymous_parameters)]` on by default
1313
help: try naming the parameter or explicitly ignoring it
1414
|
15-
LL ~ fn test(x: u32, _: (
16-
LL +
17-
LL +
18-
LL ~ )) {}
19-
|
15+
LL | fn test(x: u32, _: (
16+
| ++
2017

2118
warning: 1 warning emitted
2219

tests/ui/fn/issue-3044.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ note: method defined here
1111
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
1212
help: provide the argument
1313
|
14-
LL ~ needlesArr.iter().fold(|x, y| {
15-
LL +
14+
LL | needlesArr.iter().fold(|x, y| {
15+
LL |
1616
LL ~ }, /* f */);
1717
|
1818

tests/ui/imports/issue-45799-bad-extern-crate-rename-suggestion-formatting.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | extern crate std;
88
help: you can use `as` to change the binding name of the import
99
|
1010
LL | extern crate std as other_std;
11-
|
11+
| ++++++++++++
1212

1313
error: aborting due to 1 previous error
1414

tests/ui/imports/issue-45829/rename-extern-vs-use.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | extern crate issue_45829_b as bar;
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | extern crate issue_45829_b as other_bar;
13-
|
13+
| ++++++
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/imports/issue-45829/rename-extern.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | extern crate issue_45829_b as issue_45829_a;
1010
help: you can use `as` to change the binding name of the import
1111
|
1212
LL | extern crate issue_45829_b as other_issue_45829_a;
13-
|
13+
| ++++++
1414

1515
error: aborting due to 1 previous error
1616

0 commit comments

Comments
 (0)