2024-08-08 12:13:50 -05:00
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2022-07-28 12:08:22 -05:00
|
|
|
|
use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
|
|
|
|
|
use clippy_utils::source::snippet_with_context;
|
2022-09-21 12:02:37 -05:00
|
|
|
|
use clippy_utils::ty::{has_debug_impl, is_copy, is_type_diagnostic_item};
|
2022-07-28 12:08:22 -05:00
|
|
|
|
use clippy_utils::usage::local_used_after_expr;
|
2022-09-21 12:02:37 -05:00
|
|
|
|
use clippy_utils::{is_expr_final_block_expr, path_res};
|
2022-07-28 12:08:22 -05:00
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
|
use rustc_hir::def::Res;
|
|
|
|
|
use rustc_hir::{Expr, ExprKind};
|
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
|
use rustc_middle::ty::{self, Ty};
|
2023-12-01 11:21:58 -06:00
|
|
|
|
use rustc_session::declare_lint_pass;
|
2022-07-28 12:08:22 -05:00
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
|
/// ### What it does
|
|
|
|
|
/// Checks for `assert!(r.is_ok())` or `assert!(r.is_err())` calls.
|
|
|
|
|
///
|
2024-05-30 03:49:05 -05:00
|
|
|
|
/// ### Why restrict this?
|
|
|
|
|
/// This form of assertion does not show any of the information present in the `Result`
|
|
|
|
|
/// other than which variant it isn’t.
|
2022-07-28 12:08:22 -05:00
|
|
|
|
///
|
2022-08-01 00:22:16 -05:00
|
|
|
|
/// ### Known problems
|
|
|
|
|
/// The suggested replacement decreases the readability of code and log output.
|
|
|
|
|
///
|
2022-07-28 12:08:22 -05:00
|
|
|
|
/// ### Example
|
2024-05-30 03:49:05 -05:00
|
|
|
|
/// ```rust,no_run
|
2022-07-28 12:08:22 -05:00
|
|
|
|
/// # let r = Ok::<_, ()>(());
|
|
|
|
|
/// assert!(r.is_ok());
|
2024-05-30 03:49:05 -05:00
|
|
|
|
/// # let r = Err::<(), _>(());
|
2022-07-28 12:08:22 -05:00
|
|
|
|
/// assert!(r.is_err());
|
|
|
|
|
/// ```
|
2024-05-30 03:49:05 -05:00
|
|
|
|
///
|
|
|
|
|
/// Use instead:
|
|
|
|
|
///
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
/// # let r = Ok::<_, ()>(());
|
|
|
|
|
/// r.unwrap();
|
|
|
|
|
/// # let r = Err::<(), _>(());
|
|
|
|
|
/// r.unwrap_err();
|
|
|
|
|
/// ```
|
2022-07-28 12:08:22 -05:00
|
|
|
|
#[clippy::version = "1.64.0"]
|
|
|
|
|
pub ASSERTIONS_ON_RESULT_STATES,
|
2022-08-01 00:22:16 -05:00
|
|
|
|
restriction,
|
2024-05-30 03:49:05 -05:00
|
|
|
|
"`assert!(r.is_ok())` or `assert!(r.is_err())` gives worse panic messages than directly calling `r.unwrap()` or `r.unwrap_err()`"
|
2022-07-28 12:08:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
declare_lint_pass!(AssertionsOnResultStates => [ASSERTIONS_ON_RESULT_STATES]);
|
|
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
|
|
|
|
if let Some(macro_call) = root_macro_call_first_node(cx, e)
|
|
|
|
|
&& matches!(cx.tcx.get_diagnostic_name(macro_call.def_id), Some(sym::assert_macro))
|
|
|
|
|
&& let Some((condition, panic_expn)) = find_assert_args(cx, e, macro_call.expn)
|
|
|
|
|
&& matches!(panic_expn, PanicExpn::Empty)
|
2022-09-01 04:43:35 -05:00
|
|
|
|
&& let ExprKind::MethodCall(method_segment, recv, [], _) = condition.kind
|
2022-07-28 12:08:22 -05:00
|
|
|
|
&& let result_type_with_refs = cx.typeck_results().expr_ty(recv)
|
|
|
|
|
&& let result_type = result_type_with_refs.peel_refs()
|
|
|
|
|
&& is_type_diagnostic_item(cx, result_type, sym::Result)
|
2023-07-11 16:35:29 -05:00
|
|
|
|
&& let ty::Adt(_, args) = result_type.kind()
|
2022-07-28 12:08:22 -05:00
|
|
|
|
{
|
|
|
|
|
if !is_copy(cx, result_type) {
|
|
|
|
|
if result_type_with_refs != result_type {
|
|
|
|
|
return;
|
|
|
|
|
} else if let Res::Local(binding_id) = path_res(cx, recv)
|
2022-08-11 12:42:16 -05:00
|
|
|
|
&& local_used_after_expr(cx, binding_id, recv)
|
|
|
|
|
{
|
2022-07-28 12:08:22 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-08 12:13:50 -05:00
|
|
|
|
let (message, replacement) = match method_segment.ident.as_str() {
|
2023-07-11 16:35:29 -05:00
|
|
|
|
"is_ok" if type_suitable_to_unwrap(cx, args.type_at(1)) => {
|
2024-08-08 12:13:50 -05:00
|
|
|
|
("called `assert!` with `Result::is_ok`", "unwrap")
|
2023-11-02 11:35:56 -05:00
|
|
|
|
},
|
2023-07-11 16:35:29 -05:00
|
|
|
|
"is_err" if type_suitable_to_unwrap(cx, args.type_at(0)) => {
|
2024-08-08 12:13:50 -05:00
|
|
|
|
("called `assert!` with `Result::is_err`", "unwrap_err")
|
2023-11-02 11:35:56 -05:00
|
|
|
|
},
|
2024-08-08 12:13:50 -05:00
|
|
|
|
_ => return,
|
2022-07-28 12:08:22 -05:00
|
|
|
|
};
|
2024-08-08 12:13:50 -05:00
|
|
|
|
span_lint_and_then(cx, ASSERTIONS_ON_RESULT_STATES, macro_call.span, message, |diag| {
|
|
|
|
|
let semicolon = if is_expr_final_block_expr(cx.tcx, e) { ";" } else { "" };
|
|
|
|
|
let mut app = Applicability::MachineApplicable;
|
|
|
|
|
diag.span_suggestion(
|
|
|
|
|
macro_call.span,
|
|
|
|
|
"replace with",
|
|
|
|
|
format!(
|
|
|
|
|
"{}.{replacement}(){semicolon}",
|
|
|
|
|
snippet_with_context(cx, recv.span, condition.span.ctxt(), "..", &mut app).0
|
|
|
|
|
),
|
|
|
|
|
app,
|
|
|
|
|
);
|
|
|
|
|
});
|
2022-07-28 12:08:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-11 12:42:16 -05:00
|
|
|
|
fn type_suitable_to_unwrap<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
|
has_debug_impl(cx, ty) && !ty.is_unit() && !ty.is_never()
|
|
|
|
|
}
|