Use span_suggestion in loops lints

This commit is contained in:
mcarton 2016-02-24 20:54:35 +01:00
parent 7b1a0a9434
commit 783437eef0
2 changed files with 24 additions and 9 deletions

View File

@ -271,14 +271,17 @@ impl LateLintPass for LoopsPass {
} else { } else {
expr_block(cx, &arms[0].body, Some(other_stuff.join("\n ")), "..") expr_block(cx, &arms[0].body, Some(other_stuff.join("\n ")), "..")
}; };
span_help_and_lint(cx, span_lint_and_then(cx,
WHILE_LET_LOOP, WHILE_LET_LOOP,
expr.span, expr.span,
"this loop could be written as a `while let` loop", "this loop could be written as a `while let` loop",
&format!("try\nwhile let {} = {} {}", |db| {
snippet(cx, arms[0].pats[0].span, ".."), let sug = format!("while let {} = {} {}",
snippet(cx, matchexpr.span, ".."), snippet(cx, arms[0].pats[0].span, ".."),
loop_body)); snippet(cx, matchexpr.span, ".."),
loop_body);
db.span_suggestion(expr.span, "try", sug);
});
} }
} }
_ => (), _ => (),

View File

@ -6,7 +6,10 @@
fn main() { fn main() {
let y = Some(true); 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 { if let Some(_x) = y {
let _v = 1; let _v = 1;
} else { } else {
@ -19,13 +22,19 @@ fn main() {
} }
break; 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 { match y {
Some(_x) => true, Some(_x) => true,
None => break 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 { let x = match y {
Some(x) => x, Some(x) => x,
None => break None => break
@ -33,7 +42,10 @@ fn main() {
let _x = x; let _x = x;
let _str = "foo"; 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 { let x = match y {
Some(x) => x, Some(x) => x,
None => break, None => break,