1
Fork 0

Run rustfmt

This commit is contained in:
Oliver Schneider 2017-05-03 14:13:50 +02:00
parent 5295fe4792
commit 1522a4913f
6 changed files with 85 additions and 103 deletions

View file

@ -299,7 +299,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
tcx: self.tcx, tcx: self.tcx,
tables: self.tcx.typeck_tables_of(def_id), tables: self.tcx.typeck_tables_of(def_id),
needed_resolution: false, needed_resolution: false,
substs, substs: substs,
}; };
let body = if let Some(id) = self.tcx.hir.as_local_node_id(def_id) { let body = if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
self.tcx.mir_const_qualif(def_id); self.tcx.mir_const_qualif(def_id);

View file

@ -54,9 +54,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
if let ExprBinary(ref op, ref left, ref right) = e.node { if let ExprBinary(ref op, ref left, ref right) = e.node {
if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) { if is_valid_operator(op) && SpanlessEq::new(cx).ignore_fn().eq_expr(left, right) {
span_lint(cx, span_lint(cx,
EQ_OP, EQ_OP,
e.span, e.span,
&format!("equal expressions as operands to `{}`", op.node.as_str())); &format!("equal expressions as operands to `{}`", op.node.as_str()));
return; return;
} }
let (trait_id, requires_ref) = match op.node { let (trait_id, requires_ref) = match op.node {
@ -91,32 +91,30 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
// either operator autorefs or both args are copyable // either operator autorefs or both args are copyable
if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty], None) { if (requires_ref || (lcpy && rcpy)) && implements_trait(cx, lty, trait_id, &[rty], None) {
span_lint_and_then(cx, span_lint_and_then(cx,
OP_REF, OP_REF,
e.span, e.span,
"needlessly taken reference of both operands", "needlessly taken reference of both operands",
|db| { |db| {
let lsnip = snippet(cx, l.span, "...").to_string(); let lsnip = snippet(cx, l.span, "...").to_string();
let rsnip = snippet(cx, r.span, "...").to_string(); let rsnip = snippet(cx, r.span, "...").to_string();
multispan_sugg(db, multispan_sugg(db,
"use the values directly".to_string(), "use the values directly".to_string(),
vec![(left.span, lsnip), vec![(left.span, lsnip),
(right.span, rsnip)]); (right.span, rsnip)]);
}) })
} else if lcpy && !rcpy && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)], None) { } else if lcpy && !rcpy &&
span_lint_and_then(cx, implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)], None) {
OP_REF, span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
e.span,
"needlessly taken reference of left operand",
|db| {
let lsnip = snippet(cx, l.span, "...").to_string(); let lsnip = snippet(cx, l.span, "...").to_string();
db.span_suggestion(left.span, "use the left value directly", lsnip); db.span_suggestion(left.span, "use the left value directly", lsnip);
}) })
} else if !lcpy && rcpy && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty], None) { } else if !lcpy && rcpy &&
implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty], None) {
span_lint_and_then(cx, span_lint_and_then(cx,
OP_REF, OP_REF,
e.span, e.span,
"needlessly taken reference of right operand", "needlessly taken reference of right operand",
|db| { |db| {
let rsnip = snippet(cx, r.span, "...").to_string(); let rsnip = snippet(cx, r.span, "...").to_string();
db.span_suggestion(right.span, "use the right value directly", rsnip); db.span_suggestion(right.span, "use the right value directly", rsnip);
}) })
@ -126,7 +124,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
(&ExprAddrOf(_, ref l), _) => { (&ExprAddrOf(_, ref l), _) => {
let lty = cx.tables.expr_ty(l); let lty = cx.tables.expr_ty(l);
let lcpy = is_copy(cx, lty, parent); let lcpy = is_copy(cx, lty, parent);
if (requires_ref || lcpy) && implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)], None) { if (requires_ref || lcpy) &&
implements_trait(cx, lty, trait_id, &[cx.tables.expr_ty(right)], None) {
span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| { span_lint_and_then(cx, OP_REF, e.span, "needlessly taken reference of left operand", |db| {
let lsnip = snippet(cx, l.span, "...").to_string(); let lsnip = snippet(cx, l.span, "...").to_string();
db.span_suggestion(left.span, "use the left value directly", lsnip); db.span_suggestion(left.span, "use the left value directly", lsnip);
@ -137,7 +136,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
(_, &ExprAddrOf(_, ref r)) => { (_, &ExprAddrOf(_, ref r)) => {
let rty = cx.tables.expr_ty(r); let rty = cx.tables.expr_ty(r);
let rcpy = is_copy(cx, rty, parent); let rcpy = is_copy(cx, rty, parent);
if (requires_ref || rcpy) && implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty], None) { if (requires_ref || rcpy) &&
implements_trait(cx, cx.tables.expr_ty(left), trait_id, &[rty], None) {
span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| { span_lint_and_then(cx, OP_REF, e.span, "taken reference of right operand", |db| {
let rsnip = snippet(cx, r.span, "...").to_string(); let rsnip = snippet(cx, r.span, "...").to_string();
db.span_suggestion(right.span, "use the right value directly", rsnip); db.span_suggestion(right.span, "use the right value directly", rsnip);

View file

@ -199,7 +199,8 @@ fn has_is_empty(cx: &LateContext, expr: &Expr) -> bool {
/// Check the inherent impl's items for an `is_empty(self)` method. /// Check the inherent impl's items for an `is_empty(self)` method.
fn has_is_empty_impl(cx: &LateContext, id: DefId) -> bool { fn has_is_empty_impl(cx: &LateContext, id: DefId) -> bool {
cx.tcx.inherent_impls(id) cx.tcx
.inherent_impls(id)
.iter() .iter()
.any(|imp| cx.tcx.associated_items(*imp).any(|item| is_is_empty(cx, &item))) .any(|imp| cx.tcx.associated_items(*imp).any(|item| is_is_empty(cx, &item)))
} }

View file

@ -29,7 +29,7 @@
//! This lint is **warn** by default. //! This lint is **warn** by default.
use rustc::lint::*; use rustc::lint::*;
use syntax::ast; use syntax::ast;
use syntax::codemap::{original_sp,DUMMY_SP}; use syntax::codemap::{original_sp, DUMMY_SP};
use std::borrow::Cow; use std::borrow::Cow;
use utils::{in_macro, span_help_and_lint, snippet_block, snippet, trim_multiline}; use utils::{in_macro, span_help_and_lint, snippet_block, snippet, trim_multiline};
@ -163,7 +163,7 @@ impl EarlyLintPass for NeedlessContinue {
* // region C * // region C
* } * }
* } * }
*/ * */
/// Given an expression, returns true if either of the following is true /// Given an expression, returns true if either of the following is true
/// ///
@ -181,10 +181,12 @@ fn needless_continue_in_else(else_expr: &ast::Expr) -> bool {
fn is_first_block_stmt_continue(block: &ast::Block) -> bool { fn is_first_block_stmt_continue(block: &ast::Block) -> bool {
block.stmts.get(0).map_or(false, |stmt| match stmt.node { block.stmts.get(0).map_or(false, |stmt| match stmt.node {
ast::StmtKind::Semi(ref e) | ast::StmtKind::Semi(ref e) |
ast::StmtKind::Expr(ref e) => if let ast::ExprKind::Continue(_) = e.node { ast::StmtKind::Expr(ref e) => {
true if let ast::ExprKind::Continue(_) = e.node {
} else { true
false } else {
false
}
}, },
_ => false, _ => false,
}) })
@ -192,12 +194,14 @@ fn is_first_block_stmt_continue(block: &ast::Block) -> bool {
/// 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
/// the AST object representing the loop block of `expr`. /// the AST object representing the loop block of `expr`.
fn with_loop_block<F>(expr: &ast::Expr, mut func: F) where F: FnMut(&ast::Block) { fn with_loop_block<F>(expr: &ast::Expr, mut func: F)
where F: FnMut(&ast::Block)
{
match expr.node { match expr.node {
ast::ExprKind::While(_, ref loop_block, _) | ast::ExprKind::While(_, ref loop_block, _) |
ast::ExprKind::WhileLet(_, _, ref loop_block, _) | ast::ExprKind::WhileLet(_, _, ref loop_block, _) |
ast::ExprKind::ForLoop( _, _, ref loop_block, _) | ast::ExprKind::ForLoop(_, _, ref loop_block, _) |
ast::ExprKind::Loop(ref loop_block, _) => func(loop_block), ast::ExprKind::Loop(ref loop_block, _) => func(loop_block),
_ => {}, _ => {},
} }
} }
@ -211,7 +215,8 @@ fn with_loop_block<F>(expr: &ast::Expr, mut func: F) where F: FnMut(&ast::Block)
/// - The `else` expression. /// - 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)
{
match stmt.node { match stmt.node {
ast::StmtKind::Semi(ref e) | ast::StmtKind::Semi(ref e) |
ast::StmtKind::Expr(ref e) => { ast::StmtKind::Expr(ref e) => {
@ -219,7 +224,7 @@ fn with_if_expr<F>(stmt: &ast::Stmt, mut func: F)
func(e, cond, if_block, else_expr); func(e, cond, if_block, else_expr);
} }
}, },
_ => { }, _ => {},
} }
} }
@ -249,45 +254,37 @@ struct LintData<'a> {
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";
const MSG_ELSE_BLOCK_NOT_NEEDED: &'static str = "There is no need for an explicit `else` block for this `if` expression\n"; const MSG_ELSE_BLOCK_NOT_NEEDED: &'static str = "There is no need for an explicit `else` block for this `if` \
expression\n";
const DROP_ELSE_BLOCK_AND_MERGE_MSG: &'static str = const DROP_ELSE_BLOCK_AND_MERGE_MSG: &'static str = "Consider dropping the else clause and merging the code that \
"Consider dropping the else clause and merging the code that follows (in the loop) with the if block, like so:\n"; follows (in the loop) with the if block, like so:\n";
const DROP_ELSE_BLOCK_MSG: &'static str = const DROP_ELSE_BLOCK_MSG: &'static str = "Consider dropping the else clause, and moving out the code in the else \
"Consider dropping the else clause, and moving out the code in the else block, like so:\n"; block, like so:\n";
fn emit_warning<'a>(ctx: &EarlyContext, fn emit_warning<'a>(ctx: &EarlyContext, data: &'a LintData, header: &str, typ: LintType) {
data: &'a LintData,
header: &str,
typ: LintType) {
// snip is the whole *help* message that appears after the warning. // snip is the whole *help* message that appears after the warning.
// 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 {
LintType::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, data.else_expr)
MSG_REDUNDANT_ELSE_BLOCK,
data.else_expr)
}, },
LintType::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, data.if_expr)
MSG_ELSE_BLOCK_NOT_NEEDED, },
data.if_expr)
}
}; };
span_help_and_lint(ctx, NEEDLESS_CONTINUE, expr.span, message, &snip); span_help_and_lint(ctx, NEEDLESS_CONTINUE, expr.span, message, &snip);
} }
fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext, fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext, data: &'a LintData, header: &str) -> String {
data: &'a LintData,
header: &str) -> String {
let cond_code = snippet(ctx, data.if_cond.span, ".."); 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. */
// region B // region B
let else_code = snippet(ctx, data.else_expr.span, "..").into_owned(); let else_code = snippet(ctx, data.else_expr.span, "..").into_owned();
let else_code = erode_block(&else_code); let else_code = erode_block(&else_code);
@ -300,12 +297,9 @@ fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext,
ret ret
} }
fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext, fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext, data: &'a LintData, header: &str) -> String {
data: &'a LintData,
header: &str) -> String
{
let cond_code = snippet(ctx, data.if_cond.span, ".."); 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();
@ -318,13 +312,12 @@ fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext,
// These is the code in the loop block that follows the if/else construction // 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 // we are complaining about. We want to pull all of this code into the
// `then` block of the `if` statement. // `then` block of the `if` statement.
let to_annex = data.block_stmts[data.stmt_idx+1..] let to_annex = data.block_stmts[data.stmt_idx + 1..]
.iter() .iter()
.map(|stmt| { .map(|stmt| original_sp(stmt.span, DUMMY_SP))
original_sp(stmt.span, DUMMY_SP) .map(|span| snippet_block(ctx, span, "..").into_owned())
}) .collect::<Vec<_>>()
.map(|span| snippet_block(ctx, span, "..").into_owned()) .join("\n");
.collect::<Vec<_>>().join("\n");
let mut ret = String::from(header); let mut ret = String::from(header);
@ -336,24 +329,22 @@ fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext,
} }
fn check_and_warn<'a>(ctx: &EarlyContext, expr: &'a ast::Expr) { fn check_and_warn<'a>(ctx: &EarlyContext, expr: &'a ast::Expr) {
with_loop_block(expr, |loop_block| { with_loop_block(expr, |loop_block| for (i, stmt) in loop_block.stmts.iter().enumerate() {
for (i, stmt) in loop_block.stmts.iter().enumerate() { with_if_expr(stmt, |if_expr, cond, then_block, else_expr| {
with_if_expr(stmt, |if_expr, cond, then_block, else_expr| { let data = &LintData {
let data = &LintData { stmt_idx: i,
stmt_idx: i, if_expr: if_expr,
if_expr: if_expr, if_cond: cond,
if_cond: cond, if_block: then_block,
if_block: then_block, else_expr: else_expr,
else_expr: else_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, LintType::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, LintType::ContinueInsideThenBlock);
emit_warning(ctx, data, DROP_ELSE_BLOCK_MSG, LintType::ContinueInsideThenBlock); }
} });
});
}
}); });
} }
@ -378,7 +369,7 @@ fn check_and_warn<'a>(ctx: &EarlyContext, expr: &'a ast::Expr) {
/// an empty string will be returned in that case. /// an empty string will be returned in that case.
pub fn erode_from_back(s: &str) -> String { pub fn erode_from_back(s: &str) -> String {
let mut ret = String::from(s); let mut ret = String::from(s);
while ret.pop().map_or(false, |c| c != '}') { } while ret.pop().map_or(false, |c| c != '}') {}
while let Some(c) = ret.pop() { while let Some(c) = ret.pop() {
if !c.is_whitespace() { if !c.is_whitespace() {
ret.push(c); ret.push(c);
@ -409,10 +400,10 @@ pub fn erode_from_back(s: &str) -> String {
/// ///
pub fn erode_from_front(s: &str) -> String { pub fn erode_from_front(s: &str) -> String {
s.chars() s.chars()
.skip_while(|c| c.is_whitespace()) .skip_while(|c| c.is_whitespace())
.skip_while(|c| *c == '{') .skip_while(|c| *c == '{')
.skip_while(|c| *c == '\n') .skip_while(|c| *c == '\n')
.collect::<String>() .collect::<String>()
} }
/// If `s` contains the code for a block, delimited by braces, this function /// If `s` contains the code for a block, delimited by braces, this function

View file

@ -284,16 +284,7 @@ impl<'a, 'tcx: 'a> euv::Delegate<'tcx> for MovedVariablesCtxt<'a, 'tcx> {
} }
} }
fn borrow( fn borrow(&mut self, _: NodeId, _: Span, _: mc::cmt<'tcx>, _: ty::Region, _: ty::BorrowKind, _: euv::LoanCause) {}
&mut self,
_: NodeId,
_: Span,
_: mc::cmt<'tcx>,
_: ty::Region,
_: ty::BorrowKind,
_: euv::LoanCause
) {
}
fn mutate(&mut self, _: NodeId, _: Span, _: mc::cmt<'tcx>, _: euv::MutateMode) {} fn mutate(&mut self, _: NodeId, _: Span, _: mc::cmt<'tcx>, _: euv::MutateMode) {}

View file

@ -978,4 +978,3 @@ pub fn type_size<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: ty::Ty<'tcx>) -> Opti
.infer_ctxt((), Reveal::All) .infer_ctxt((), Reveal::All)
.enter(|infcx| ty.layout(&infcx).ok().map(|lay| lay.size(&TargetDataLayout::parse(cx.sess())).bytes())) .enter(|infcx| ty.layout(&infcx).ok().map(|lay| lay.size(&TargetDataLayout::parse(cx.sess())).bytes()))
} }