From 4068ff4d8a97cfdd21c717748e75a4839923e231 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Wed, 5 Feb 2020 10:42:33 +0100 Subject: [PATCH] Improve help message in needless_continue --- clippy_lints/src/needless_continue.rs | 26 ++++++++++++++------------ tests/ui/needless_continue.stderr | 6 ++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/needless_continue.rs b/clippy_lints/src/needless_continue.rs index 75cbfe13072..45e5fdd5ce8 100644 --- a/clippy_lints/src/needless_continue.rs +++ b/clippy_lints/src/needless_continue.rs @@ -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), ) } @@ -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 /// { @@ -405,12 +405,9 @@ 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() { @@ -418,7 +415,11 @@ fn erode_from_back(s: &str) -> String { break; } } - ret + if ret.is_empty() { + s.to_string() + } else { + ret + } } fn span_of_first_expr_in_block(block: &ast::Block) -> Option { @@ -428,6 +429,7 @@ fn span_of_first_expr_in_block(block: &ast::Block) -> Option { #[cfg(test)] mod test { use super::erode_from_back; + #[test] #[rustfmt::skip] fn test_erode_from_back() { @@ -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); } diff --git a/tests/ui/needless_continue.stderr b/tests/ui/needless_continue.stderr index 6c4aa744520..8d6a37df960 100644 --- a/tests/ui/needless_continue.stderr +++ b/tests/ui/needless_continue.stderr @@ -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"); } @@ -89,7 +90,8 @@ LL | | } = help: consider dropping the `else` clause if condition() { continue; // should lint here - } { + } + { println!("bar-5"); }