Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FP: needless_return_with_question_mark with implicit Error Conversion #12021

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then, span_lin
use clippy_utils::source::{snippet_opt, snippet_with_context};
use clippy_utils::sugg::has_enclosing_paren;
use clippy_utils::visitors::{for_each_expr_with_closures, Descend};
use clippy_utils::{fn_def_id, is_from_proc_macro, is_inside_let_else, path_to_local_id, span_find_starting_semi};
use clippy_utils::{
fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor, path_res, path_to_local_id,
span_find_starting_semi,
};
use core::ops::ControlFlow;
use rustc_errors::Applicability;
use rustc_hir::intravisit::FnKind;
use rustc_hir::LangItem::ResultErr;
use rustc_hir::{
Block, Body, Expr, ExprKind, FnDecl, HirId, ItemKind, LangItem, MatchSource, Node, OwnerNode, PatKind, QPath, Stmt,
StmtKind,
Expand Down Expand Up @@ -181,7 +185,15 @@ impl<'tcx> LateLintPass<'tcx> for Return {
if !in_external_macro(cx.sess(), stmt.span)
&& let StmtKind::Semi(expr) = stmt.kind
&& let ExprKind::Ret(Some(ret)) = expr.kind
&& let ExprKind::Match(.., MatchSource::TryDesugar(_)) = ret.kind
// return Err(...)? desugars to a match
// over a Err(...).branch()
// which breaks down to a branch call, with the callee being
// the constructor of the Err variant
&& let ExprKind::Match(maybe_cons, _, MatchSource::TryDesugar(_)) = ret.kind
&& let ExprKind::Call(_, [maybe_result_err]) = maybe_cons.kind
&& let ExprKind::Call(maybe_constr, _) = maybe_result_err.kind
&& is_res_lang_ctor(cx, path_res(cx, maybe_constr), ResultErr)

// Ensure this is not the final stmt, otherwise removing it would cause a compile error
&& let OwnerNode::Item(item) = cx.tcx.hir().owner(cx.tcx.hir().get_parent_item(expr.hir_id))
&& let ItemKind::Fn(_, _, body) = item.kind
Expand Down
50 changes: 50 additions & 0 deletions tests/ui/needless_return_with_question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,53 @@ fn issue11616() -> Result<(), ()> {
};
Ok(())
}

fn issue11982() {
mod bar {
pub struct Error;
pub fn foo(_: bool) -> Result<(), Error> {
Ok(())
}
}

pub struct Error;

impl From<bar::Error> for Error {
fn from(_: bar::Error) -> Self {
Error
}
}

fn foo(ok: bool) -> Result<(), Error> {
if !ok {
return bar::foo(ok).map(|_| Ok::<(), Error>(()))?;
};
Ok(())
}
}

fn issue11982_no_conversion() {
mod bar {
pub struct Error;
pub fn foo(_: bool) -> Result<(), Error> {
Ok(())
}
}

fn foo(ok: bool) -> Result<(), bar::Error> {
if !ok {
return bar::foo(ok).map(|_| Ok::<(), bar::Error>(()))?;
};
Ok(())
}
}

fn general_return() {
fn foo(ok: bool) -> Result<(), ()> {
let bar = Result::Ok(Result::<(), ()>::Ok(()));
if !ok {
return bar?;
};
Ok(())
}
}
50 changes: 50 additions & 0 deletions tests/ui/needless_return_with_question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,53 @@ fn issue11616() -> Result<(), ()> {
};
Ok(())
}

fn issue11982() {
mod bar {
pub struct Error;
pub fn foo(_: bool) -> Result<(), Error> {
Ok(())
}
}

pub struct Error;

impl From<bar::Error> for Error {
fn from(_: bar::Error) -> Self {
Error
}
}

fn foo(ok: bool) -> Result<(), Error> {
if !ok {
return bar::foo(ok).map(|_| Ok::<(), Error>(()))?;
};
Ok(())
}
}

fn issue11982_no_conversion() {
mod bar {
pub struct Error;
pub fn foo(_: bool) -> Result<(), Error> {
Ok(())
}
}

fn foo(ok: bool) -> Result<(), bar::Error> {
if !ok {
return bar::foo(ok).map(|_| Ok::<(), bar::Error>(()))?;
};
Ok(())
}
}

fn general_return() {
fn foo(ok: bool) -> Result<(), ()> {
let bar = Result::Ok(Result::<(), ()>::Ok(()));
if !ok {
return bar?;
};
Ok(())
}
}