2022-10-06 02:44:38 -05:00
|
|
|
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
|
2022-06-04 06:34:07 -05:00
|
|
|
use clippy_utils::source::{snippet_opt, snippet_with_context};
|
2022-10-06 02:44:38 -05:00
|
|
|
use clippy_utils::visitors::{for_each_expr, Descend};
|
2021-12-06 05:33:31 -06:00
|
|
|
use clippy_utils::{fn_def_id, path_to_local_id};
|
2022-10-06 02:44:38 -05:00
|
|
|
use core::ops::ControlFlow;
|
2018-11-27 14:14:15 -06:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2022-10-06 02:44:38 -05:00
|
|
|
use rustc_hir::intravisit::FnKind;
|
2022-12-29 07:28:34 -06:00
|
|
|
use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, LangItem, MatchSource, PatKind, QPath, StmtKind};
|
2020-08-28 09:10:16 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2020-08-28 09:10:16 -05:00
|
|
|
use rustc_middle::ty::subst::GenericArgKind;
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-01-04 04:00:00 -06:00
|
|
|
use rustc_span::source_map::Span;
|
2022-12-01 11:29:38 -06:00
|
|
|
use rustc_span::{BytePos, Pos};
|
2015-08-11 14:47:34 -05:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `let`-bindings, which are subsequently
|
2020-08-28 09:10:16 -05:00
|
|
|
/// returned.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It is just extraneous code. Remove it to make your code
|
2019-03-05 10:50:33 -06:00
|
|
|
/// more rusty.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2020-08-28 09:10:16 -05:00
|
|
|
/// fn foo() -> String {
|
|
|
|
/// let x = String::new();
|
|
|
|
/// x
|
2019-03-05 10:50:33 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2020-08-28 09:10:16 -05:00
|
|
|
/// instead, use
|
|
|
|
/// ```
|
|
|
|
/// fn foo() -> String {
|
|
|
|
/// String::new()
|
2019-03-05 10:50:33 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2020-08-28 09:10:16 -05:00
|
|
|
pub LET_AND_RETURN,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2020-08-28 09:10:16 -05:00
|
|
|
"creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block"
|
2016-02-05 17:13:29 -06:00
|
|
|
}
|
|
|
|
|
2018-09-27 12:10:20 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for return statements at the end of a block.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Removing the `return` and semicolon will make the code
|
2020-08-28 09:10:16 -05:00
|
|
|
/// more rusty.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2020-08-28 09:10:16 -05:00
|
|
|
/// fn foo(x: usize) -> usize {
|
|
|
|
/// return x;
|
2019-03-05 10:50:33 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2020-08-28 09:10:16 -05:00
|
|
|
/// simplify to
|
|
|
|
/// ```rust
|
|
|
|
/// fn foo(x: usize) -> usize {
|
|
|
|
/// x
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2020-08-28 09:10:16 -05:00
|
|
|
pub NEEDLESS_RETURN,
|
2018-09-27 12:10:20 -05:00
|
|
|
style,
|
2020-08-28 09:10:16 -05:00
|
|
|
"using a return statement like `return expr;` where an expression would suffice"
|
2018-09-27 12:10:20 -05:00
|
|
|
}
|
|
|
|
|
2019-06-19 07:56:02 -05:00
|
|
|
#[derive(PartialEq, Eq, Copy, Clone)]
|
|
|
|
enum RetReplacement {
|
|
|
|
Empty,
|
2019-07-08 13:45:51 -05:00
|
|
|
Block,
|
2021-12-30 08:10:43 -06:00
|
|
|
Unit,
|
2019-06-19 07:56:02 -05:00
|
|
|
}
|
|
|
|
|
2022-10-06 02:44:38 -05:00
|
|
|
impl RetReplacement {
|
|
|
|
fn sugg_help(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Empty => "remove `return`",
|
|
|
|
Self::Block => "replace `return` with an empty block",
|
|
|
|
Self::Unit => "replace `return` with a unit value",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for RetReplacement {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match *self {
|
|
|
|
Self::Empty => "",
|
|
|
|
Self::Block => "{}",
|
|
|
|
Self::Unit => "()",
|
|
|
|
}
|
|
|
|
.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:10:16 -05:00
|
|
|
declare_lint_pass!(Return => [LET_AND_RETURN, NEEDLESS_RETURN]);
|
2015-08-11 14:47:34 -05:00
|
|
|
|
2020-08-28 09:10:16 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for Return {
|
|
|
|
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
|
|
|
|
// we need both a let-binding stmt and an expr
|
|
|
|
if_chain! {
|
|
|
|
if let Some(retexpr) = block.expr;
|
|
|
|
if let Some(stmt) = block.stmts.iter().last();
|
2022-07-05 16:31:18 -05:00
|
|
|
if let StmtKind::Local(local) = &stmt.kind;
|
2020-08-28 09:10:16 -05:00
|
|
|
if local.ty.is_none();
|
2020-11-25 15:45:24 -06:00
|
|
|
if cx.tcx.hir().attrs(local.hir_id).is_empty();
|
2020-08-28 09:10:16 -05:00
|
|
|
if let Some(initexpr) = &local.init;
|
2021-04-22 04:31:13 -05:00
|
|
|
if let PatKind::Binding(_, local_id, _, _) = local.pat.kind;
|
|
|
|
if path_to_local_id(retexpr, local_id);
|
2020-08-28 09:10:16 -05:00
|
|
|
if !last_statement_borrows(cx, initexpr);
|
|
|
|
if !in_external_macro(cx.sess(), initexpr.span);
|
|
|
|
if !in_external_macro(cx.sess(), retexpr.span);
|
2021-12-06 05:33:31 -06:00
|
|
|
if !local.span.from_expansion();
|
2020-08-28 09:10:16 -05:00
|
|
|
then {
|
2022-06-30 03:50:09 -05:00
|
|
|
span_lint_hir_and_then(
|
2020-08-28 09:10:16 -05:00
|
|
|
cx,
|
|
|
|
LET_AND_RETURN,
|
2022-06-30 03:50:09 -05:00
|
|
|
retexpr.hir_id,
|
2020-08-28 09:10:16 -05:00
|
|
|
retexpr.span,
|
|
|
|
"returning the result of a `let` binding from a block",
|
|
|
|
|err| {
|
|
|
|
err.span_label(local.span, "unnecessary `let` binding");
|
|
|
|
|
|
|
|
if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
|
2021-04-08 10:50:13 -05:00
|
|
|
if !cx.typeck_results().expr_adjustments(retexpr).is_empty() {
|
2020-08-28 09:10:16 -05:00
|
|
|
snippet.push_str(" as _");
|
|
|
|
}
|
|
|
|
err.multipart_suggestion(
|
|
|
|
"return the expression directly",
|
|
|
|
vec![
|
|
|
|
(local.span, String::new()),
|
|
|
|
(retexpr.span, snippet),
|
|
|
|
],
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
err.span_help(initexpr.span, "this expression can be directly returned");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:10:16 -05:00
|
|
|
fn check_fn(
|
2019-06-21 07:46:34 -05:00
|
|
|
&mut self,
|
2020-08-28 09:10:16 -05:00
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
kind: FnKind<'tcx>,
|
|
|
|
_: &'tcx FnDecl<'tcx>,
|
|
|
|
body: &'tcx Body<'tcx>,
|
|
|
|
_: Span,
|
|
|
|
_: HirId,
|
2019-06-21 07:46:34 -05:00
|
|
|
) {
|
2020-08-28 09:10:16 -05:00
|
|
|
match kind {
|
2020-11-27 02:24:42 -06:00
|
|
|
FnKind::Closure => {
|
2021-01-30 11:06:34 -06:00
|
|
|
// when returning without value in closure, replace this `return`
|
|
|
|
// with an empty block to prevent invalid suggestion (see #6501)
|
|
|
|
let replacement = if let ExprKind::Ret(None) = &body.value.kind {
|
|
|
|
RetReplacement::Block
|
|
|
|
} else {
|
|
|
|
RetReplacement::Empty
|
|
|
|
};
|
2022-10-06 02:44:38 -05:00
|
|
|
check_final_expr(cx, body.value, vec![], replacement);
|
2021-01-30 11:06:34 -06:00
|
|
|
},
|
2020-08-28 09:10:16 -05:00
|
|
|
FnKind::ItemFn(..) | FnKind::Method(..) => {
|
2022-10-06 02:44:38 -05:00
|
|
|
check_block_return(cx, &body.value.kind, vec![]);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
|
|
|
}
|
2020-08-28 09:10:16 -05:00
|
|
|
}
|
2015-08-11 14:47:34 -05:00
|
|
|
|
2022-10-06 02:44:38 -05:00
|
|
|
// if `expr` is a block, check if there are needless returns in it
|
|
|
|
fn check_block_return<'tcx>(cx: &LateContext<'tcx>, expr_kind: &ExprKind<'tcx>, semi_spans: Vec<Span>) {
|
|
|
|
if let ExprKind::Block(block, _) = expr_kind {
|
|
|
|
if let Some(block_expr) = block.expr {
|
|
|
|
check_final_expr(cx, block_expr, semi_spans, RetReplacement::Empty);
|
|
|
|
} else if let Some(stmt) = block.stmts.iter().last() {
|
|
|
|
match stmt.kind {
|
|
|
|
StmtKind::Expr(expr) => {
|
|
|
|
check_final_expr(cx, expr, semi_spans, RetReplacement::Empty);
|
|
|
|
},
|
|
|
|
StmtKind::Semi(semi_expr) => {
|
|
|
|
let mut semi_spans_and_this_one = semi_spans;
|
|
|
|
// we only want the span containing the semicolon so we can remove it later. From `entry.rs:382`
|
|
|
|
if let Some(semicolon_span) = stmt.span.trim_start(semi_expr.span) {
|
|
|
|
semi_spans_and_this_one.push(semicolon_span);
|
|
|
|
check_final_expr(cx, semi_expr, semi_spans_and_this_one, RetReplacement::Empty);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
2020-08-28 09:10:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_final_expr<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &'tcx Expr<'tcx>,
|
2022-10-06 02:44:38 -05:00
|
|
|
semi_spans: Vec<Span>, /* containing all the places where we would need to remove semicolons if finding an
|
|
|
|
* needless return */
|
2020-08-28 09:10:16 -05:00
|
|
|
replacement: RetReplacement,
|
|
|
|
) {
|
2022-10-06 02:44:38 -05:00
|
|
|
let peeled_drop_expr = expr.peel_drop_temps();
|
|
|
|
match &peeled_drop_expr.kind {
|
2020-08-28 09:10:16 -05:00
|
|
|
// simple return is always "bad"
|
|
|
|
ExprKind::Ret(ref inner) => {
|
2022-12-29 07:28:34 -06:00
|
|
|
// if desugar of `do yeet`, don't lint
|
|
|
|
if let Some(inner_expr) = inner
|
|
|
|
&& let ExprKind::Call(path_expr, _) = inner_expr.kind
|
2023-01-12 12:48:13 -06:00
|
|
|
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitFromYeet, _, _)) = path_expr.kind
|
|
|
|
{
|
|
|
|
return;
|
2022-12-29 07:28:34 -06:00
|
|
|
}
|
2023-01-12 12:48:13 -06:00
|
|
|
if !cx.tcx.hir().attrs(expr.hir_id).is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
|
|
|
|
if borrows {
|
|
|
|
return;
|
2020-08-28 09:10:16 -05:00
|
|
|
}
|
2023-01-12 12:48:13 -06:00
|
|
|
// check if expr return nothing
|
|
|
|
let ret_span = if inner.is_none() && replacement == RetReplacement::Empty {
|
|
|
|
extend_span_to_previous_non_ws(cx, peeled_drop_expr.span)
|
|
|
|
} else {
|
|
|
|
peeled_drop_expr.span
|
|
|
|
};
|
|
|
|
|
|
|
|
emit_return_lint(cx, ret_span, semi_spans, inner.as_ref().map(|i| i.span), replacement);
|
2020-08-28 09:10:16 -05:00
|
|
|
},
|
2021-01-01 12:38:11 -06:00
|
|
|
ExprKind::If(_, then, else_clause_opt) => {
|
2022-10-06 02:44:38 -05:00
|
|
|
check_block_return(cx, &then.kind, semi_spans.clone());
|
2021-01-01 12:38:11 -06:00
|
|
|
if let Some(else_clause) = else_clause_opt {
|
2022-10-06 02:44:38 -05:00
|
|
|
check_block_return(cx, &else_clause.kind, semi_spans);
|
2021-01-01 12:38:11 -06:00
|
|
|
}
|
|
|
|
},
|
2020-08-28 09:10:16 -05:00
|
|
|
// a match expr, check all arms
|
|
|
|
// 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
|
2021-09-08 09:31:47 -05:00
|
|
|
ExprKind::Match(_, arms, MatchSource::Normal) => {
|
|
|
|
for arm in arms.iter() {
|
2022-10-06 02:44:38 -05:00
|
|
|
check_final_expr(cx, arm.body, semi_spans.clone(), RetReplacement::Unit);
|
2021-09-08 09:31:47 -05:00
|
|
|
}
|
2020-08-28 09:10:16 -05:00
|
|
|
},
|
2022-10-06 02:44:38 -05:00
|
|
|
// if it's a whole block, check it
|
|
|
|
other_expr_kind => check_block_return(cx, other_expr_kind, semi_spans),
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 03:50:09 -05:00
|
|
|
fn emit_return_lint(
|
|
|
|
cx: &LateContext<'_>,
|
|
|
|
ret_span: Span,
|
2022-10-06 02:44:38 -05:00
|
|
|
semi_spans: Vec<Span>,
|
2022-06-30 03:50:09 -05:00
|
|
|
inner_span: Option<Span>,
|
|
|
|
replacement: RetReplacement,
|
|
|
|
) {
|
2021-01-15 03:56:44 -06:00
|
|
|
if ret_span.from_expansion() {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-06 02:44:38 -05:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let return_replacement = inner_span.map_or_else(
|
|
|
|
|| replacement.to_string(),
|
|
|
|
|inner_span| {
|
|
|
|
let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
|
|
|
|
snippet.to_string()
|
2020-08-28 09:10:16 -05:00
|
|
|
},
|
2022-10-06 02:44:38 -05:00
|
|
|
);
|
|
|
|
let sugg_help = if inner_span.is_some() {
|
|
|
|
"remove `return`"
|
|
|
|
} else {
|
|
|
|
replacement.sugg_help()
|
|
|
|
};
|
|
|
|
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
|
|
|
|
diag.span_suggestion_hidden(ret_span, sugg_help, return_replacement, applicability);
|
|
|
|
// for each parent statement, we need to remove the semicolon
|
|
|
|
for semi_stmt_span in semi_spans {
|
|
|
|
diag.tool_only_span_suggestion(semi_stmt_span, "remove this semicolon", "", applicability);
|
|
|
|
}
|
|
|
|
});
|
2015-08-11 14:47:34 -05:00
|
|
|
}
|
2016-11-19 19:15:40 -06:00
|
|
|
|
2020-08-28 09:10:16 -05:00
|
|
|
fn last_statement_borrows<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
|
2022-10-06 02:44:38 -05:00
|
|
|
for_each_expr(expr, |e| {
|
|
|
|
if let Some(def_id) = fn_def_id(cx, e)
|
|
|
|
&& cx
|
2020-08-28 09:10:16 -05:00
|
|
|
.tcx
|
2023-01-18 17:52:47 -06:00
|
|
|
.fn_sig(def_id)
|
2023-01-18 16:43:20 -06:00
|
|
|
.subst_identity()
|
2020-08-28 09:10:16 -05:00
|
|
|
.skip_binder()
|
2022-10-06 02:44:38 -05:00
|
|
|
.output()
|
2022-01-11 21:19:52 -06:00
|
|
|
.walk()
|
2022-10-06 02:44:38 -05:00
|
|
|
.any(|arg| matches!(arg.unpack(), GenericArgKind::Lifetime(_)))
|
|
|
|
{
|
|
|
|
ControlFlow::Break(())
|
|
|
|
} else {
|
2023-01-12 12:48:13 -06:00
|
|
|
ControlFlow::Continue(Descend::from(!e.span.from_expansion()))
|
2020-08-28 09:10:16 -05:00
|
|
|
}
|
2022-10-06 02:44:38 -05:00
|
|
|
})
|
|
|
|
.is_some()
|
2020-05-17 10:36:26 -05:00
|
|
|
}
|
2022-12-01 11:29:38 -06:00
|
|
|
|
|
|
|
// Go backwards while encountering whitespace and extend the given Span to that point.
|
|
|
|
fn extend_span_to_previous_non_ws(cx: &LateContext<'_>, sp: Span) -> Span {
|
|
|
|
if let Ok(prev_source) = cx.sess().source_map().span_to_prev_source(sp) {
|
|
|
|
let ws = [' ', '\t', '\n'];
|
|
|
|
if let Some(non_ws_pos) = prev_source.rfind(|c| !ws.contains(&c)) {
|
|
|
|
let len = prev_source.len() - non_ws_pos - 1;
|
|
|
|
return sp.with_lo(sp.lo() - BytePos::from_usize(len));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sp
|
|
|
|
}
|