Skip to content

Commit

Permalink
Improve help message in needless_continue
Browse files Browse the repository at this point in the history
  • Loading branch information
flip1995 committed Feb 6, 2020
1 parent c7979d3 commit 4068ff4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
26 changes: 14 additions & 12 deletions clippy_lints/src/needless_continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,16 @@ fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext<'_>, data: &
let cond_code = snippet(ctx, data.if_cond.span, "..");

let continue_code = snippet_block(ctx, data.if_block.span, "..", Some(data.if_expr.span));
// region B

let else_code = snippet_block(ctx, data.else_expr.span, "..", Some(data.if_expr.span));

let indent_if = indent_of(ctx, data.if_expr.span).unwrap_or(0);
format!(
"{}if {} {} {}",
" ".repeat(indent_if),
"{indent}if {} {}\n{indent}{}",
cond_code,
continue_code,
else_code,
indent = " ".repeat(indent_if),
)
}

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

/// Eats at `s` from the end till a closing brace `}` is encountered, and then
/// continues eating till a non-whitespace character is found.
/// e.g., the string
/// Eats at `s` from the end till a closing brace `}` is encountered, and then continues eating
/// till a non-whitespace character is found. e.g., the string. If no closing `}` is present, the
/// string will be preserved.
///
/// ```rust
/// {
Expand All @@ -405,20 +405,21 @@ fn check_and_warn<'a>(ctx: &EarlyContext<'_>, expr: &'a ast::Expr) {
/// {
/// let x = 5;
/// ```
///
/// NOTE: when there is no closing brace in `s`, `s` is _not_ preserved, i.e.,
/// an empty string will be returned in that case.
#[must_use]
fn erode_from_back(s: &str) -> String {
let mut ret = String::from(s);
let mut ret = s.to_string();
while ret.pop().map_or(false, |c| c != '}') {}
while let Some(c) = ret.pop() {
if !c.is_whitespace() {
ret.push(c);
break;
}
}
ret
if ret.is_empty() {
s.to_string()
} else {
ret
}
}

fn span_of_first_expr_in_block(block: &ast::Block) -> Option<Span> {
Expand All @@ -428,6 +429,7 @@ fn span_of_first_expr_in_block(block: &ast::Block) -> Option<Span> {
#[cfg(test)]
mod test {
use super::erode_from_back;

#[test]
#[rustfmt::skip]
fn test_erode_from_back() {
Expand All @@ -453,7 +455,7 @@ mod test {
let x = 5;
let y = something();
";
let expected = "";
let expected = input;
let got = erode_from_back(input);
assert_eq!(expected, got);
}
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/needless_continue.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ LL | | }
= help: consider dropping the `else` clause
if (zero!(i % 2) || nonzero!(i % 5)) && i % 3 != 0 {
continue;
} {
}
{
println!("Blabber");
println!("Jabber");
}
Expand Down Expand Up @@ -89,7 +90,8 @@ LL | | }
= help: consider dropping the `else` clause
if condition() {
continue; // should lint here
} {
}
{
println!("bar-5");
}

Expand Down

0 comments on commit 4068ff4

Please sign in to comment.