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,
tables: self.tcx.typeck_tables_of(def_id),
needed_resolution: false,
substs,
substs: substs,
};
let body = if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
self.tcx.mir_const_qualif(def_id);

View file

@ -102,16 +102,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
vec![(left.span, lsnip),
(right.span, rsnip)]);
})
} else if lcpy && !rcpy && 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| {
} else if lcpy && !rcpy &&
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| {
let lsnip = snippet(cx, l.span, "...").to_string();
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,
OP_REF,
e.span,
@ -126,7 +124,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
(&ExprAddrOf(_, ref l), _) => {
let lty = cx.tables.expr_ty(l);
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| {
let lsnip = snippet(cx, l.span, "...").to_string();
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)) => {
let rty = cx.tables.expr_ty(r);
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| {
let rsnip = snippet(cx, r.span, "...").to_string();
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.
fn has_is_empty_impl(cx: &LateContext, id: DefId) -> bool {
cx.tcx.inherent_impls(id)
cx.tcx
.inherent_impls(id)
.iter()
.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.
use rustc::lint::*;
use syntax::ast;
use syntax::codemap::{original_sp,DUMMY_SP};
use syntax::codemap::{original_sp, DUMMY_SP};
use std::borrow::Cow;
use utils::{in_macro, span_help_and_lint, snippet_block, snippet, trim_multiline};
@ -163,7 +163,7 @@ impl EarlyLintPass for NeedlessContinue {
* // region C
* }
* }
*/
* */
/// 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 {
block.stmts.get(0).map_or(false, |stmt| match stmt.node {
ast::StmtKind::Semi(ref e) |
ast::StmtKind::Expr(ref e) => if let ast::ExprKind::Continue(_) = e.node {
ast::StmtKind::Expr(ref e) => {
if let ast::ExprKind::Continue(_) = e.node {
true
} else {
false
}
},
_ => false,
})
@ -192,11 +194,13 @@ fn is_first_block_stmt_continue(block: &ast::Block) -> bool {
/// If `expr` is a loop expression (while/while let/for/loop), calls `func` with
/// 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 {
ast::ExprKind::While(_, 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),
_ => {},
}
@ -211,7 +215,8 @@ fn with_loop_block<F>(expr: &ast::Expr, mut func: F) where F: FnMut(&ast::Block)
/// - The `else` expression.
///
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 {
ast::StmtKind::Semi(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);
}
},
_ => { },
_ => {},
}
}
@ -249,41 +254,33 @@ struct LintData<'a> {
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 =
"Consider dropping the else clause and merging the code that follows (in the loop) with the if block, like so:\n";
const DROP_ELSE_BLOCK_AND_MERGE_MSG: &'static str = "Consider dropping the else clause and merging the code that \
follows (in the loop) with the if block, like so:\n";
const DROP_ELSE_BLOCK_MSG: &'static str =
"Consider dropping the else clause, and moving out the code in the else block, like so:\n";
const DROP_ELSE_BLOCK_MSG: &'static str = "Consider dropping the else clause, and moving out the code in the else \
block, like so:\n";
fn emit_warning<'a>(ctx: &EarlyContext,
data: &'a LintData,
header: &str,
typ: LintType) {
fn emit_warning<'a>(ctx: &EarlyContext, data: &'a LintData, header: &str, typ: LintType) {
// 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 {
LintType::ContinueInsideElseBlock => {
(suggestion_snippet_for_continue_inside_else(ctx, data, header),
MSG_REDUNDANT_ELSE_BLOCK,
data.else_expr)
(suggestion_snippet_for_continue_inside_else(ctx, data, header), MSG_REDUNDANT_ELSE_BLOCK, data.else_expr)
},
LintType::ContinueInsideThenBlock => {
(suggestion_snippet_for_continue_inside_if(ctx, data, header),
MSG_ELSE_BLOCK_NOT_NEEDED,
data.if_expr)
}
(suggestion_snippet_for_continue_inside_if(ctx, data, header), MSG_ELSE_BLOCK_NOT_NEEDED, data.if_expr)
},
};
span_help_and_lint(ctx, NEEDLESS_CONTINUE, expr.span, message, &snip);
}
fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext,
data: &'a LintData,
header: &str) -> String {
fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext, data: &'a LintData, header: &str) -> String {
let cond_code = snippet(ctx, data.if_cond.span, "..");
let if_code = format!("if {} {{\n continue;\n}}\n", cond_code);
@ -300,10 +297,7 @@ fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext,
ret
}
fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext,
data: &'a LintData,
header: &str) -> String
{
fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext, data: &'a LintData, header: &str) -> String {
let cond_code = snippet(ctx, data.if_cond.span, "..");
let mut if_code = format!("if {} {{\n", cond_code);
@ -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
// we are complaining about. We want to pull all of this code into the
// `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()
.map(|stmt| {
original_sp(stmt.span, DUMMY_SP)
})
.map(|stmt| original_sp(stmt.span, DUMMY_SP))
.map(|span| snippet_block(ctx, span, "..").into_owned())
.collect::<Vec<_>>().join("\n");
.collect::<Vec<_>>()
.join("\n");
let mut ret = String::from(header);
@ -336,8 +329,7 @@ fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext,
}
fn check_and_warn<'a>(ctx: &EarlyContext, expr: &'a ast::Expr) {
with_loop_block(expr, |loop_block| {
for (i, stmt) in loop_block.stmts.iter().enumerate() {
with_loop_block(expr, |loop_block| 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,
@ -353,7 +345,6 @@ fn check_and_warn<'a>(ctx: &EarlyContext, expr: &'a ast::Expr) {
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.
pub fn erode_from_back(s: &str) -> String {
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() {
if !c.is_whitespace() {
ret.push(c);

View file

@ -284,16 +284,7 @@ impl<'a, 'tcx: 'a> euv::Delegate<'tcx> for MovedVariablesCtxt<'a, 'tcx> {
}
}
fn borrow(
&mut self,
_: NodeId,
_: Span,
_: mc::cmt<'tcx>,
_: ty::Region,
_: ty::BorrowKind,
_: euv::LoanCause
) {
}
fn borrow(&mut self, _: NodeId, _: Span, _: mc::cmt<'tcx>, _: ty::Region, _: ty::BorrowKind, _: euv::LoanCause) {}
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)
.enter(|infcx| ty.layout(&infcx).ok().map(|lay| lay.size(&TargetDataLayout::parse(cx.sess())).bytes()))
}