Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions clippy_lints/src/returns/needless_return_with_question_mark.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::res::{MaybeDef, MaybeQPath};
use clippy_utils::{is_from_proc_macro, is_inside_let_else};
use clippy_utils::res::{MaybeDef, MaybeQPath, MaybeResPath};
use clippy_utils::usage::local_used_after_expr;
use clippy_utils::{higher, is_from_proc_macro, is_inside_let_else};
use rustc_errors::Applicability;
use rustc_hir::LangItem::ResultErr;
use rustc_hir::{Expr, ExprKind, HirId, MatchSource, Node, Stmt, StmtKind};
Expand Down Expand Up @@ -31,6 +32,20 @@ pub(super) fn check_stmt<'tcx>(cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
&& !is_from_proc_macro(cx, expr)
&& !stmt_needs_never_type(cx, stmt.hir_id)
{
// get the hir id of parent of return statement
for (_hir_id, node) in cx.tcx.hir_parent_iter(expr.hir_id) {
if let Node::Expr(parent_expr) = node {
if let Some(if_let) = higher::IfLet::hir(cx, parent_expr) {
let scrutinee = if_let.let_expr;
if let Some(local_id) = scrutinee.res_local_id()
&& local_used_after_expr(cx, local_id, parent_expr)
{
return;
}
}
}
}

span_lint_and_sugg(
cx,
NEEDLESS_RETURN_WITH_QUESTION_MARK,
Expand Down
11 changes: 10 additions & 1 deletion tests/ui/needless_return_with_question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,16 @@ fn issue11982_no_conversion() {
Ok(())
}
}

#[allow(clippy::unnecessary_literal_unwrap)]
fn issue17421_no_lint_partial_move_after_if_let() -> Result<(), String> {
let v: Result<String, String> = Err("".to_string());
if let Err(e) = v {
return Err(e)?;
}
let v = v.unwrap();
println!("{}", v);
Ok(())
}
fn general_return() {
fn foo(ok: bool) -> Result<(), ()> {
let bar = Result::Ok(Result::<(), ()>::Ok(()));
Expand Down
11 changes: 10 additions & 1 deletion tests/ui/needless_return_with_question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,16 @@ fn issue11982_no_conversion() {
Ok(())
}
}

#[allow(clippy::unnecessary_literal_unwrap)]
fn issue17421_no_lint_partial_move_after_if_let() -> Result<(), String> {
let v: Result<String, String> = Err("".to_string());
if let Err(e) = v {
return Err(e)?;
}
let v = v.unwrap();
println!("{}", v);
Ok(())
}
fn general_return() {
fn foo(ok: bool) -> Result<(), ()> {
let bar = Result::Ok(Result::<(), ()>::Ok(()));
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/needless_return_with_question_mark.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ LL | return Err(())?;
| ^^^^^^^ help: remove it

error: unneeded `return` statement with `?` operator
--> tests/ui/needless_return_with_question_mark.rs:133:9
--> tests/ui/needless_return_with_question_mark.rs:142:9
|
LL | return Err(())?;
| ^^^^^^^ help: remove it

error: unneeded `return` statement with `?` operator
--> tests/ui/needless_return_with_question_mark.rs:142:13
--> tests/ui/needless_return_with_question_mark.rs:151:13
|
LL | return Err(())?;
| ^^^^^^^ help: remove it

error: unneeded `return` statement with `?` operator
--> tests/ui/needless_return_with_question_mark.rs:162:5
--> tests/ui/needless_return_with_question_mark.rs:171:5
|
LL | return Err(())?;
| ^^^^^^^ help: remove it
Expand Down
Loading