2016-08-21 21:18:58 +00:00
|
|
|
//! Checks for continue statements in loops that are redundant.
|
|
|
|
//!
|
|
|
|
//! For example, the lint would catch
|
|
|
|
//!
|
2019-03-09 07:51:23 +00:00
|
|
|
//! ```rust
|
|
|
|
//! let mut a = 1;
|
|
|
|
//! let x = true;
|
|
|
|
//!
|
|
|
|
//! while a < 5 {
|
|
|
|
//! a = 6;
|
2016-08-21 21:18:58 +00:00
|
|
|
//! if x {
|
|
|
|
//! // ...
|
|
|
|
//! } else {
|
|
|
|
//! continue;
|
|
|
|
//! }
|
|
|
|
//! println!("Hello, world");
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! And suggest something like this:
|
|
|
|
//!
|
2019-03-09 07:51:23 +00:00
|
|
|
//! ```rust
|
|
|
|
//! let mut a = 1;
|
|
|
|
//! let x = true;
|
|
|
|
//!
|
|
|
|
//! while a < 5 {
|
|
|
|
//! a = 6;
|
2016-08-21 21:18:58 +00:00
|
|
|
//! if x {
|
|
|
|
//! // ...
|
|
|
|
//! println!("Hello, world");
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! This lint is **warn** by default.
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::{original_sp, DUMMY_SP};
|
2020-02-04 15:10:59 +00:00
|
|
|
use rustc_span::Span;
|
2016-08-21 21:18:58 +00:00
|
|
|
|
2020-02-04 15:10:59 +00:00
|
|
|
use crate::utils::{indent_of, snippet, snippet_block, span_lint_and_help};
|
2016-08-21 21:18:58 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** The lint checks for `if`-statements appearing in loops
|
|
|
|
/// that contain a `continue` statement in either their main blocks or their
|
|
|
|
/// `else`-blocks, when omitting the `else`-block possibly with some
|
|
|
|
/// rearrangement of code can make the code easier to understand.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Having explicit `else` blocks for `if` statements
|
|
|
|
/// containing `continue` in their THEN branch adds unnecessary branching and
|
|
|
|
/// nesting to the code. Having an else block containing just `continue` can
|
|
|
|
/// also be better written by grouping the statements following the whole `if`
|
|
|
|
/// statement within the THEN block and omitting the else block completely.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-08-03 16:42:05 +00:00
|
|
|
/// # fn condition() -> bool { false }
|
|
|
|
/// # fn update_condition() {}
|
|
|
|
/// # let x = false;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// while condition() {
|
|
|
|
/// update_condition();
|
|
|
|
/// if x {
|
|
|
|
/// // ...
|
|
|
|
/// } else {
|
|
|
|
/// continue;
|
|
|
|
/// }
|
|
|
|
/// println!("Hello, world");
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be rewritten as
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-08-03 16:42:05 +00:00
|
|
|
/// # fn condition() -> bool { false }
|
|
|
|
/// # fn update_condition() {}
|
|
|
|
/// # let x = false;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// while condition() {
|
|
|
|
/// update_condition();
|
|
|
|
/// if x {
|
|
|
|
/// // ...
|
|
|
|
/// println!("Hello, world");
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// As another example, the following code
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-08-03 16:42:05 +00:00
|
|
|
/// # fn waiting() -> bool { false }
|
2019-03-05 16:50:33 +00:00
|
|
|
/// loop {
|
|
|
|
/// if waiting() {
|
|
|
|
/// continue;
|
|
|
|
/// } else {
|
|
|
|
/// // Do something useful
|
|
|
|
/// }
|
2019-08-03 16:42:05 +00:00
|
|
|
/// # break;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Could be rewritten as
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-08-03 16:42:05 +00:00
|
|
|
/// # fn waiting() -> bool { false }
|
2019-03-05 16:50:33 +00:00
|
|
|
/// loop {
|
|
|
|
/// if waiting() {
|
|
|
|
/// continue;
|
|
|
|
/// }
|
|
|
|
/// // Do something useful
|
2019-08-03 16:42:05 +00:00
|
|
|
/// # break;
|
2019-03-05 16:50:33 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2016-08-21 21:18:58 +00:00
|
|
|
pub NEEDLESS_CONTINUE,
|
2018-03-28 13:24:26 +00:00
|
|
|
pedantic,
|
2016-08-21 21:18:58 +00:00
|
|
|
"`continue` statements that can be replaced by a rearrangement of code"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(NeedlessContinue => [NEEDLESS_CONTINUE]);
|
2016-08-21 21:18:58 +00:00
|
|
|
|
|
|
|
impl EarlyLintPass for NeedlessContinue {
|
2020-02-05 10:09:48 +00:00
|
|
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
|
2019-08-19 16:30:32 +00:00
|
|
|
if !expr.span.from_expansion() {
|
2020-02-05 10:09:48 +00:00
|
|
|
check_and_warn(cx, expr);
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
/* This lint has to mainly deal with two cases of needless continue
|
|
|
|
* statements. */
|
|
|
|
// Case 1 [Continue inside else block]:
|
|
|
|
//
|
|
|
|
// loop {
|
|
|
|
// // region A
|
|
|
|
// if cond {
|
|
|
|
// // region B
|
|
|
|
// } else {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
// // region C
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// This code can better be written as follows:
|
|
|
|
//
|
|
|
|
// loop {
|
|
|
|
// // region A
|
|
|
|
// if cond {
|
|
|
|
// // region B
|
|
|
|
// // region C
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Case 2 [Continue inside then block]:
|
|
|
|
//
|
|
|
|
// loop {
|
|
|
|
// // region A
|
|
|
|
// if cond {
|
|
|
|
// continue;
|
|
|
|
// // potentially more code here.
|
|
|
|
// } else {
|
|
|
|
// // region B
|
|
|
|
// }
|
|
|
|
// // region C
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// This snippet can be refactored to:
|
|
|
|
//
|
|
|
|
// loop {
|
|
|
|
// // region A
|
|
|
|
// if !cond {
|
|
|
|
// // region B
|
|
|
|
// // region C
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
2016-12-31 21:35:39 +00:00
|
|
|
|
|
|
|
/// Given an expression, returns true if either of the following is true
|
2016-08-21 21:18:58 +00:00
|
|
|
///
|
2016-12-31 21:35:39 +00:00
|
|
|
/// - The expression is a `continue` node.
|
2017-08-09 07:30:56 +00:00
|
|
|
/// - The expression node is a block with the first statement being a
|
|
|
|
/// `continue`.
|
2019-03-11 18:40:30 +00:00
|
|
|
fn needless_continue_in_else(else_expr: &ast::Expr, label: Option<&ast::Label>) -> bool {
|
2019-09-27 15:16:06 +00:00
|
|
|
match else_expr.kind {
|
2019-03-11 18:40:30 +00:00
|
|
|
ast::ExprKind::Block(ref else_block, _) => is_first_block_stmt_continue(else_block, label),
|
|
|
|
ast::ExprKind::Continue(l) => compare_labels(label, l.as_ref()),
|
2016-12-31 21:35:39 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 18:40:30 +00:00
|
|
|
fn is_first_block_stmt_continue(block: &ast::Block, label: Option<&ast::Label>) -> bool {
|
2019-09-27 15:16:06 +00:00
|
|
|
block.stmts.get(0).map_or(false, |stmt| match stmt.kind {
|
2018-11-27 20:14:15 +00:00
|
|
|
ast::StmtKind::Semi(ref e) | ast::StmtKind::Expr(ref e) => {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ast::ExprKind::Continue(ref l) = e.kind {
|
2019-03-11 18:40:30 +00:00
|
|
|
compare_labels(label, l.as_ref())
|
2018-11-27 20:14:15 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2016-12-31 21:35:39 +00:00
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
})
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 18:40:30 +00:00
|
|
|
/// If the `continue` has a label, check it matches the label of the loop.
|
|
|
|
fn compare_labels(loop_label: Option<&ast::Label>, continue_label: Option<&ast::Label>) -> bool {
|
|
|
|
match (loop_label, continue_label) {
|
|
|
|
// `loop { continue; }` or `'a loop { continue; }`
|
|
|
|
(_, None) => true,
|
|
|
|
// `loop { continue 'a; }`
|
|
|
|
(None, _) => false,
|
|
|
|
// `'a loop { continue 'a; }` or `'a loop { continue 'b; }`
|
|
|
|
(Some(x), Some(y)) => x.ident == y.ident,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 21:18:58 +00:00
|
|
|
/// If `expr` is a loop expression (while/while let/for/loop), calls `func` with
|
|
|
|
/// the AST object representing the loop block of `expr`.
|
2017-05-03 12:13:50 +00:00
|
|
|
fn with_loop_block<F>(expr: &ast::Expr, mut func: F)
|
2017-08-09 07:30:56 +00:00
|
|
|
where
|
2019-03-11 18:40:30 +00:00
|
|
|
F: FnMut(&ast::Block, Option<&ast::Label>),
|
2017-05-03 12:13:50 +00:00
|
|
|
{
|
2019-05-21 05:43:43 +00:00
|
|
|
if let ast::ExprKind::While(_, loop_block, label)
|
2019-06-24 02:54:23 +00:00
|
|
|
| ast::ExprKind::ForLoop(_, _, loop_block, label)
|
2021-01-21 01:15:08 +00:00
|
|
|
| ast::ExprKind::Loop(loop_block, label, _) = &expr.kind
|
2019-05-21 05:43:43 +00:00
|
|
|
{
|
|
|
|
func(loop_block, label.as_ref());
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
/// If `stmt` is an if expression node with an `else` branch, calls func with
|
|
|
|
/// the
|
2016-08-21 21:18:58 +00:00
|
|
|
/// following:
|
|
|
|
///
|
2016-12-31 21:35:39 +00:00
|
|
|
/// - The `if` expression itself,
|
|
|
|
/// - The `if` condition expression,
|
|
|
|
/// - The `then` block, and
|
|
|
|
/// - The `else` expression.
|
2016-08-21 21:18:58 +00:00
|
|
|
fn with_if_expr<F>(stmt: &ast::Stmt, mut func: F)
|
2017-08-09 07:30:56 +00:00
|
|
|
where
|
|
|
|
F: FnMut(&ast::Expr, &ast::Expr, &ast::Block, &ast::Expr),
|
2017-05-03 12:13:50 +00:00
|
|
|
{
|
2019-09-27 15:16:06 +00:00
|
|
|
match stmt.kind {
|
2017-09-05 09:33:04 +00:00
|
|
|
ast::StmtKind::Semi(ref e) | ast::StmtKind::Expr(ref e) => {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ast::ExprKind::If(ref cond, ref if_block, Some(ref else_expr)) = e.kind {
|
2016-08-21 21:18:58 +00:00
|
|
|
func(e, cond, if_block, else_expr);
|
|
|
|
}
|
|
|
|
},
|
2017-05-03 12:13:50 +00:00
|
|
|
_ => {},
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A type to distinguish between the two distinct cases this lint handles.
|
2017-04-12 08:55:34 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2016-08-21 21:18:58 +00:00
|
|
|
enum LintType {
|
|
|
|
ContinueInsideElseBlock,
|
|
|
|
ContinueInsideThenBlock,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Data we pass around for construction of help messages.
|
|
|
|
struct LintData<'a> {
|
2016-12-31 21:35:39 +00:00
|
|
|
/// The `if` expression encountered in the above loop.
|
|
|
|
if_expr: &'a ast::Expr,
|
|
|
|
/// The condition expression for the above `if`.
|
|
|
|
if_cond: &'a ast::Expr,
|
|
|
|
/// The `then` block of the `if` statement.
|
|
|
|
if_block: &'a ast::Block,
|
|
|
|
/// The `else` block of the `if` statement.
|
|
|
|
/// Note that we only work with `if` exprs that have an `else` branch.
|
|
|
|
else_expr: &'a ast::Expr,
|
|
|
|
/// The 0-based index of the `if` statement in the containing loop block.
|
|
|
|
stmt_idx: usize,
|
|
|
|
/// The statements of the loop block.
|
|
|
|
block_stmts: &'a [ast::Stmt],
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 15:10:59 +00:00
|
|
|
const MSG_REDUNDANT_ELSE_BLOCK: &str = "this `else` block is redundant";
|
2016-08-21 21:18:58 +00:00
|
|
|
|
2020-02-04 15:10:59 +00:00
|
|
|
const MSG_ELSE_BLOCK_NOT_NEEDED: &str = "there is no need for an explicit `else` block for this `if` \
|
|
|
|
expression";
|
2016-08-21 21:18:58 +00:00
|
|
|
|
2020-02-04 15:10:59 +00:00
|
|
|
const DROP_ELSE_BLOCK_AND_MERGE_MSG: &str = "consider dropping the `else` clause and merging the code that \
|
|
|
|
follows (in the loop) with the `if` block";
|
2016-08-21 21:18:58 +00:00
|
|
|
|
2020-02-04 15:10:59 +00:00
|
|
|
const DROP_ELSE_BLOCK_MSG: &str = "consider dropping the `else` clause";
|
2016-08-21 21:18:58 +00:00
|
|
|
|
2020-02-05 10:09:48 +00:00
|
|
|
fn emit_warning<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>, header: &str, typ: LintType) {
|
2016-08-21 21:18:58 +00:00
|
|
|
// snip is the whole *help* message that appears after the warning.
|
|
|
|
// message is the warning message.
|
|
|
|
// expr is the expression which the lint warning message refers to.
|
|
|
|
let (snip, message, expr) = match typ {
|
2017-09-05 09:33:04 +00:00
|
|
|
LintType::ContinueInsideElseBlock => (
|
2020-02-05 10:09:48 +00:00
|
|
|
suggestion_snippet_for_continue_inside_else(cx, data),
|
2017-09-05 09:33:04 +00:00
|
|
|
MSG_REDUNDANT_ELSE_BLOCK,
|
|
|
|
data.else_expr,
|
|
|
|
),
|
|
|
|
LintType::ContinueInsideThenBlock => (
|
2020-02-05 10:09:48 +00:00
|
|
|
suggestion_snippet_for_continue_inside_if(cx, data),
|
2017-09-05 09:33:04 +00:00
|
|
|
MSG_ELSE_BLOCK_NOT_NEEDED,
|
|
|
|
data.if_expr,
|
|
|
|
),
|
2016-08-21 21:18:58 +00:00
|
|
|
};
|
2020-02-04 15:10:59 +00:00
|
|
|
span_lint_and_help(
|
2020-02-05 10:09:48 +00:00
|
|
|
cx,
|
2020-02-04 15:10:59 +00:00
|
|
|
NEEDLESS_CONTINUE,
|
|
|
|
expr.span,
|
|
|
|
message,
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2020-02-04 15:10:59 +00:00
|
|
|
&format!("{}\n{}", header, snip),
|
|
|
|
);
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 10:09:48 +00:00
|
|
|
fn suggestion_snippet_for_continue_inside_if<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>) -> String {
|
|
|
|
let cond_code = snippet(cx, data.if_cond.span, "..");
|
2016-08-21 21:18:58 +00:00
|
|
|
|
2020-02-05 10:09:48 +00:00
|
|
|
let continue_code = snippet_block(cx, data.if_block.span, "..", Some(data.if_expr.span));
|
2020-02-05 09:42:33 +00:00
|
|
|
|
2020-02-05 10:09:48 +00:00
|
|
|
let else_code = snippet_block(cx, data.else_expr.span, "..", Some(data.if_expr.span));
|
2016-08-21 21:18:58 +00:00
|
|
|
|
2020-02-05 10:09:48 +00:00
|
|
|
let indent_if = indent_of(cx, data.if_expr.span).unwrap_or(0);
|
2020-02-04 15:10:59 +00:00
|
|
|
format!(
|
2020-02-05 09:42:33 +00:00
|
|
|
"{indent}if {} {}\n{indent}{}",
|
2020-02-04 15:10:59 +00:00
|
|
|
cond_code,
|
|
|
|
continue_code,
|
|
|
|
else_code,
|
2020-02-05 09:42:33 +00:00
|
|
|
indent = " ".repeat(indent_if),
|
2020-02-04 15:10:59 +00:00
|
|
|
)
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 10:09:48 +00:00
|
|
|
fn suggestion_snippet_for_continue_inside_else<'a>(cx: &EarlyContext<'_>, data: &'a LintData<'_>) -> String {
|
|
|
|
let cond_code = snippet(cx, data.if_cond.span, "..");
|
2016-08-21 21:18:58 +00:00
|
|
|
|
|
|
|
// Region B
|
2020-02-05 10:09:48 +00:00
|
|
|
let block_code = erode_from_back(&snippet_block(cx, data.if_block.span, "..", Some(data.if_expr.span)));
|
2016-08-21 21:18:58 +00:00
|
|
|
|
|
|
|
// Region C
|
|
|
|
// These is the code in the loop block that follows the if/else construction
|
|
|
|
// we are complaining about. We want to pull all of this code into the
|
|
|
|
// `then` block of the `if` statement.
|
2020-02-04 15:10:59 +00:00
|
|
|
let indent = span_of_first_expr_in_block(data.if_block)
|
2020-02-05 10:09:48 +00:00
|
|
|
.and_then(|span| indent_of(cx, span))
|
2020-02-04 15:10:59 +00:00
|
|
|
.unwrap_or(0);
|
2017-05-03 12:13:50 +00:00
|
|
|
let to_annex = data.block_stmts[data.stmt_idx + 1..]
|
|
|
|
.iter()
|
|
|
|
.map(|stmt| original_sp(stmt.span, DUMMY_SP))
|
2020-02-04 15:10:59 +00:00
|
|
|
.map(|span| {
|
2020-02-05 10:09:48 +00:00
|
|
|
let snip = snippet_block(cx, span, "..", None).into_owned();
|
2020-02-04 15:10:59 +00:00
|
|
|
snip.lines()
|
|
|
|
.map(|line| format!("{}{}", " ".repeat(indent), line))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n")
|
|
|
|
})
|
2017-05-03 12:13:50 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n");
|
2016-08-21 21:18:58 +00:00
|
|
|
|
2020-02-05 10:09:48 +00:00
|
|
|
let indent_if = indent_of(cx, data.if_expr.span).unwrap_or(0);
|
2020-02-04 15:10:59 +00:00
|
|
|
format!(
|
|
|
|
"{indent_if}if {} {}\n{indent}// merged code follows:\n{}\n{indent_if}}}",
|
|
|
|
cond_code,
|
|
|
|
block_code,
|
|
|
|
to_annex,
|
|
|
|
indent = " ".repeat(indent),
|
|
|
|
indent_if = " ".repeat(indent_if),
|
|
|
|
)
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 10:09:48 +00:00
|
|
|
fn check_and_warn<'a>(cx: &EarlyContext<'_>, expr: &'a ast::Expr) {
|
2019-03-11 18:40:30 +00:00
|
|
|
with_loop_block(expr, |loop_block, label| {
|
2017-11-04 19:55:56 +00:00
|
|
|
for (i, stmt) in loop_block.stmts.iter().enumerate() {
|
|
|
|
with_if_expr(stmt, |if_expr, cond, then_block, else_expr| {
|
|
|
|
let data = &LintData {
|
|
|
|
stmt_idx: i,
|
2018-03-15 15:07:15 +00:00
|
|
|
if_expr,
|
2017-11-04 19:55:56 +00:00
|
|
|
if_cond: cond,
|
|
|
|
if_block: then_block,
|
2018-03-15 15:07:15 +00:00
|
|
|
else_expr,
|
2017-11-04 19:55:56 +00:00
|
|
|
block_stmts: &loop_block.stmts,
|
|
|
|
};
|
2019-03-11 18:40:30 +00:00
|
|
|
if needless_continue_in_else(else_expr, label) {
|
2018-11-27 20:14:15 +00:00
|
|
|
emit_warning(
|
2020-02-05 10:09:48 +00:00
|
|
|
cx,
|
2018-11-27 20:14:15 +00:00
|
|
|
data,
|
|
|
|
DROP_ELSE_BLOCK_AND_MERGE_MSG,
|
|
|
|
LintType::ContinueInsideElseBlock,
|
|
|
|
);
|
2019-03-11 18:40:30 +00:00
|
|
|
} else if is_first_block_stmt_continue(then_block, label) {
|
2020-02-05 10:09:48 +00:00
|
|
|
emit_warning(cx, data, DROP_ELSE_BLOCK_MSG, LintType::ContinueInsideThenBlock);
|
2017-11-04 19:55:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-08-21 21:18:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-05 09:42:33 +00:00
|
|
|
/// 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.
|
2016-08-21 21:18:58 +00:00
|
|
|
///
|
2019-03-09 07:51:23 +00:00
|
|
|
/// ```rust
|
2018-11-27 20:14:15 +00:00
|
|
|
/// {
|
|
|
|
/// let x = 5;
|
|
|
|
/// }
|
2017-04-12 08:55:34 +00:00
|
|
|
/// ```
|
2016-08-21 21:18:58 +00:00
|
|
|
///
|
|
|
|
/// is transformed to
|
|
|
|
///
|
2018-06-25 19:22:53 +00:00
|
|
|
/// ```ignore
|
2017-04-09 12:07:11 +00:00
|
|
|
/// {
|
2017-05-01 14:57:27 +00:00
|
|
|
/// let x = 5;
|
2017-04-12 08:55:34 +00:00
|
|
|
/// ```
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2020-02-04 15:10:59 +00:00
|
|
|
fn erode_from_back(s: &str) -> String {
|
2020-02-05 09:42:33 +00:00
|
|
|
let mut ret = s.to_string();
|
2017-05-03 12:13:50 +00:00
|
|
|
while ret.pop().map_or(false, |c| c != '}') {}
|
2016-08-21 21:18:58 +00:00
|
|
|
while let Some(c) = ret.pop() {
|
|
|
|
if !c.is_whitespace() {
|
|
|
|
ret.push(c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-05 09:42:33 +00:00
|
|
|
if ret.is_empty() {
|
|
|
|
s.to_string()
|
|
|
|
} else {
|
|
|
|
ret
|
|
|
|
}
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 15:10:59 +00:00
|
|
|
fn span_of_first_expr_in_block(block: &ast::Block) -> Option<Span> {
|
2020-06-09 14:36:01 +00:00
|
|
|
block.stmts.get(0).map(|stmt| stmt.span)
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 15:10:59 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::erode_from_back;
|
2020-02-05 09:42:33 +00:00
|
|
|
|
2020-02-04 15:10:59 +00:00
|
|
|
#[test]
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn test_erode_from_back() {
|
|
|
|
let input = "\
|
|
|
|
{
|
|
|
|
let x = 5;
|
|
|
|
let y = format!(\"{}\", 42);
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected = "\
|
|
|
|
{
|
|
|
|
let x = 5;
|
|
|
|
let y = format!(\"{}\", 42);";
|
|
|
|
|
|
|
|
let got = erode_from_back(input);
|
|
|
|
assert_eq!(expected, got);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn test_erode_from_back_no_brace() {
|
|
|
|
let input = "\
|
|
|
|
let x = 5;
|
|
|
|
let y = something();
|
|
|
|
";
|
2020-02-05 09:42:33 +00:00
|
|
|
let expected = input;
|
2020-02-04 15:10:59 +00:00
|
|
|
let got = erode_from_back(input);
|
|
|
|
assert_eq!(expected, got);
|
|
|
|
}
|
2016-08-21 21:18:58 +00:00
|
|
|
}
|