diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index c347dba..bb39495 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -55,7 +55,6 @@ use std::borrow::Cow; use std::cmp::{max, min, Ordering, Reverse}; use std::collections::{HashMap, VecDeque}; use std::fmt; -use std::ops::Range; use stylesheet::Stylesheet; const ANONYMIZED_LINE_NUM: &str = "LL"; @@ -1911,9 +1910,7 @@ impl Renderer { assert!(underline_start >= 0 && underline_end >= 0); let padding: usize = max_line_num_len + 3; for p in underline_start..underline_end { - if matches!(show_code_change, DisplaySuggestion::Underline) - && is_different(sm, &part.replacement, part.span.clone()) - { + if matches!(show_code_change, DisplaySuggestion::Underline) { // If this is a replacement, underline with `~`, if this is an addition // underline with `+`. buffer.putc( @@ -2955,14 +2952,6 @@ struct UnderlineParts { multiline_bottom_right_with_text: char, } -/// Whether the original and suggested code are the same. -pub(crate) fn is_different(sm: &SourceMap<'_>, suggested: &str, range: Range) -> bool { - match sm.span_to_snippet(range) { - Some(s) => s != suggested, - None => true, - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum OutputTheme { Ascii, diff --git a/src/renderer/source_map.rs b/src/renderer/source_map.rs index 44b773b..fdc4cd6 100644 --- a/src/renderer/source_map.rs +++ b/src/renderer/source_map.rs @@ -1,4 +1,4 @@ -use crate::renderer::{char_width, is_different, num_overlap, LineAnnotation, LineAnnotationType}; +use crate::renderer::{char_width, num_overlap, LineAnnotation, LineAnnotationType}; use crate::{Annotation, AnnotationKind, Patch}; use std::borrow::Cow; use std::cmp::{max, min}; @@ -474,16 +474,10 @@ impl<'a> SourceMap<'a> { _ => 1, }) .sum(); - if !is_different(self, &part.replacement, part.span.clone()) { - // Account for cases where we are suggesting the same code that's already - // there. This shouldn't happen often, but in some cases for multipart - // suggestions it's much easier to handle it here than in the origin. - } else { - line_highlight.push(SubstitutionHighlight { - start: (cur_lo.char as isize + acc) as usize, - end: (cur_lo.char as isize + acc + len) as usize, - }); - } + line_highlight.push(SubstitutionHighlight { + start: (cur_lo.char as isize + acc) as usize, + end: (cur_lo.char as isize + acc + len) as usize, + }); buf.push_str(&part.replacement); // Account for the difference between the width of the current code and the // snippet being suggested, so that the *later* suggestions are correctly diff --git a/tests/formatter.rs b/tests/formatter.rs index 5462130..6d2b895 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -2952,3 +2952,55 @@ LL + .sum::() //~ ERROR type annotations needed let renderer = renderer.theme(OutputTheme::Unicode); assert_data_eq!(renderer.render(input), expected_unicode); } + +#[test] +fn suggestion_same_as_source() { + let source = r#"// When the type of a method call's receiver is unknown, the span should point +// to the receiver (and not the entire call, as was previously the case before +// the fix of which this tests). + +fn shines_a_beacon_through_the_darkness() { + let x: Option<_> = None; //~ ERROR type annotations needed + x.unwrap().method_that_could_exist_on_some_type(); +} + +fn courier_to_des_moines_and_points_west(data: &[u32]) -> String { + data.iter() + .sum::<_>() //~ ERROR type annotations needed + .to_string() +} + +fn main() {} +"#; + + let input = &[ + Group::with_title(Level::ERROR.title("type annotations needed").id("E0282")).element( + Snippet::source(source) + .path("$DIR/issue-42234-unknown-receiver-type.rs") + .annotation(AnnotationKind::Primary.span(449..452).label( + "cannot infer type of the type parameter `S` declared on the method `sum`", + )), + ), + Group::with_title(Level::HELP.title("consider specifying the generic argument")).element( + Snippet::source(source) + .path("$DIR/issue-42234-unknown-receiver-type.rs") + .line_start(12) + .fold(true) + .patch(Patch::new(452..457, "::<_>")), + ), + ]; + let expected = str![[r#" +error[E0282]: type annotations needed + --> $DIR/issue-42234-unknown-receiver-type.rs:12:10 + | +LL | .sum::<_>() //~ ERROR type annotations needed + | ^^^ cannot infer type of the type parameter `S` declared on the method `sum` + | +help: consider specifying the generic argument + | +LL | .sum::<_>() //~ ERROR type annotations needed + | +"#]]; + let renderer = Renderer::plain().anonymized_line_numbers(true); + assert_data_eq!(renderer.render(input), expected); +}