2015-08-16 01:54:43 -05:00
|
|
|
use rustc::lint::*;
|
2015-09-03 09:42:17 -05:00
|
|
|
use rustc_front::hir::*;
|
|
|
|
use reexport::*;
|
2015-08-11 15:06:30 -05:00
|
|
|
use syntax::codemap::{Span, Spanned};
|
2015-09-03 09:42:17 -05:00
|
|
|
use rustc_front::visit::FnKind;
|
2015-08-11 14:47:34 -05:00
|
|
|
|
2015-08-26 17:31:35 -05:00
|
|
|
use utils::{span_lint, snippet, match_path, in_external_macro};
|
2015-08-11 14:47:34 -05:00
|
|
|
|
|
|
|
declare_lint!(pub NEEDLESS_RETURN, Warn,
|
2015-08-13 03:32:35 -05:00
|
|
|
"using a return statement like `return expr;` where an expression would suffice");
|
2015-08-11 15:06:30 -05:00
|
|
|
declare_lint!(pub LET_AND_RETURN, Warn,
|
2015-08-13 03:32:35 -05:00
|
|
|
"creating a let-binding and then immediately returning it like `let x = expr; x` at \
|
|
|
|
the end of a function");
|
2015-08-11 14:47:34 -05:00
|
|
|
|
2015-08-21 10:11:34 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2015-08-11 14:47:34 -05:00
|
|
|
pub struct ReturnPass;
|
|
|
|
|
|
|
|
impl ReturnPass {
|
|
|
|
// Check the final stmt or expr in a block for unnecessary return.
|
2015-09-18 21:53:04 -05:00
|
|
|
fn check_block_return(&mut self, cx: &LateContext, block: &Block) {
|
2015-08-11 14:47:34 -05:00
|
|
|
if let Some(ref expr) = block.expr {
|
|
|
|
self.check_final_expr(cx, expr);
|
|
|
|
} else if let Some(stmt) = block.stmts.last() {
|
|
|
|
if let StmtSemi(ref expr, _) = stmt.node {
|
|
|
|
if let ExprRet(Some(ref inner)) = expr.node {
|
2015-08-11 15:06:30 -05:00
|
|
|
self.emit_return_lint(cx, (expr.span, inner.span));
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check a the final expression in a block if it's a return.
|
2015-09-18 21:53:04 -05:00
|
|
|
fn check_final_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
2015-08-11 14:47:34 -05:00
|
|
|
match expr.node {
|
|
|
|
// simple return is always "bad"
|
|
|
|
ExprRet(Some(ref inner)) => {
|
2015-08-11 15:06:30 -05:00
|
|
|
self.emit_return_lint(cx, (expr.span, inner.span));
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
|
|
|
// a whole block? check it!
|
|
|
|
ExprBlock(ref block) => {
|
|
|
|
self.check_block_return(cx, block);
|
|
|
|
}
|
|
|
|
// an if/if let expr, check both exprs
|
|
|
|
// note, if without else is going to be a type checking error anyways
|
|
|
|
// (except for unit type functions) so we don't match it
|
2015-09-03 09:42:17 -05:00
|
|
|
ExprIf(_, ref ifblock, Some(ref elsexpr)) => {
|
2015-08-11 14:47:34 -05:00
|
|
|
self.check_block_return(cx, ifblock);
|
|
|
|
self.check_final_expr(cx, elsexpr);
|
|
|
|
}
|
|
|
|
// a match expr, check all arms
|
|
|
|
ExprMatch(_, ref arms, _) => {
|
|
|
|
for arm in arms {
|
2015-08-25 07:41:35 -05:00
|
|
|
self.check_final_expr(cx, &arm.body);
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 21:53:04 -05:00
|
|
|
fn emit_return_lint(&mut self, cx: &LateContext, spans: (Span, Span)) {
|
2015-08-26 17:31:35 -05:00
|
|
|
if in_external_macro(cx, spans.1) {return;}
|
2015-08-11 14:47:34 -05:00
|
|
|
span_lint(cx, NEEDLESS_RETURN, spans.0, &format!(
|
2015-08-12 03:46:49 -05:00
|
|
|
"unneeded return statement. Consider using `{}` \
|
2015-08-26 07:26:43 -05:00
|
|
|
without the return and trailing semicolon",
|
2015-08-11 14:47:34 -05:00
|
|
|
snippet(cx, spans.1, "..")))
|
|
|
|
}
|
2015-08-11 15:06:30 -05:00
|
|
|
|
|
|
|
// Check for "let x = EXPR; x"
|
2015-09-18 21:53:04 -05:00
|
|
|
fn check_let_return(&mut self, cx: &LateContext, block: &Block) {
|
2015-08-11 15:06:30 -05:00
|
|
|
// we need both a let-binding stmt and an expr
|
2015-08-12 00:48:00 -05:00
|
|
|
if_let_chain! {
|
|
|
|
[
|
2015-08-12 14:11:32 -05:00
|
|
|
let Some(stmt) = block.stmts.last(),
|
|
|
|
let StmtDecl(ref decl, _) = stmt.node,
|
|
|
|
let DeclLocal(ref local) = decl.node,
|
|
|
|
let Some(ref initexpr) = local.init,
|
|
|
|
let PatIdent(_, Spanned { node: id, .. }, _) = local.pat.node,
|
|
|
|
let Some(ref retexpr) = block.expr,
|
|
|
|
let ExprPath(_, ref path) = retexpr.node,
|
2015-08-25 07:41:35 -05:00
|
|
|
match_path(path, &[&id.name.as_str()])
|
2015-08-12 00:48:00 -05:00
|
|
|
], {
|
2015-08-12 14:11:32 -05:00
|
|
|
self.emit_let_lint(cx, retexpr.span, initexpr.span);
|
2015-08-11 15:06:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 21:53:04 -05:00
|
|
|
fn emit_let_lint(&mut self, cx: &LateContext, lint_span: Span, note_span: Span) {
|
2015-08-26 17:31:35 -05:00
|
|
|
if in_external_macro(cx, note_span) {return;}
|
2015-08-11 15:06:30 -05:00
|
|
|
span_lint(cx, LET_AND_RETURN, lint_span,
|
|
|
|
"returning the result of a let binding. \
|
|
|
|
Consider returning the expression directly.");
|
|
|
|
if cx.current_level(LET_AND_RETURN) != Level::Allow {
|
|
|
|
cx.sess().span_note(note_span,
|
|
|
|
"this expression can be directly returned");
|
|
|
|
}
|
|
|
|
}
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for ReturnPass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2015-08-11 15:06:30 -05:00
|
|
|
lint_array!(NEEDLESS_RETURN, LET_AND_RETURN)
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
2015-09-18 21:53:04 -05:00
|
|
|
}
|
2015-08-11 14:47:34 -05:00
|
|
|
|
2015-09-18 21:53:04 -05:00
|
|
|
impl LateLintPass for ReturnPass {
|
|
|
|
fn check_fn(&mut self, cx: &LateContext, _: FnKind, _: &FnDecl,
|
2015-08-16 01:54:43 -05:00
|
|
|
block: &Block, _: Span, _: NodeId) {
|
2015-08-11 14:47:34 -05:00
|
|
|
self.check_block_return(cx, block);
|
2015-08-11 15:06:30 -05:00
|
|
|
self.check_let_return(cx, block);
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
|
|
|
}
|