[needless_continue] Add comments explaining terminology used thoughout in the code.

This commit is contained in:
Yati Sagade 2016-12-31 22:35:39 +01:00
parent 9396120008
commit 38238f576d

View File

@ -33,7 +33,6 @@
use syntax::codemap::{original_sp,DUMMY_SP}; use syntax::codemap::{original_sp,DUMMY_SP};
use utils::{in_macro, span_help_and_lint, snippet_block, snippet}; use utils::{in_macro, span_help_and_lint, snippet_block, snippet};
use self::LintType::*;
/// **What it does:** The lint checks for `if`-statements appearing in loops /// **What it does:** The lint checks for `if`-statements appearing in loops
/// that contain a `continue` statement in either their main blocks or their /// that contain a `continue` statement in either their main blocks or their
@ -117,34 +116,78 @@ fn check_expr(&mut self, ctx: &EarlyContext, expr: &ast::Expr) {
} }
} }
/// Given an Expr, returns true if either of the following is true /* 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
* }
* }
*/
/// Given an expression, returns true if either of the following is true
/// ///
/// - The Expr is a `continue` node. /// - The expression is a `continue` node.
/// - The Expr node is a block with the first statement being a `continue`. /// - The expression node is a block with the first statement being a `continue`.
/// ///
fn needless_continue_in_else(else_expr: &ast::Expr) -> bool { fn needless_continue_in_else(else_expr: &ast::Expr) -> bool {
let mut found = false;
match else_expr.node { match else_expr.node {
ast::ExprKind::Block(ref else_block) => { ast::ExprKind::Block(ref else_block) => is_first_block_stmt_continue(else_block),
found = is_first_block_stmt_continue(else_block); ast::ExprKind::Continue(_) => true,
}, _ => false,
ast::ExprKind::Continue(_) => { found = true }, }
_ => { },
};
found
} }
fn is_first_block_stmt_continue(block: &ast::Block) -> bool { fn is_first_block_stmt_continue(block: &ast::Block) -> bool {
let mut ret = false; block.stmts.get(0).map_or(false, |stmt| match stmt.node {
block.stmts.get(0).map(|stmt| { ast::StmtKind::Semi(ref e) |
if_let_chain! {[ ast::StmtKind::Expr(ref e) => if let ast::ExprKind::Continue(_) = e.node {
let ast::StmtKind::Semi(ref e) = stmt.node, true
let ast::ExprKind::Continue(_) = e.node, } else {
], { false
ret = true; },
}} _ => false,
}); })
ret
} }
/// If `expr` is a loop expression (while/while let/for/loop), calls `func` with /// If `expr` is a loop expression (while/while let/for/loop), calls `func` with
@ -159,13 +202,13 @@ fn with_loop_block<F>(expr: &ast::Expr, mut func: F) where F: FnMut(&ast::Block)
} }
} }
/// If `stmt` is an if expression node with an else branch, calls func with the /// If `stmt` is an if expression node with an `else` branch, calls func with the
/// following: /// following:
/// ///
/// - The if Expr, /// - The `if` expression itself,
/// - The if condition Expr, /// - The `if` condition expression,
/// - The then block of this if Expr, and /// - The `then` block, and
/// - The else expr. /// - The `else` expression.
/// ///
fn with_if_expr<F>(stmt: &ast::Stmt, mut func: F) fn with_if_expr<F>(stmt: &ast::Stmt, mut func: F)
where F: FnMut(&ast::Expr, &ast::Expr, &ast::Block, &ast::Expr) { where F: FnMut(&ast::Expr, &ast::Expr, &ast::Block, &ast::Expr) {
@ -188,15 +231,19 @@ enum LintType {
/// Data we pass around for construction of help messages. /// Data we pass around for construction of help messages.
struct LintData<'a> { struct LintData<'a> {
if_expr: &'a ast::Expr, // The `if` expr encountered in the above loop. /// The `if` expression encountered in the above loop.
if_cond: &'a ast::Expr, // The condition expression for the above `if`. if_expr: &'a ast::Expr,
if_block: &'a ast::Block, // The `then` block of the `if` statement. /// The condition expression for the above `if`.
else_expr: &'a ast::Expr, /* The `else` block of the `if` statement. if_cond: &'a ast::Expr,
Note that we only work with `if` exprs that /// The `then` block of the `if` statement.
have an `else` branch. */ if_block: &'a ast::Block,
stmt_idx: usize, /* The 0-based index of the `if` statement in /// The `else` block of the `if` statement.
the containing loop block. */ /// Note that we only work with `if` exprs that have an `else` branch.
block_stmts: &'a [ast::Stmt], // The statements of the loop block. 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],
} }
const MSG_REDUNDANT_ELSE_BLOCK: &'static str = "This else block is redundant.\n"; const MSG_REDUNDANT_ELSE_BLOCK: &'static str = "This else block is redundant.\n";
@ -219,12 +266,12 @@ fn emit_warning<'a>(ctx: &EarlyContext,
// message is the warning message. // message is the warning message.
// expr is the expression which the lint warning message refers to. // expr is the expression which the lint warning message refers to.
let (snip, message, expr) = match typ { let (snip, message, expr) = match typ {
ContinueInsideElseBlock => { LintType::ContinueInsideElseBlock => {
(suggestion_snippet_for_continue_inside_else(ctx, data, header), (suggestion_snippet_for_continue_inside_else(ctx, data, header),
MSG_REDUNDANT_ELSE_BLOCK, MSG_REDUNDANT_ELSE_BLOCK,
data.else_expr) data.else_expr)
}, },
ContinueInsideThenBlock => { LintType::ContinueInsideThenBlock => {
(suggestion_snippet_for_continue_inside_if(ctx, data, header), (suggestion_snippet_for_continue_inside_if(ctx, data, header),
MSG_ELSE_BLOCK_NOT_NEEDED, MSG_ELSE_BLOCK_NOT_NEEDED,
data.if_expr) data.if_expr)
@ -236,7 +283,7 @@ fn emit_warning<'a>(ctx: &EarlyContext,
fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext, fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext,
data: &'a LintData, data: &'a LintData,
header: &str) -> String { header: &str) -> String {
let cond_code = &snippet(ctx, data.if_cond.span, "..").into_owned(); let cond_code = snippet(ctx, data.if_cond.span, "..");
let if_code = format!("if {} {{\n continue;\n}}\n", cond_code); let if_code = format!("if {} {{\n continue;\n}}\n", cond_code);
/* ^^^^--- Four spaces of indentation. */ /* ^^^^--- Four spaces of indentation. */
@ -256,14 +303,14 @@ fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext,
data: &'a LintData, data: &'a LintData,
header: &str) -> String header: &str) -> String
{ {
let cond_code = &snippet(ctx, data.if_cond.span, "..").into_owned(); let cond_code = snippet(ctx, data.if_cond.span, "..");
let mut if_code = format!("if {} {{\n", cond_code); let mut if_code = format!("if {} {{\n", cond_code);
// Region B // Region B
let block_code = &snippet(ctx, data.if_block.span, "..").into_owned(); let block_code = &snippet(ctx, data.if_block.span, "..").into_owned();
let block_code = erode_block(block_code); let block_code = erode_block(block_code);
let block_code = trim_indent(&block_code, false); let block_code = trim_indent(&block_code, false);
let block_code = left_pad_lines_with_spaces(&block_code, 4usize); let block_code = left_pad_lines_with_spaces(&block_code, 4_usize);
if_code.push_str(&block_code); if_code.push_str(&block_code);
@ -300,9 +347,9 @@ fn check_and_warn<'a>(ctx: &EarlyContext, expr: &'a ast::Expr) {
block_stmts: &loop_block.stmts, block_stmts: &loop_block.stmts,
}; };
if needless_continue_in_else(else_expr) { if needless_continue_in_else(else_expr) {
emit_warning(ctx, data, DROP_ELSE_BLOCK_AND_MERGE_MSG, ContinueInsideElseBlock); emit_warning(ctx, data, DROP_ELSE_BLOCK_AND_MERGE_MSG, LintType::ContinueInsideElseBlock);
} else if is_first_block_stmt_continue(then_block) { } else if is_first_block_stmt_continue(then_block) {
emit_warning(ctx, data, DROP_ELSE_BLOCK_MSG, ContinueInsideThenBlock); emit_warning(ctx, data, DROP_ELSE_BLOCK_MSG, LintType::ContinueInsideThenBlock);
} }
}); });
} }
@ -361,7 +408,7 @@ fn indent_level(s: &str) -> usize {
s.chars() s.chars()
.enumerate() .enumerate()
.find(|&(_, c)| !c.is_whitespace()) .find(|&(_, c)| !c.is_whitespace())
.map_or(0usize, |(i, _)| i) .map_or(0_usize, |(i, _)| i)
} }
/// Trims indentation from a snippet such that the line with the minimum /// Trims indentation from a snippet such that the line with the minimum
@ -372,7 +419,7 @@ fn trim_indent(s: &str, skip_first_line: bool) -> String {
.skip(skip_first_line as usize) .skip(skip_first_line as usize)
.map(indent_level) .map(indent_level)
.min() .min()
.unwrap_or(0usize); .unwrap_or(0_usize);
let ret = s.lines().map(|line| { let ret = s.lines().map(|line| {
if is_null(line) { if is_null(line) {
String::from(line) String::from(line)
@ -421,14 +468,14 @@ fn align_two_snippets(s: &str, t: &str) -> String {
.rev() .rev()
.skip_while(|line| line.is_empty() || is_all_whitespace(line)) .skip_while(|line| line.is_empty() || is_all_whitespace(line))
.next() .next()
.map_or(0usize, indent_level); .map_or(0_usize, indent_level);
// We want to align the first nonempty, non-all-whitespace line of t to // We want to align the first nonempty, non-all-whitespace line of t to
// have the same indent level as target_ilevel // have the same indent level as target_ilevel
let level = t.lines() let level = t.lines()
.skip_while(|line| line.is_empty() || is_all_whitespace(line)) .skip_while(|line| line.is_empty() || is_all_whitespace(line))
.next() .next()
.map_or(0usize, indent_level); .map_or(0_usize, indent_level);
let add_or_not_remove = target_ilevel > level; /* when true, we add spaces, let add_or_not_remove = target_ilevel > level; /* when true, we add spaces,
otherwise eat. */ otherwise eat. */
@ -440,11 +487,14 @@ fn align_two_snippets(s: &str, t: &str) -> String {
}; };
let new_t = t.lines() let new_t = t.lines()
.filter(|line| !is_null(line)) .filter_map(|line| {
.map(|line| if add_or_not_remove { if is_null(line) {
left_pad_with_spaces(line, delta) None
} else { } else if add_or_not_remove {
remove_whitespace_from_left(line, delta) Some(left_pad_with_spaces(line, delta))
} else {
Some(remove_whitespace_from_left(line, delta))
}
}) })
.collect::<Vec<_>>().join("\n"); .collect::<Vec<_>>().join("\n");
@ -452,16 +502,14 @@ fn align_two_snippets(s: &str, t: &str) -> String {
} }
fn align_snippets(xs: &[&str]) -> String { fn align_snippets(xs: &[&str]) -> String {
match xs.len() { if xs.is_empty() {
0 => String::from(""), String::from("")
_ => { } else {
let mut ret = String::new(); let mut ret = xs[0].to_string();
ret.push_str(xs[0]); for x in xs.iter().skip(1_usize) {
for x in xs.iter().skip(1usize) { ret = align_two_snippets(&ret, x);
ret = align_two_snippets(&ret, x);
}
ret
} }
ret
} }
} }