Skip to content

Commit 6d71251

Browse files
compiler-errorsworkingjubilee
authored andcommitted
Trim suggestion parts to the subset that is purely additive
1 parent f6406df commit 6d71251

File tree

148 files changed

+321
-304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+321
-304
lines changed

compiler/rustc_errors/src/emitter.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1976,9 +1976,11 @@ impl HumanEmitter {
19761976
Some(Style::HeaderMsg),
19771977
);
19781978

1979+
let other_suggestions = suggestions.len().saturating_sub(MAX_SUGGESTIONS);
1980+
19791981
let mut row_num = 2;
19801982
for (i, (complete, parts, highlights, _)) in
1981-
suggestions.iter().enumerate().take(MAX_SUGGESTIONS)
1983+
suggestions.into_iter().enumerate().take(MAX_SUGGESTIONS)
19821984
{
19831985
debug!(?complete, ?parts, ?highlights);
19841986

@@ -2168,7 +2170,7 @@ impl HumanEmitter {
21682170
self.draw_code_line(
21692171
&mut buffer,
21702172
&mut row_num,
2171-
highlight_parts,
2173+
&highlight_parts,
21722174
line_pos + line_start,
21732175
line,
21742176
show_code_change,
@@ -2214,7 +2216,12 @@ impl HumanEmitter {
22142216
if let DisplaySuggestion::Diff | DisplaySuggestion::Underline | DisplaySuggestion::Add =
22152217
show_code_change
22162218
{
2217-
for part in parts {
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+
22182225
let snippet = if let Ok(snippet) = sm.span_to_snippet(part.span) {
22192226
snippet
22202227
} else {
@@ -2377,9 +2384,12 @@ impl HumanEmitter {
23772384
row_num = row + 1;
23782385
}
23792386
}
2380-
if suggestions.len() > MAX_SUGGESTIONS {
2381-
let others = suggestions.len() - MAX_SUGGESTIONS;
2382-
let msg = format!("and {} other candidate{}", others, pluralize!(others));
2387+
if other_suggestions > 0 {
2388+
let msg = format!(
2389+
"and {} other candidate{}",
2390+
other_suggestions,
2391+
pluralize!(other_suggestions)
2392+
);
23832393
buffer.puts(row_num, max_line_num_len + 3, &msg, Style::NoStyle);
23842394
}
23852395

compiler/rustc_errors/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ impl SubstitutionPart {
246246
sm.span_to_snippet(self.span)
247247
.map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())
248248
}
249+
250+
/// Try to turn a replacement into an addition when the span that is being
251+
/// overwritten matches either the prefix or suffix of the replacement.
252+
fn trim_trivial_replacements(&mut self, sm: &SourceMap) {
253+
if self.snippet.is_empty() {
254+
return;
255+
}
256+
let Ok(snippet) = sm.span_to_snippet(self.span) else {
257+
return;
258+
};
259+
if self.snippet.starts_with(&snippet) {
260+
self.span = self.span.shrink_to_hi();
261+
self.snippet = self.snippet[snippet.len()..].to_string();
262+
} else if self.snippet.ends_with(&snippet) {
263+
self.span = self.span.shrink_to_lo();
264+
self.snippet = self.snippet[..self.snippet.len() - snippet.len()].to_string();
265+
}
266+
}
249267
}
250268

251269
impl CodeSuggestion {

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

+16-26
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ LL | true
88
= help: to override `-D warnings` add `#[allow(clippy::implicit_return)]`
99
help: add `return` as shown
1010
|
11-
LL - true
12-
LL + return true
11+
LL | return true
1312
|
1413

1514
error: missing `return` statement
@@ -20,9 +19,8 @@ LL | if true { true } else { false }
2019
|
2120
help: add `return` as shown
2221
|
23-
LL - if true { true } else { false }
24-
LL + if true { return true } else { false }
25-
|
22+
LL | if true { return true } else { false }
23+
| ++++++
2624

2725
error: missing `return` statement
2826
--> tests/ui/implicit_return.rs:19:29
@@ -32,9 +30,8 @@ LL | if true { true } else { false }
3230
|
3331
help: add `return` as shown
3432
|
35-
LL - if true { true } else { false }
36-
LL + if true { true } else { return false }
37-
|
33+
LL | if true { true } else { return false }
34+
| ++++++
3835

3936
error: missing `return` statement
4037
--> tests/ui/implicit_return.rs:25:17
@@ -44,9 +41,8 @@ LL | true => false,
4441
|
4542
help: add `return` as shown
4643
|
47-
LL - true => false,
48-
LL + true => return false,
49-
|
44+
LL | true => return false,
45+
| ++++++
5046

5147
error: missing `return` statement
5248
--> tests/ui/implicit_return.rs:26:20
@@ -56,9 +52,8 @@ LL | false => { true },
5652
|
5753
help: add `return` as shown
5854
|
59-
LL - false => { true },
60-
LL + false => { return true },
61-
|
55+
LL | false => { return true },
56+
| ++++++
6257

6358
error: missing `return` statement
6459
--> tests/ui/implicit_return.rs:39:9
@@ -104,9 +99,8 @@ LL | let _ = || { true };
10499
|
105100
help: add `return` as shown
106101
|
107-
LL - let _ = || { true };
108-
LL + let _ = || { return true };
109-
|
102+
LL | let _ = || { return true };
103+
| ++++++
110104

111105
error: missing `return` statement
112106
--> tests/ui/implicit_return.rs:73:16
@@ -116,9 +110,8 @@ LL | let _ = || true;
116110
|
117111
help: add `return` as shown
118112
|
119-
LL - let _ = || true;
120-
LL + let _ = || return true;
121-
|
113+
LL | let _ = || return true;
114+
| ++++++
122115

123116
error: missing `return` statement
124117
--> tests/ui/implicit_return.rs:81:5
@@ -128,8 +121,7 @@ LL | format!("test {}", "test")
128121
|
129122
help: add `return` as shown
130123
|
131-
LL - format!("test {}", "test")
132-
LL + return format!("test {}", "test")
124+
LL | return format!("test {}", "test")
133125
|
134126

135127
error: missing `return` statement
@@ -140,8 +132,7 @@ LL | m!(true, false)
140132
|
141133
help: add `return` as shown
142134
|
143-
LL - m!(true, false)
144-
LL + return m!(true, false)
135+
LL | return m!(true, false)
145136
|
146137

147138
error: missing `return` statement
@@ -191,8 +182,7 @@ LL | true
191182
|
192183
help: add `return` as shown
193184
|
194-
LL - true
195-
LL + return true
185+
LL | return true
196186
|
197187

198188
error: aborting due to 16 previous errors

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ LL | MAX;
6868
|
6969
help: use the associated constant instead
7070
|
71-
LL - MAX;
72-
LL + u32::MAX;
73-
|
71+
LL | u32::MAX;
72+
| +++++
7473

7574
error: usage of a legacy numeric method
7675
--> tests/ui/legacy_numeric_constants.rs:49:10

tests/ui/associated-types/defaults-suitability.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ LL | type Baz = T;
135135
help: consider further restricting type parameter `T` with trait `Clone`
136136
|
137137
LL | Self::Baz: Clone, T: std::clone::Clone
138-
| ~~~~~~~~~~~~~~~~~~~~~~
138+
| ++++++++++++++++++++
139139

140140
error: aborting due to 8 previous errors
141141

tests/ui/associated-types/defaults-suitability.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ LL | type Baz = T;
135135
help: consider further restricting type parameter `T` with trait `Clone`
136136
|
137137
LL | Self::Baz: Clone, T: std::clone::Clone
138-
| ~~~~~~~~~~~~~~~~~~~~~~
138+
| ++++++++++++++++++++
139139

140140
error: aborting due to 8 previous errors
141141

tests/ui/associated-types/issue-38821.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | impl<T: NotNull> IntoNullable for T {
1414
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement
1515
|
1616
LL | Expr: Expression<SqlType=<Col::SqlType as IntoNullable>::Nullable>, <Col as Expression>::SqlType: NotNull
17-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17+
| +++++++++++++++++++++++++++++++++++++
1818

1919
error[E0277]: the trait bound `<Col as Expression>::SqlType: NotNull` is not satisfied
2020
--> $DIR/issue-38821.rs:40:1
@@ -38,7 +38,7 @@ LL | impl<T: NotNull> IntoNullable for T {
3838
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement
3939
|
4040
LL | Expr: Expression<SqlType=<Col::SqlType as IntoNullable>::Nullable>, <Col as Expression>::SqlType: NotNull
41-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41+
| +++++++++++++++++++++++++++++++++++++
4242

4343
error[E0277]: the trait bound `<Col as Expression>::SqlType: NotNull` is not satisfied
4444
--> $DIR/issue-38821.rs:23:10

tests/ui/associated-types/issue-54108.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | type Size: Add<Output = Self::Size>;
1313
help: consider further restricting the associated type
1414
|
1515
LL | T: SubEncoder, <T as SubEncoder>::ActualSize: Add
16-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16+
| ++++++++++++++++++++++++++++++++++
1717

1818
error: aborting due to 1 previous error
1919

tests/ui/associated-types/issue-54108.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | type Size: Add<Output = Self::Size>;
1313
help: consider further restricting the associated type
1414
|
1515
LL | T: SubEncoder, <T as SubEncoder>::ActualSize: Add
16-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16+
| ++++++++++++++++++++++++++++++++++
1717

1818
error: aborting due to 1 previous error
1919

tests/ui/attributes/rustc_confusables.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ LL | x.inser();
3636
help: there is a method `insert` with a similar name
3737
|
3838
LL | x.insert();
39-
| ~~~~~~
39+
| +
4040

4141
error[E0599]: no method named `foo` found for struct `rustc_confusables_across_crate::BTreeSet` in the current scope
4242
--> $DIR/rustc_confusables.rs:15:7

tests/ui/attributes/rustc_confusables_std_cases.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ LL | let mut x = VecDeque::new();
3939
help: you might have meant to use `push_back`
4040
|
4141
LL | x.push_back(1);
42-
| ~~~~~~~~~
42+
| +++++
4343

4444
error[E0599]: no method named `length` found for struct `Vec<{integer}>` in the current scope
4545
--> $DIR/rustc_confusables_std_cases.rs:15:7
@@ -98,7 +98,7 @@ note: method defined here
9898
help: you might have meant to use `push_str`
9999
|
100100
LL | String::new().push_str("");
101-
| ~~~~~~~~
101+
| ++++
102102

103103
error[E0599]: no method named `append` found for struct `String` in the current scope
104104
--> $DIR/rustc_confusables_std_cases.rs:24:19

tests/ui/borrowck/issue-115259-suggest-iter-mut.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | self.layers.iter().fold(0, |result, mut layer| result + layer.proce
99
help: you may want to use `iter_mut` here
1010
|
1111
LL | self.layers.iter_mut().fold(0, |result, mut layer| result + layer.process())
12-
| ~~~~~~~~
12+
| ++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/issue-62387-suggest-iter-mut-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | vec.iter().flat_map(|container| container.things()).cloned().co
99
help: you may want to use `iter_mut` here
1010
|
1111
LL | vec.iter_mut().flat_map(|container| container.things()).cloned().collect::<Vec<PathBuf>>();
12-
| ~~~~~~~~
12+
| ++++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/borrowck/issue-62387-suggest-iter-mut.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | v.iter().for_each(|a| a.double());
99
help: you may want to use `iter_mut` here
1010
|
1111
LL | v.iter_mut().for_each(|a| a.double());
12-
| ~~~~~~~~
12+
| ++++
1313

1414
error[E0596]: cannot borrow `*a` as mutable, as it is behind a `&` reference
1515
--> $DIR/issue-62387-suggest-iter-mut.rs:25:39
@@ -22,7 +22,7 @@ LL | v.iter().rev().rev().for_each(|a| a.double());
2222
help: you may want to use `iter_mut` here
2323
|
2424
LL | v.iter_mut().rev().rev().for_each(|a| a.double());
25-
| ~~~~~~~~
25+
| ++++
2626

2727
error: aborting due to 2 previous errors
2828

tests/ui/c-variadic/issue-86053-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ LL | self , ... , self , self , ... ) where F : FnOnce ( & 'a & 'b usize
6464
help: a trait with a similar name exists
6565
|
6666
LL | self , ... , self , self , ... ) where Fn : FnOnce ( & 'a & 'b usize ) {
67-
| ~~
67+
| +
6868
help: you might be missing a type parameter
6969
|
7070
LL | fn ordering4 < 'a , 'b, F > ( a : , self , self , self ,

tests/ui/cfg/cfg-method-receiver.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | cbor_map! { #[cfg(test)] 4};
1717
help: you must specify a concrete type for this numeric value, like `i32`
1818
|
1919
LL | cbor_map! { #[cfg(test)] 4_i32};
20-
| ~~~~~
20+
| ++++
2121

2222
error: aborting due to 2 previous errors
2323

tests/ui/check-cfg/diagnotics.cargo.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | #[cfg(featur = "foo")]
1818
help: there is a config with a similar name and value
1919
|
2020
LL | #[cfg(feature = "foo")]
21-
| ~~~~~~~
21+
| +
2222

2323
warning: unexpected `cfg` condition name: `featur`
2424
--> $DIR/diagnotics.rs:17:7

tests/ui/check-cfg/diagnotics.rustc.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | #[cfg(featur = "foo")]
2020
help: there is a config with a similar name and value
2121
|
2222
LL | #[cfg(feature = "foo")]
23-
| ~~~~~~~
23+
| +
2424

2525
warning: unexpected `cfg` condition name: `featur`
2626
--> $DIR/diagnotics.rs:17:7

tests/ui/closures/2229_closure_analysis/bad-pattern.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ LL | let PAT = v1;
109109
help: introduce a variable instead
110110
|
111111
LL | let PAT_var = v1;
112-
| ~~~~~~~
112+
| ++++
113113

114114
error: aborting due to 7 previous errors
115115

tests/ui/closures/2229_closure_analysis/issue-118144.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | V(x) = func_arg;
99
help: consider dereferencing to access the inner value using the Deref trait
1010
|
1111
LL | V(x) = &*func_arg;
12-
| ~~~~~~~~~~
12+
| ++
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/closures/issue-78720.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | _func: F,
1616
help: a trait with a similar name exists
1717
|
1818
LL | _func: Fn,
19-
| ~~
19+
| +
2020
help: you might be missing a type parameter
2121
|
2222
LL | struct Map2<Segment2, F> {

tests/ui/compare-method/bad-self-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | fn poll(self, _: &mut Context<'_>) -> Poll<()> {
99
help: change the self-receiver type to match the trait
1010
|
1111
LL | fn poll(self: Pin<&mut MyFuture>, _: &mut Context<'_>) -> Poll<()> {
12-
| ~~~~~~~~~~~~~~~~~~~~~~~~
12+
| ++++++++++++++++++++
1313

1414
error[E0053]: method `foo` has an incompatible type for trait
1515
--> $DIR/bad-self-type.rs:22:18

tests/ui/const-generics/ensure_is_evaluatable.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | [(); N + 1]:,
1515
help: try adding a `where` bound
1616
|
1717
LL | [(); M + 1]:, [(); N + 1]:
18-
| ~~~~~~~~~~~~~~
18+
| ++++++++++++
1919

2020
error: aborting due to 1 previous error
2121

0 commit comments

Comments
 (0)