diff options
| author | Michael Goulet <michael@errs.io> | 2022-09-11 20:02:33 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2022-09-12 22:08:30 +0000 |
| commit | c2cff68d8445fc2bcaff060a8b78993939f33878 (patch) | |
| tree | 8f8ffb103a6ed90f2ea387cb3157f707a205c021 /compiler/rustc_errors/src | |
| parent | 370c816a71742373401fd3c75699c04f1ceaf81f (diff) | |
| download | rust-c2cff68d8445fc2bcaff060a8b78993939f33878.tar.gz rust-c2cff68d8445fc2bcaff060a8b78993939f33878.zip | |
Don't trim substitution if it's only whitespace
Diffstat (limited to 'compiler/rustc_errors/src')
| -rw-r--r-- | compiler/rustc_errors/src/emitter.rs | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 880006cf1fc..016404c5f67 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -268,7 +268,10 @@ pub trait Emitter: Translate { SuggestionStyle::ShowAlways, ].contains(&sugg.style) { - let substitution = &sugg.substitutions[0].parts[0].snippet.trim(); + // Don't trim the substitution if it's only whitespace changes + let substitution = &sugg.substitutions[0].parts[0].snippet; + let substitution = + if substitution.trim().is_empty() { substitution } else { substitution.trim() }; let msg = if substitution.is_empty() || sugg.style.hide_inline() { // This substitution is only removal OR we explicitly don't want to show the // code inline (`hide_inline`). Therefore, we don't show the substitution. @@ -1880,16 +1883,23 @@ impl EmitterWriter { let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display; let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display; + // If this addition is _only_ whitespace, then don't trim it, + // or else we're just not rendering anything. + let is_whitespace_addition = part.snippet.trim().is_empty(); + // Do not underline the leading... - let start = part.snippet.len().saturating_sub(part.snippet.trim_start().len()); + let start = if is_whitespace_addition { + 0 + } else { + part.snippet.len().saturating_sub(part.snippet.trim_start().len()) + }; // ...or trailing spaces. Account for substitutions containing unicode // characters. - let sub_len: usize = part - .snippet - .trim() - .chars() - .map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1)) - .sum(); + let sub_len: usize = + if is_whitespace_addition { &part.snippet } else { part.snippet.trim() } + .chars() + .map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1)) + .sum(); let offset: isize = offsets .iter() @@ -2130,7 +2140,7 @@ impl EmitterWriter { } } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] enum DisplaySuggestion { Underline, Diff, |
