Skip to content

Commit 11b19dc

Browse files
committed
Cleanup by limiting lookups
1 parent abe5b1c commit 11b19dc

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

clippy_lints/src/unwrap.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
2-
use clippy_utils::ty::is_type_diagnostic_item;
2+
use clippy_utils::ty::get_type_diagnostic_name;
33
use clippy_utils::usage::is_potentially_local_place;
44
use clippy_utils::{higher, path_to_local, sym};
55
use rustc_errors::Applicability;
@@ -133,12 +133,14 @@ fn collect_unwrap_info<'tcx>(
133133
invert: bool,
134134
is_entire_condition: bool,
135135
) -> Vec<UnwrapInfo<'tcx>> {
136-
fn is_relevant_option_call(cx: &LateContext<'_>, ty: Ty<'_>, method_name: Symbol) -> bool {
137-
is_type_diagnostic_item(cx, ty, sym::Option) && matches!(method_name, sym::is_none | sym::is_some)
138-
}
139-
140-
fn is_relevant_result_call(cx: &LateContext<'_>, ty: Ty<'_>, method_name: Symbol) -> bool {
141-
is_type_diagnostic_item(cx, ty, sym::Result) && matches!(method_name, sym::is_err | sym::is_ok)
136+
fn option_or_result_call(cx: &LateContext<'_>, ty: Ty<'_>, method_name: Symbol) -> Option<(UnwrappableKind, bool)> {
137+
match (get_type_diagnostic_name(cx, ty)?, method_name) {
138+
(sym::Option, sym::is_some) => Some((UnwrappableKind::Option, true)),
139+
(sym::Option, sym::is_none) => Some((UnwrappableKind::Option, false)),
140+
(sym::Result, sym::is_ok) => Some((UnwrappableKind::Result, true)),
141+
(sym::Result, sym::is_err) => Some((UnwrappableKind::Result, false)),
142+
_ => None,
143+
}
142144
}
143145

144146
match expr.kind {
@@ -157,15 +159,9 @@ fn collect_unwrap_info<'tcx>(
157159
if let Some(local_id) = path_to_local(receiver)
158160
&& let ty = cx.typeck_results().expr_ty(receiver)
159161
&& let name = method_name.ident.name
160-
&& (is_relevant_option_call(cx, ty, name) || is_relevant_result_call(cx, ty, name)) =>
162+
&& let Some((kind, unwrappable)) = option_or_result_call(cx, ty, name) =>
161163
{
162-
let unwrappable = matches!(name, sym::is_some | sym::is_ok);
163164
let safe_to_unwrap = unwrappable != invert;
164-
let kind = if is_type_diagnostic_item(cx, ty, sym::Option) {
165-
UnwrappableKind::Option
166-
} else {
167-
UnwrappableKind::Result
168-
};
169165

170166
vec![UnwrapInfo {
171167
local_id,

0 commit comments

Comments
 (0)