Skip to content

Commit

Permalink
Auto merge of #10109 - Niki4tap:yeet_not_return, r=flip1995
Browse files Browse the repository at this point in the history
Fix FP in needless_return when using yeet

Fixes #9947

changelog: Fix: [`needless_return`]: don't lint when using `do yeet`
#10109
  • Loading branch information
bors committed Dec 22, 2022
2 parents 065c6f7 + b6882f6 commit f0d331a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 47 deletions.
8 changes: 7 additions & 1 deletion clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::ops::ControlFlow;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::intravisit::FnKind;
use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, MatchSource, PatKind, StmtKind};
use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, LangItem, MatchSource, PatKind, QPath, StmtKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::subst::GenericArgKind;
Expand Down Expand Up @@ -207,6 +207,12 @@ fn check_final_expr<'tcx>(
match &peeled_drop_expr.kind {
// simple return is always "bad"
ExprKind::Ret(ref inner) => {
// if desugar of `do yeet`, don't lint
if let Some(inner_expr) = inner
&& let ExprKind::Call(path_expr, _) = inner_expr.kind
&& let ExprKind::Path(QPath::LangItem(LangItem::TryTraitFromYeet, _, _)) = path_expr.kind {
return;
}
if cx.tcx.hir().attrs(expr.hir_id).is_empty() {
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
if !borrows {
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// run-rustfix

#![feature(lint_reasons)]
#![feature(yeet_expr)]
#![allow(unused)]
#![allow(
clippy::if_same_then_else,
Expand Down Expand Up @@ -272,4 +273,8 @@ mod issue9416 {
}
}

fn issue9947() -> Result<(), String> {
do yeet "hello";
}

fn main() {}
5 changes: 5 additions & 0 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// run-rustfix

#![feature(lint_reasons)]
#![feature(yeet_expr)]
#![allow(unused)]
#![allow(
clippy::if_same_then_else,
Expand Down Expand Up @@ -282,4 +283,8 @@ mod issue9416 {
}
}

fn issue9947() -> Result<(), String> {
do yeet "hello";
}

fn main() {}
Loading

0 comments on commit f0d331a

Please sign in to comment.