2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
|
|
|
use clippy_utils::{method_chain_args, return_ty};
|
2020-08-28 14:10:16 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_hir as hir;
|
2022-01-15 22:07:52 +00:00
|
|
|
use rustc_hir::intravisit::{self, Visitor};
|
2021-03-25 18:29:11 +00:00
|
|
|
use rustc_hir::{Expr, ImplItemKind};
|
2020-08-28 14:10:16 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-11-05 13:29:48 +00:00
|
|
|
use rustc_span::{sym, Span};
|
2020-08-28 14:10:16 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
2021-11-04 12:52:36 +00:00
|
|
|
/// Checks for functions of type `Result` that contain `expect()` or `unwrap()`
|
2020-08-28 14:10:16 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// These functions promote recoverable errors to non-recoverable errors which may be undesirable in code bases which wish to avoid panics.
|
2020-08-28 14:10:16 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Known problems
|
|
|
|
/// This can cause false positives in functions that handle both recoverable and non recoverable errors.
|
2020-08-28 14:10:16 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2020-08-28 14:10:16 +00:00
|
|
|
/// Before:
|
|
|
|
/// ```rust
|
|
|
|
/// fn divisible_by_3(i_str: String) -> Result<(), String> {
|
|
|
|
/// let i = i_str
|
|
|
|
/// .parse::<i32>()
|
|
|
|
/// .expect("cannot divide the input by three");
|
|
|
|
///
|
|
|
|
/// if i % 3 != 0 {
|
|
|
|
/// Err("Number is not divisible by 3")?
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// After:
|
|
|
|
/// ```rust
|
|
|
|
/// fn divisible_by_3(i_str: String) -> Result<(), String> {
|
|
|
|
/// let i = i_str
|
|
|
|
/// .parse::<i32>()
|
|
|
|
/// .map_err(|e| format!("cannot divide the input by three: {}", e))?;
|
|
|
|
///
|
|
|
|
/// if i % 3 != 0 {
|
|
|
|
/// Err("Number is not divisible by 3")?
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.48.0"]
|
2020-08-28 14:10:16 +00:00
|
|
|
pub UNWRAP_IN_RESULT,
|
|
|
|
restriction,
|
|
|
|
"functions of type `Result<..>` or `Option`<...> that contain `expect()` or `unwrap()`"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(UnwrapInResult=> [UNWRAP_IN_RESULT]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for UnwrapInResult {
|
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
|
|
|
|
if_chain! {
|
|
|
|
// first check if it's a method or function
|
|
|
|
if let hir::ImplItemKind::Fn(ref _signature, _) = impl_item.kind;
|
|
|
|
// checking if its return type is `result` or `option`
|
2021-10-02 23:51:01 +00:00
|
|
|
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::Result)
|
|
|
|
|| is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::Option);
|
2020-08-28 14:10:16 +00:00
|
|
|
then {
|
|
|
|
lint_impl_body(cx, impl_item.span, impl_item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FindExpectUnwrap<'a, 'tcx> {
|
|
|
|
lcx: &'a LateContext<'tcx>,
|
|
|
|
typeck_results: &'tcx ty::TypeckResults<'tcx>,
|
|
|
|
result: Vec<Span>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for FindExpectUnwrap<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
|
|
|
// check for `expect`
|
|
|
|
if let Some(arglists) = method_chain_args(expr, &["expect"]) {
|
2022-09-09 11:36:26 +00:00
|
|
|
let receiver_ty = self.typeck_results.expr_ty(arglists[0].0).peel_refs();
|
2022-03-16 12:12:30 +00:00
|
|
|
if is_type_diagnostic_item(self.lcx, receiver_ty, sym::Option)
|
|
|
|
|| is_type_diagnostic_item(self.lcx, receiver_ty, sym::Result)
|
2020-08-28 14:10:16 +00:00
|
|
|
{
|
|
|
|
self.result.push(expr.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for `unwrap`
|
|
|
|
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
|
2022-09-09 11:36:26 +00:00
|
|
|
let receiver_ty = self.typeck_results.expr_ty(arglists[0].0).peel_refs();
|
2022-03-16 12:12:30 +00:00
|
|
|
if is_type_diagnostic_item(self.lcx, receiver_ty, sym::Option)
|
|
|
|
|| is_type_diagnostic_item(self.lcx, receiver_ty, sym::Result)
|
2020-08-28 14:10:16 +00:00
|
|
|
{
|
|
|
|
self.result.push(expr.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// and check sub-expressions
|
|
|
|
intravisit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_item: &'tcx hir::ImplItem<'_>) {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let ImplItemKind::Fn(_, body_id) = impl_item.kind {
|
|
|
|
let body = cx.tcx.hir().body(body_id);
|
|
|
|
let mut fpu = FindExpectUnwrap {
|
|
|
|
lcx: cx,
|
2022-09-20 05:11:23 +00:00
|
|
|
typeck_results: cx.tcx.typeck(impl_item.def_id.def_id),
|
2021-04-08 15:50:13 +00:00
|
|
|
result: Vec::new(),
|
|
|
|
};
|
2022-09-09 11:36:26 +00:00
|
|
|
fpu.visit_expr(body.value);
|
2020-08-28 14:10:16 +00:00
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
// if we've found one, lint
|
|
|
|
if !fpu.result.is_empty() {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
UNWRAP_IN_RESULT,
|
|
|
|
impl_span,
|
|
|
|
"used unwrap or expect in a function that returns result or option",
|
|
|
|
move |diag| {
|
|
|
|
diag.help("unwrap and expect should not be used in a function that returns result or option");
|
|
|
|
diag.span_note(fpu.result, "potential non-recoverable error(s)");
|
|
|
|
},
|
|
|
|
);
|
2020-08-28 14:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|