Skip to content

Commit b461247

Browse files
committed
test: Suggestion span beyond source
1 parent 6b314dd commit b461247

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

tests/formatter.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,3 +2635,140 @@ fn empty_span_start_line() {
26352635
let renderer = Renderer::plain();
26362636
assert_data_eq!(renderer.render(input), expected);
26372637
}
2638+
2639+
#[test]
2640+
fn suggestion_span_one_bigger_than_source() {
2641+
let snippet_source = r#"#![allow(unused)]
2642+
fn main() {
2643+
[1, 2, 3].into_iter().for_each(|n| { *n; });
2644+
}
2645+
"#;
2646+
2647+
let suggestion_source = r#"[1, 2, 3].into_iter().for_each(|n| { *n; });
2648+
"#;
2649+
2650+
let long_title1 ="this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021";
2651+
let long_title2 = "for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>";
2652+
let long_title3 = "or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value";
2653+
2654+
let input = &[
2655+
Group::with_title(Level::WARNING.title(long_title1))
2656+
.element(
2657+
Snippet::source(snippet_source)
2658+
.path("lint_example.rs")
2659+
.annotation(AnnotationKind::Primary.span(40..49)),
2660+
)
2661+
.element(Level::WARNING.message("this changes meaning in Rust 2021"))
2662+
.element(Level::NOTE.message(long_title2))
2663+
.element(Level::NOTE.message("`#[warn(array_into_iter)]` on by default")),
2664+
Group::with_title(
2665+
Level::HELP.title("use `.iter()` instead of `.into_iter()` to avoid ambiguity"),
2666+
)
2667+
.element(
2668+
Snippet::source(suggestion_source)
2669+
.path("lint_example.rs")
2670+
.line_start(3)
2671+
.patch(Patch::new(10..19, "iter")),
2672+
),
2673+
Group::with_title(Level::HELP.title(long_title3)).element(
2674+
Snippet::source(suggestion_source)
2675+
.path("lint_example.rs")
2676+
.line_start(3)
2677+
.patch(Patch::new(
2678+
suggestion_source.len() + 1..suggestion_source.len() + 1,
2679+
"IntoIterator::into_iter(",
2680+
)),
2681+
),
2682+
];
2683+
2684+
let expected = str![[r#"
2685+
warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021
2686+
--> lint_example.rs:3:11
2687+
|
2688+
3 | [1, 2, 3].into_iter().for_each(|n| { *n; });
2689+
| ^^^^^^^^^
2690+
|
2691+
= warning: this changes meaning in Rust 2021
2692+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
2693+
= note: `#[warn(array_into_iter)]` on by default
2694+
help: use `.iter()` instead of `.into_iter()` to avoid ambiguity
2695+
|
2696+
3 - [1, 2, 3].into_iter().for_each(|n| { *n; });
2697+
3 + [1, 2, 3].iter().for_each(|n| { *n; });
2698+
|
2699+
help: or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value
2700+
|
2701+
3 | IntoIterator::into_iter(
2702+
|
2703+
"#]];
2704+
let renderer = Renderer::plain();
2705+
assert_data_eq!(renderer.render(input), expected);
2706+
}
2707+
2708+
#[test]
2709+
fn suggestion_span_bigger_than_source() {
2710+
let snippet_source = r#"#![allow(unused)]
2711+
fn main() {
2712+
[1, 2, 3].into_iter().for_each(|n| { *n; });
2713+
}
2714+
"#;
2715+
let suggestion_source = r#"[1, 2, 3].into_iter().for_each(|n| { *n; });
2716+
"#;
2717+
2718+
let long_title1 ="this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021";
2719+
let long_title2 = "for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>";
2720+
let long_title3 = "or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value";
2721+
2722+
let input = &[
2723+
Group::with_title(Level::WARNING.title(long_title1))
2724+
.element(
2725+
Snippet::source(snippet_source)
2726+
.path("lint_example.rs")
2727+
.annotation(AnnotationKind::Primary.span(40..49)),
2728+
)
2729+
.element(Level::WARNING.message("this changes meaning in Rust 2021"))
2730+
.element(Level::NOTE.message(long_title2))
2731+
.element(Level::NOTE.message("`#[warn(array_into_iter)]` on by default")),
2732+
Group::with_title(
2733+
Level::HELP.title("use `.iter()` instead of `.into_iter()` to avoid ambiguity"),
2734+
)
2735+
.element(
2736+
Snippet::source(suggestion_source)
2737+
.path("lint_example.rs")
2738+
.line_start(3)
2739+
.patch(Patch::new(10..19, "iter")),
2740+
),
2741+
Group::with_title(Level::HELP.title(long_title3)).element(
2742+
Snippet::source(suggestion_source)
2743+
.path("lint_example.rs")
2744+
.line_start(3)
2745+
.patch(Patch::new(
2746+
suggestion_source.len() + 2..suggestion_source.len() + 2,
2747+
"IntoIterator::into_iter(",
2748+
)),
2749+
),
2750+
];
2751+
2752+
let expected = str![[r#"
2753+
warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021
2754+
--> lint_example.rs:3:11
2755+
|
2756+
3 | [1, 2, 3].into_iter().for_each(|n| { *n; });
2757+
| ^^^^^^^^^
2758+
|
2759+
= warning: this changes meaning in Rust 2021
2760+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/IntoIterator-for-arrays.html>
2761+
= note: `#[warn(array_into_iter)]` on by default
2762+
help: use `.iter()` instead of `.into_iter()` to avoid ambiguity
2763+
|
2764+
3 - [1, 2, 3].into_iter().for_each(|n| { *n; });
2765+
3 + [1, 2, 3].iter().for_each(|n| { *n; });
2766+
|
2767+
help: or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value
2768+
|
2769+
3 | IntoIterator::into_iter(
2770+
|
2771+
"#]];
2772+
let renderer = Renderer::plain();
2773+
assert_data_eq!(renderer.render(input), expected);
2774+
}

0 commit comments

Comments
 (0)