mirror of
https://github.com/rust-lang/rust.git
synced 2026-06-03 01:16:14 +03:00
Fix false-positive in LET_AND_RETURN
If the declaration has a type, it might be required for coercion to happen.
This commit is contained in:
+11
-17
@@ -3,7 +3,7 @@
|
||||
use syntax::codemap::{Span, Spanned};
|
||||
use syntax::visit::FnKind;
|
||||
|
||||
use utils::{span_lint, span_lint_and_then, snippet_opt, match_path_ast, in_external_macro};
|
||||
use utils::{span_note_and_lint, span_lint_and_then, snippet_opt, match_path_ast, in_external_macro};
|
||||
|
||||
/// **What it does:** This lint checks for return statements at the end of a block.
|
||||
///
|
||||
@@ -95,29 +95,23 @@ fn check_let_return(&mut self, cx: &EarlyContext, block: &Block) {
|
||||
let Some(ref retexpr) = block.expr,
|
||||
let StmtKind::Decl(ref decl, _) = stmt.node,
|
||||
let DeclKind::Local(ref local) = decl.node,
|
||||
local.ty.is_none(),
|
||||
let Some(ref initexpr) = local.init,
|
||||
let PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
|
||||
let ExprKind::Path(_, ref path) = retexpr.node,
|
||||
match_path_ast(path, &[&id.name.as_str()])
|
||||
match_path_ast(path, &[&id.name.as_str()]),
|
||||
!in_external_macro(cx, initexpr.span),
|
||||
], {
|
||||
self.emit_let_lint(cx, retexpr.span, initexpr.span);
|
||||
span_note_and_lint(cx,
|
||||
LET_AND_RETURN,
|
||||
retexpr.span,
|
||||
"returning the result of a let binding from a block. \
|
||||
Consider returning the expression directly.",
|
||||
initexpr.span,
|
||||
"this expression can be directly returned");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_let_lint(&mut self, cx: &EarlyContext, lint_span: Span, note_span: Span) {
|
||||
if in_external_macro(cx, note_span) {
|
||||
return;
|
||||
}
|
||||
let mut db = span_lint(cx,
|
||||
LET_AND_RETURN,
|
||||
lint_span,
|
||||
"returning the result of a let binding from a block. Consider returning the \
|
||||
expression directly.");
|
||||
if cx.current_level(LET_AND_RETURN) != Level::Allow {
|
||||
db.span_note(note_span, "this expression can be directly returned");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LintPass for ReturnPass {
|
||||
|
||||
Reference in New Issue
Block a user