Skip to content

Commit 4068ff4

Browse files
committed
Improve help message in needless_continue
1 parent c7979d3 commit 4068ff4

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

clippy_lints/src/needless_continue.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,16 @@ fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext<'_>, data: &
312312
let cond_code = snippet(ctx, data.if_cond.span, "..");
313313

314314
let continue_code = snippet_block(ctx, data.if_block.span, "..", Some(data.if_expr.span));
315-
// region B
315+
316316
let else_code = snippet_block(ctx, data.else_expr.span, "..", Some(data.if_expr.span));
317317

318318
let indent_if = indent_of(ctx, data.if_expr.span).unwrap_or(0);
319319
format!(
320-
"{}if {} {} {}",
321-
" ".repeat(indent_if),
320+
"{indent}if {} {}\n{indent}{}",
322321
cond_code,
323322
continue_code,
324323
else_code,
324+
indent = " ".repeat(indent_if),
325325
)
326326
}
327327

@@ -389,9 +389,9 @@ fn check_and_warn<'a>(ctx: &EarlyContext<'_>, expr: &'a ast::Expr) {
389389
});
390390
}
391391

392-
/// Eats at `s` from the end till a closing brace `}` is encountered, and then
393-
/// continues eating till a non-whitespace character is found.
394-
/// e.g., the string
392+
/// Eats at `s` from the end till a closing brace `}` is encountered, and then continues eating
393+
/// till a non-whitespace character is found. e.g., the string. If no closing `}` is present, the
394+
/// string will be preserved.
395395
///
396396
/// ```rust
397397
/// {
@@ -405,20 +405,21 @@ fn check_and_warn<'a>(ctx: &EarlyContext<'_>, expr: &'a ast::Expr) {
405405
/// {
406406
/// let x = 5;
407407
/// ```
408-
///
409-
/// NOTE: when there is no closing brace in `s`, `s` is _not_ preserved, i.e.,
410-
/// an empty string will be returned in that case.
411408
#[must_use]
412409
fn erode_from_back(s: &str) -> String {
413-
let mut ret = String::from(s);
410+
let mut ret = s.to_string();
414411
while ret.pop().map_or(false, |c| c != '}') {}
415412
while let Some(c) = ret.pop() {
416413
if !c.is_whitespace() {
417414
ret.push(c);
418415
break;
419416
}
420417
}
421-
ret
418+
if ret.is_empty() {
419+
s.to_string()
420+
} else {
421+
ret
422+
}
422423
}
423424

424425
fn span_of_first_expr_in_block(block: &ast::Block) -> Option<Span> {
@@ -428,6 +429,7 @@ fn span_of_first_expr_in_block(block: &ast::Block) -> Option<Span> {
428429
#[cfg(test)]
429430
mod test {
430431
use super::erode_from_back;
432+
431433
#[test]
432434
#[rustfmt::skip]
433435
fn test_erode_from_back() {
@@ -453,7 +455,7 @@ mod test {
453455
let x = 5;
454456
let y = something();
455457
";
456-
let expected = "";
458+
let expected = input;
457459
let got = erode_from_back(input);
458460
assert_eq!(expected, got);
459461
}

tests/ui/needless_continue.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ LL | | }
4848
= help: consider dropping the `else` clause
4949
if (zero!(i % 2) || nonzero!(i % 5)) && i % 3 != 0 {
5050
continue;
51-
} {
51+
}
52+
{
5253
println!("Blabber");
5354
println!("Jabber");
5455
}
@@ -89,7 +90,8 @@ LL | | }
8990
= help: consider dropping the `else` clause
9091
if condition() {
9192
continue; // should lint here
92-
} {
93+
}
94+
{
9395
println!("bar-5");
9496
}
9597

0 commit comments

Comments
 (0)