Skip to content

fix: Match rustc's multiline reordering #215

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
Jun 12, 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
3 changes: 1 addition & 2 deletions src/renderer/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ impl<'a> SourceMap<'a> {
let mut primary_spans = vec![];

// Find overlapping multiline annotations, put them at different depths
multiline_annotations
.sort_by_key(|ml| (ml.start.line, usize::MAX - ml.end.line, ml.start.byte));
multiline_annotations.sort_by_key(|ml| (ml.start.line, usize::MAX - ml.end.line));
for ann in multiline_annotations.clone() {
if ann.kind.is_primary() {
primary_spans.push((ann.start, ann.end));
Expand Down
16 changes: 8 additions & 8 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,15 +887,15 @@ bar = { version = "0.1.0", optional = true }
error: unused optional dependency
|
4 | bar = { version = "0.1.0", optional = true }
| _________^__________________--------------^
| | | |
| |_________| This should also be long but not too long
| __________^__________________--------------^
| | | |
| | _________| This should also be long but not too long
| ||
5 | || this is another line
6 | || so is this
7 | || bar = { version = "0.1.0", optional = true }
| ||_________________________^________________^ I need this to be really long so I can test overlaps
| |__________________________|
| |_________________________|
| I need this to be really long so I can test overlaps
"#]];
let renderer = Renderer::plain();
Expand Down Expand Up @@ -940,16 +940,16 @@ this is another line
error: unused optional dependency
|
4 | bar = { version = "0.1.0", optional = true }
| __________^__________________--------------^
| | | |
| |__________| This should also be long but not too long
| ___________^__________________--------------^
| | | |
| | __________| This should also be long but not too long
| ||
5 | || this is another line
| || ____^
6 | ||| so is this
7 | ||| bar = { version = "0.1.0", optional = true }
| |||_________________________^________________^ I need this to be really long so I can test overlaps
| |_|_________________________|
| ||_________________________|
| | I need this to be really long so I can test overlaps
8 | | this is another line
| |____^ I need this to be really long so I can test overlaps
Expand Down
93 changes: 92 additions & 1 deletion tests/rustc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! [parser-tests]: https://github.com/rust-lang/rust/blob/894f7a4ba6554d3797404bbf550d9919df060b97/compiler/rustc_parse/src/parser/tests.rs

use annotate_snippets::{AnnotationKind, Group, Level, Origin, Renderer, Snippet};
use annotate_snippets::{AnnotationKind, Group, Level, Origin, Patch, Renderer, Snippet};

use annotate_snippets::renderer::OutputTheme;
use snapbox::{assert_data_eq, str};
Expand Down Expand Up @@ -2020,3 +2020,94 @@ LL | ... = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0, 0, 0, 0, 0, 0, 0, 0, 0
.term_width(120);
assert_data_eq!(renderer.render(input), expected);
}

#[test]
fn lint_map_unit_fn() {
// tests/ui/lint/lint_map_unit_fn.rs
let source = r#"#![deny(map_unit_fn)]

fn foo(items: &mut Vec<u8>) {
items.sort();
}

fn main() {
let mut x: Vec<Vec<u8>> = vec![vec![0, 2, 1], vec![5, 4, 3]];
x.iter_mut().map(foo);
//~^ ERROR `Iterator::map` call that discard the iterator's values
x.iter_mut().map(|items| {
//~^ ERROR `Iterator::map` call that discard the iterator's values
items.sort();
});
let f = |items: &mut Vec<u8>| {
items.sort();
};
x.iter_mut().map(f);
//~^ ERROR `Iterator::map` call that discard the iterator's values
}
"#;

let input = Level::ERROR
.header("`Iterator::map` call that discard the iterator's values")
.group(
Group::new()
.element(
Snippet::source(source)
.origin("$DIR/lint_map_unit_fn.rs")
.fold(true)
.annotation(AnnotationKind::Context.span(271..278).label(
"this function returns `()`, which is likely not what you wanted",
))
.annotation(
AnnotationKind::Context
.span(271..379)
.label("called `Iterator::map` with callable that returns `()`"),
)
.annotation(
AnnotationKind::Context
.span(267..380)
.label("after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items")
)
.annotation(AnnotationKind::Primary.span(267..380)),
)
.element(
Level::NOTE.title("`Iterator::map`, like many of the methods on `Iterator`, gets executed lazily, meaning that its effects won't be visible until it is iterated")),
)
.group(
Group::new()
.element(Level::HELP.title("you might have meant to use `Iterator::for_each`"))
.element(
Snippet::source(source)
.origin("$DIR/lint_map_unit_fn.rs")
.fold(true)
.patch(Patch::new(267..270, r#"for_each"#)),
),
);

let expected = str![[r#"
error: `Iterator::map` call that discard the iterator's values
--> $DIR/lint_map_unit_fn.rs:11:18
|
LL | x.iter_mut().map(|items| {
| ^ -------
| | |
| ____________________|___this function returns `()`, which is likely not what you wanted
| | __________________|
| | |
LL | | | //~^ ERROR `Iterator::map` call that discard the iterator's values
LL | | | items.sort();
LL | | | });
| | | -^ after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items
| | |_____||
| |_______|
| called `Iterator::map` with callable that returns `()`
|
= note: `Iterator::map`, like many of the methods on `Iterator`, gets executed lazily, meaning that its effects won't be visible until it is iterated
help: you might have meant to use `Iterator::for_each`
|
LL - x.iter_mut().map(|items| {
LL + x.iter_mut().for_each(|items| {
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(true);
assert_data_eq!(renderer.render(input), expected);
}