-use crate::utils::{is_type_diagnostic_item, method_chain_args, return_ty, span_lint_and_then, walk_ptrs_ty};
+use clippy_utils::diagnostics::span_lint_and_then;
+use clippy_utils::ty::is_type_diagnostic_item;
+use clippy_utils::{method_chain_args, return_ty};
use if_chain::if_chain;
use rustc_hir as hir;
+use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
+use rustc_hir::{Expr, ImplItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::map::Map;
use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
-use rustc_span::Span;
+use rustc_span::{sym, Span};
declare_clippy_lint! {
/// **What it does:** Checks for functions of type Result that contain `expect()` or `unwrap()`
// 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`
- if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym!(result_type))
- || is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym!(option_type));
+ if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::result_type)
+ || is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::option_type);
then {
lint_impl_body(cx, impl_item.span, impl_item);
}
}
}
-use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
-use rustc_hir::{Expr, ImplItemKind};
-
struct FindExpectUnwrap<'a, 'tcx> {
lcx: &'a LateContext<'tcx>,
typeck_results: &'tcx ty::TypeckResults<'tcx>,
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
// check for `expect`
if let Some(arglists) = method_chain_args(expr, &["expect"]) {
- let reciever_ty = walk_ptrs_ty(self.typeck_results.expr_ty(&arglists[0][0]));
- if is_type_diagnostic_item(self.lcx, reciever_ty, sym!(option_type))
- || is_type_diagnostic_item(self.lcx, reciever_ty, sym!(result_type))
+ let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
+ if is_type_diagnostic_item(self.lcx, reciever_ty, sym::option_type)
+ || is_type_diagnostic_item(self.lcx, reciever_ty, sym::result_type)
{
self.result.push(expr.span);
}
// check for `unwrap`
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
- let reciever_ty = walk_ptrs_ty(self.typeck_results.expr_ty(&arglists[0][0]));
- if is_type_diagnostic_item(self.lcx, reciever_ty, sym!(option_type))
- || is_type_diagnostic_item(self.lcx, reciever_ty, sym!(result_type))
+ let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
+ if is_type_diagnostic_item(self.lcx, reciever_ty, sym::option_type)
+ || is_type_diagnostic_item(self.lcx, reciever_ty, sym::result_type)
{
self.result.push(expr.span);
}
if let ImplItemKind::Fn(_, body_id) = impl_item.kind;
then {
let body = cx.tcx.hir().body(body_id);
- let impl_item_def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
let mut fpu = FindExpectUnwrap {
lcx: cx,
- typeck_results: cx.tcx.typeck(impl_item_def_id),
+ typeck_results: cx.tcx.typeck(impl_item.def_id),
result: Vec::new(),
};
fpu.visit_expr(&body.value);