From 783437eef0380dff8e1a6a4d049366aa54011f48 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 24 Feb 2016 20:54:35 +0100 Subject: [PATCH] Use `span_suggestion` in loops lints --- src/loops.rs | 13 ++++++++----- tests/compile-fail/while_loop.rs | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/loops.rs b/src/loops.rs index acfb6c150d5..602de0eebcb 100644 --- a/src/loops.rs +++ b/src/loops.rs @@ -271,14 +271,17 @@ impl LateLintPass for LoopsPass { } else { expr_block(cx, &arms[0].body, Some(other_stuff.join("\n ")), "..") }; - span_help_and_lint(cx, + span_lint_and_then(cx, WHILE_LET_LOOP, expr.span, "this loop could be written as a `while let` loop", - &format!("try\nwhile let {} = {} {}", - snippet(cx, arms[0].pats[0].span, ".."), - snippet(cx, matchexpr.span, ".."), - loop_body)); + |db| { + let sug = format!("while let {} = {} {}", + snippet(cx, arms[0].pats[0].span, ".."), + snippet(cx, matchexpr.span, ".."), + loop_body); + db.span_suggestion(expr.span, "try", sug); + }); } } _ => (), diff --git a/tests/compile-fail/while_loop.rs b/tests/compile-fail/while_loop.rs index ee8e4622e0e..bbb76cfbfaf 100644 --- a/tests/compile-fail/while_loop.rs +++ b/tests/compile-fail/while_loop.rs @@ -6,7 +6,10 @@ fn main() { let y = Some(true); - loop { //~ERROR + loop { + //~^ERROR this loop could be written as a `while let` loop + //~|HELP try + //~|SUGGESTION while let Some(_x) = y { if let Some(_x) = y { let _v = 1; } else { @@ -19,13 +22,19 @@ fn main() { } break; } - loop { //~ERROR + loop { + //~^ERROR this loop could be written as a `while let` loop + //~|HELP try + //~|SUGGESTION while let Some(_x) = y { match y { Some(_x) => true, None => break }; } - loop { //~ERROR + loop { + //~^ERROR this loop could be written as a `while let` loop + //~|HELP try + //~|SUGGESTION while let Some(x) = y { let x = match y { Some(x) => x, None => break @@ -33,7 +42,10 @@ fn main() { let _x = x; let _str = "foo"; } - loop { //~ERROR + loop { + //~^ERROR this loop could be written as a `while let` loop + //~|HELP try + //~|SUGGESTION while let Some(x) = y { let x = match y { Some(x) => x, None => break,