Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
use clippy_utils::{
eq_expr_value, higher, is_else_clause, is_in_const_context, is_lint_allowed, is_path_lang_item, is_res_lang_ctor,
pat_and_expr_can_be_question_mark, path_res, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
span_contains_cfg, span_contains_comment, sym,
eq_expr_value, fn_def_id_with_node_args, higher, is_else_clause, is_in_const_context, is_lint_allowed,
is_path_lang_item, is_res_lang_ctor, pat_and_expr_can_be_question_mark, path_res, path_to_local, path_to_local_id,
peel_blocks, peel_blocks_with_stmt, span_contains_cfg, span_contains_comment, sym,
};
use rustc_errors::Applicability;
use rustc_hir::LangItem::{self, OptionNone, OptionSome, ResultErr, ResultOk};
Expand Down Expand Up @@ -393,8 +393,8 @@ fn check_arm_is_none_or_err<'tcx>(cx: &LateContext<'tcx>, mode: TryMode, arm: &A
&& let ExprKind::Ret(Some(wrapped_ret_expr)) = arm_body.kind
&& let ExprKind::Call(ok_ctor, [ret_expr]) = wrapped_ret_expr.kind
&& is_res_lang_ctor(cx, path_res(cx, ok_ctor), ResultErr)
// check `...` is `val` from binding
&& path_to_local_id(ret_expr, ok_val)
// check if `...` is `val` from binding or `val.into()`
&& is_local_or_local_into(cx, ret_expr, ok_val)
{
true
} else {
Expand All @@ -417,6 +417,17 @@ fn check_arm_is_none_or_err<'tcx>(cx: &LateContext<'tcx>, mode: TryMode, arm: &A
}
}

/// Check if `expr` is `val` or `val.into()`
fn is_local_or_local_into(cx: &LateContext<'_>, expr: &Expr<'_>, val: HirId) -> bool {
let is_into_call = fn_def_id_with_node_args(cx, expr)
.and_then(|(fn_def_id, _)| cx.tcx.trait_of_assoc(fn_def_id))
.is_some_and(|trait_def_id| cx.tcx.is_diagnostic_item(sym::Into, trait_def_id));
match expr.kind {
ExprKind::MethodCall(_, recv, [], _) | ExprKind::Call(_, [recv]) => is_into_call && path_to_local_id(recv, val),
_ => path_to_local_id(expr, val),
}
}

fn check_arms_are_try<'tcx>(cx: &LateContext<'tcx>, mode: TryMode, arm1: &Arm<'tcx>, arm2: &Arm<'tcx>) -> bool {
(check_arm_is_some_or_ok(cx, mode, arm1) && check_arm_is_none_or_err(cx, mode, arm2))
|| (check_arm_is_some_or_ok(cx, mode, arm2) && check_arm_is_none_or_err(cx, mode, arm1))
Expand Down
14 changes: 12 additions & 2 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![feature(try_blocks)]
#![allow(unreachable_code)]
#![allow(dead_code)]
#![allow(clippy::unnecessary_wraps)]

use std::sync::MutexGuard;
Expand Down Expand Up @@ -465,3 +463,15 @@ fn issue_13642(x: Option<i32>) -> Option<()> {

None
}

fn issue_15679() -> Result<i32, String> {
let some_result: Result<i32, &'static str> = todo!();

some_result?;

some_result?;

some_result?;

Ok(0)
}
26 changes: 24 additions & 2 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![feature(try_blocks)]
#![allow(unreachable_code)]
#![allow(dead_code)]
#![allow(clippy::unnecessary_wraps)]

use std::sync::MutexGuard;
Expand Down Expand Up @@ -561,3 +559,27 @@ fn issue_13642(x: Option<i32>) -> Option<()> {

None
}

fn issue_15679() -> Result<i32, String> {
let some_result: Result<i32, &'static str> = todo!();

match some_result {
//~^ question_mark
Ok(val) => val,
Err(err) => return Err(err.into()),
};

match some_result {
//~^ question_mark
Ok(val) => val,
Err(err) => return Err(Into::into(err)),
};

match some_result {
//~^ question_mark
Ok(val) => val,
Err(err) => return Err(<&str as Into<String>>::into(err)),
};

Ok(0)
}
Loading