Skip to content

fix: Show suggestion if same as source #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<usize>) -> bool {
match sm.span_to_snippet(range) {
Some(s) => s != suggested,
None => true,
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputTheme {
Ascii,
Expand Down
16 changes: 5 additions & 11 deletions src/renderer/source_map.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2952,3 +2952,55 @@ LL + .sum::<GENERIC_ARG>() //~ 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);
}