From 679fc128583e7831caa2e7826707f390628d6d94 Mon Sep 17 00:00:00 2001 From: Juan Aguilar Santillana Date: Tue, 12 May 2020 21:26:38 +0200 Subject: [PATCH 1/2] Fix unnecessary mutable at convert snippet::Slice --- src/display_list/from_snippet.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/display_list/from_snippet.rs b/src/display_list/from_snippet.rs index b3ba5a3..e75a861 100644 --- a/src/display_list/from_snippet.rs +++ b/src/display_list/from_snippet.rs @@ -104,15 +104,15 @@ fn format_annotation(annotation: snippet::Annotation<'_>) -> Vec } fn format_slice( - mut slice: snippet::Slice<'_>, + slice: snippet::Slice<'_>, is_first: bool, has_footer: bool, ) -> Vec> { let main_range = slice.annotations.get(0).map(|x| x.range.0); - let row = slice.line_start; - let origin = slice.origin.take(); + let origin = slice.origin; + let line_start = slice.line_start; let mut body = format_body(slice, has_footer); - let header = format_header(origin, main_range, row, &body, is_first); + let header = format_header(origin, main_range, line_start, &body, is_first); let mut result = vec![]; if let Some(header) = header { From 147a823196f1c34114beae47a1c8be38402dc374 Mon Sep 17 00:00:00 2001 From: Juan Aguilar Santillana Date: Tue, 12 May 2020 23:26:40 +0200 Subject: [PATCH 2/2] Removing duplicate Slice origins and surrounding empty lines --- src/display_list/from_snippet.rs | 51 ++++++++++++++++++---------- tests/fixtures/no-color/issue_9.toml | 28 +++++++++++++++ tests/fixtures/no-color/issue_9.txt | 12 +++++++ tests/snippet/mod.rs | 2 ++ 4 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 tests/fixtures/no-color/issue_9.toml create mode 100644 tests/fixtures/no-color/issue_9.txt diff --git a/src/display_list/from_snippet.rs b/src/display_list/from_snippet.rs index e75a861..da0deea 100644 --- a/src/display_list/from_snippet.rs +++ b/src/display_list/from_snippet.rs @@ -111,7 +111,8 @@ fn format_slice( let main_range = slice.annotations.get(0).map(|x| x.range.0); let origin = slice.origin; let line_start = slice.line_start; - let mut body = format_body(slice, has_footer); + let need_empty_header = origin.is_some() || is_first; + let mut body = format_body(slice, need_empty_header, has_footer); let header = format_header(origin, main_range, line_start, &body, is_first); let mut result = vec![]; @@ -122,6 +123,12 @@ fn format_slice( result } +#[inline] +// TODO: option_zip +fn zip_opt(a: Option, b: Option) -> Option<(A, B)> { + a.and_then(|a| b.map(|b| (a, b))) +} + fn format_header<'a>( origin: Option<&'a str>, main_range: Option, @@ -135,7 +142,7 @@ fn format_header<'a>( DisplayHeaderType::Continuation }; - if let Some(main_range) = main_range { + if let Some((main_range, path)) = zip_opt(main_range, origin) { let mut col = 1; for item in body { @@ -151,14 +158,14 @@ fn format_header<'a>( row += 1; } } - if let Some(path) = origin { - return Some(DisplayLine::Raw(DisplayRawLine::Origin { - path, - pos: Some((row, col)), - header_type: display_header, - })); - } + + return Some(DisplayLine::Raw(DisplayRawLine::Origin { + path, + pos: Some((row, col)), + header_type: display_header, + })); } + if let Some(path) = origin { return Some(DisplayLine::Raw(DisplayRawLine::Origin { path, @@ -166,6 +173,7 @@ fn format_header<'a>( header_type: display_header, })); } + None } @@ -261,7 +269,11 @@ fn fold_body(mut body: Vec>) -> Vec> { new_body } -fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec> { +fn format_body( + slice: snippet::Slice<'_>, + need_empty_header: bool, + has_footer: bool, +) -> Vec> { let source_len = slice.source.chars().count(); if let Some(bigger) = slice.annotations.iter().find_map(|x| { if source_len < x.range.1 { @@ -445,14 +457,17 @@ fn format_body(slice: snippet::Slice<'_>, has_footer: bool) -> Vec /code/rust/src/test/ui/annotate-snippet/suggestion.rs:4:5 + | +4 | let x = vec![1]; + | - move occurs because `x` has type `std::vec::Vec`, which does not implement the `Copy` trait + | +7 | let y = x; + | - value moved here + | +9 | x; + | ^ value used here after move + | \ No newline at end of file diff --git a/tests/snippet/mod.rs b/tests/snippet/mod.rs index c334874..cc747a4 100644 --- a/tests/snippet/mod.rs +++ b/tests/snippet/mod.rs @@ -53,7 +53,9 @@ where #[derive(Deserialize)] #[serde(remote = "FormatOptions")] pub struct FormatOptionsDef { + #[serde(default)] pub color: bool, + #[serde(default)] pub anonymized_line_numbers: bool, }