Skip to content

Commit 2ef10ac

Browse files
authored
Merge pull request #255 from Muscraft/sugg-same-as-source
fix: Show suggestion if same as source
2 parents 20b2070 + 212d620 commit 2ef10ac

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

src/renderer/mod.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ use std::borrow::Cow;
5555
use std::cmp::{max, min, Ordering, Reverse};
5656
use std::collections::{HashMap, VecDeque};
5757
use std::fmt;
58-
use std::ops::Range;
5958
use stylesheet::Stylesheet;
6059

6160
const ANONYMIZED_LINE_NUM: &str = "LL";
@@ -1911,9 +1910,7 @@ impl Renderer {
19111910
assert!(underline_start >= 0 && underline_end >= 0);
19121911
let padding: usize = max_line_num_len + 3;
19131912
for p in underline_start..underline_end {
1914-
if matches!(show_code_change, DisplaySuggestion::Underline)
1915-
&& is_different(sm, &part.replacement, part.span.clone())
1916-
{
1913+
if matches!(show_code_change, DisplaySuggestion::Underline) {
19171914
// If this is a replacement, underline with `~`, if this is an addition
19181915
// underline with `+`.
19191916
buffer.putc(
@@ -2955,14 +2952,6 @@ struct UnderlineParts {
29552952
multiline_bottom_right_with_text: char,
29562953
}
29572954

2958-
/// Whether the original and suggested code are the same.
2959-
pub(crate) fn is_different(sm: &SourceMap<'_>, suggested: &str, range: Range<usize>) -> bool {
2960-
match sm.span_to_snippet(range) {
2961-
Some(s) => s != suggested,
2962-
None => true,
2963-
}
2964-
}
2965-
29662955
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29672956
pub enum OutputTheme {
29682957
Ascii,

src/renderer/source_map.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::renderer::{char_width, is_different, num_overlap, LineAnnotation, LineAnnotationType};
1+
use crate::renderer::{char_width, num_overlap, LineAnnotation, LineAnnotationType};
22
use crate::{Annotation, AnnotationKind, Patch};
33
use std::borrow::Cow;
44
use std::cmp::{max, min};
@@ -474,16 +474,10 @@ impl<'a> SourceMap<'a> {
474474
_ => 1,
475475
})
476476
.sum();
477-
if !is_different(self, &part.replacement, part.span.clone()) {
478-
// Account for cases where we are suggesting the same code that's already
479-
// there. This shouldn't happen often, but in some cases for multipart
480-
// suggestions it's much easier to handle it here than in the origin.
481-
} else {
482-
line_highlight.push(SubstitutionHighlight {
483-
start: (cur_lo.char as isize + acc) as usize,
484-
end: (cur_lo.char as isize + acc + len) as usize,
485-
});
486-
}
477+
line_highlight.push(SubstitutionHighlight {
478+
start: (cur_lo.char as isize + acc) as usize,
479+
end: (cur_lo.char as isize + acc + len) as usize,
480+
});
487481
buf.push_str(&part.replacement);
488482
// Account for the difference between the width of the current code and the
489483
// snippet being suggested, so that the *later* suggestions are correctly

tests/formatter.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,3 +2952,55 @@ LL + .sum::<GENERIC_ARG>() //~ ERROR type annotations needed
29522952
let renderer = renderer.theme(OutputTheme::Unicode);
29532953
assert_data_eq!(renderer.render(input), expected_unicode);
29542954
}
2955+
2956+
#[test]
2957+
fn suggestion_same_as_source() {
2958+
let source = r#"// When the type of a method call's receiver is unknown, the span should point
2959+
// to the receiver (and not the entire call, as was previously the case before
2960+
// the fix of which this tests).
2961+
2962+
fn shines_a_beacon_through_the_darkness() {
2963+
let x: Option<_> = None; //~ ERROR type annotations needed
2964+
x.unwrap().method_that_could_exist_on_some_type();
2965+
}
2966+
2967+
fn courier_to_des_moines_and_points_west(data: &[u32]) -> String {
2968+
data.iter()
2969+
.sum::<_>() //~ ERROR type annotations needed
2970+
.to_string()
2971+
}
2972+
2973+
fn main() {}
2974+
"#;
2975+
2976+
let input = &[
2977+
Group::with_title(Level::ERROR.title("type annotations needed").id("E0282")).element(
2978+
Snippet::source(source)
2979+
.path("$DIR/issue-42234-unknown-receiver-type.rs")
2980+
.annotation(AnnotationKind::Primary.span(449..452).label(
2981+
"cannot infer type of the type parameter `S` declared on the method `sum`",
2982+
)),
2983+
),
2984+
Group::with_title(Level::HELP.title("consider specifying the generic argument")).element(
2985+
Snippet::source(source)
2986+
.path("$DIR/issue-42234-unknown-receiver-type.rs")
2987+
.line_start(12)
2988+
.fold(true)
2989+
.patch(Patch::new(452..457, "::<_>")),
2990+
),
2991+
];
2992+
let expected = str![[r#"
2993+
error[E0282]: type annotations needed
2994+
--> $DIR/issue-42234-unknown-receiver-type.rs:12:10
2995+
|
2996+
LL | .sum::<_>() //~ ERROR type annotations needed
2997+
| ^^^ cannot infer type of the type parameter `S` declared on the method `sum`
2998+
|
2999+
help: consider specifying the generic argument
3000+
|
3001+
LL | .sum::<_>() //~ ERROR type annotations needed
3002+
|
3003+
"#]];
3004+
let renderer = Renderer::plain().anonymized_line_numbers(true);
3005+
assert_data_eq!(renderer.render(input), expected);
3006+
}

0 commit comments

Comments
 (0)