1- use clippy_utils::diagnostics::span_lint_and_sugg;
21use clippy_utils::source::snippet_with_applicability;
32use clippy_utils::ty::is_type_diagnostic_item;
4- use if_chain::if_chain ;
3+ use clippy_utils::{diagnostics::span_lint_and_sugg, is_lang_ctor} ;
54use rustc_errors::Applicability;
6- use rustc_hir::{Expr, ExprKind, QPath };
5+ use rustc_hir::{lang_items::LangItem, Expr, ExprKind };
76use rustc_lint::LateContext;
87use rustc_span::{sym, Span};
98
@@ -22,14 +21,14 @@ pub(super) fn check<'tcx>(
2221
2322 if is_type_diagnostic_item(cx, ty, sym::Option) {
2423 title = "found `.or(Some(…)).unwrap()`";
25- if let Some(content) = get_content_if_is( or_arg, "Some" ) {
24+ if let Some(content) = get_content_if_ctor_matches(cx, or_arg, LangItem::OptionSome ) {
2625 or_arg_content = content;
2726 } else {
2827 return;
2928 }
3029 } else if is_type_diagnostic_item(cx, ty, sym::Result) {
3130 title = "found `.or(Ok(…)).unwrap()`";
32- if let Some(content) = get_content_if_is( or_arg, "Ok" ) {
31+ if let Some(content) = get_content_if_ctor_matches(cx, or_arg, LangItem::ResultOk ) {
3332 or_arg_content = content;
3433 } else {
3534 return;
@@ -64,19 +63,13 @@ pub(super) fn check<'tcx>(
6463 );
6564}
6665
67- /// is expr a Call to name? if so, return what it's wrapping
68- /// name might be "Some", "Ok", "Err", etc.
69- fn get_content_if_is<'a>(expr: &Expr<'a>, name: &str) -> Option<Span> {
70- if_chain! {
71- if let ExprKind::Call(some_expr, [arg]) = expr.kind;
72- if let ExprKind::Path(QPath::Resolved(_, path)) = &some_expr.kind;
73- if let Some(path_segment) = path.segments.first();
74- if path_segment.ident.name.as_str() == name;
75- then {
76- Some(arg.span)
77- }
78- else {
79- None
80- }
66+ fn get_content_if_ctor_matches(cx: &LateContext<'_>, expr: &Expr<'_>, item: LangItem) -> Option<Span> {
67+ if let ExprKind::Call(some_expr, [arg]) = expr.kind
68+ && let ExprKind::Path(qpath) = &some_expr.kind
69+ && is_lang_ctor(cx, qpath, item)
70+ {
71+ Some(arg.span)
72+ } else {
73+ None
8174 }
8275}
0 commit comments