@@ -4,15 +4,15 @@ use clippy_utils::usage::is_potentially_local_place;
4
4
use clippy_utils::{higher, path_to_local, sym};
5
5
use rustc_errors::Applicability;
6
6
use rustc_hir::intravisit::{FnKind, Visitor, walk_expr, walk_fn};
7
- use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Node, PathSegment, UnOp};
7
+ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, Node, UnOp};
8
8
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceWithHirId};
9
9
use rustc_lint::{LateContext, LateLintPass};
10
10
use rustc_middle::hir::nested_filter;
11
11
use rustc_middle::mir::FakeReadCause;
12
12
use rustc_middle::ty::{self, Ty, TyCtxt};
13
13
use rustc_session::declare_lint_pass;
14
- use rustc_span::Span;
15
14
use rustc_span::def_id::LocalDefId;
15
+ use rustc_span::{Span, Symbol};
16
16
17
17
declare_clippy_lint! {
18
18
/// ### What it does
@@ -111,7 +111,7 @@ struct UnwrapInfo<'tcx> {
111
111
/// The check, like `x.is_ok()`
112
112
check: &'tcx Expr<'tcx>,
113
113
/// The check's name, like `is_ok`
114
- check_name: &'tcx PathSegment<'tcx> ,
114
+ check_name: Symbol ,
115
115
/// The branch where the check takes place, like `if x.is_ok() { .. }`
116
116
branch: &'tcx Expr<'tcx>,
117
117
/// Whether `is_some()` or `is_ok()` was called (as opposed to `is_err()` or `is_none()`).
@@ -133,12 +133,12 @@ fn collect_unwrap_info<'tcx>(
133
133
invert: bool,
134
134
is_entire_condition: bool,
135
135
) -> Vec<UnwrapInfo<'tcx>> {
136
- fn is_relevant_option_call(cx: &LateContext<'_>, ty: Ty<'_>, method_name: &str ) -> bool {
137
- is_type_diagnostic_item(cx, ty, sym::Option) && ["is_some", " is_none"].contains(&method_name )
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
138
}
139
139
140
- fn is_relevant_result_call(cx: &LateContext<'_>, ty: Ty<'_>, method_name: &str ) -> bool {
141
- is_type_diagnostic_item(cx, ty, sym::Result) && ["is_ok", " is_err"].contains(&method_name )
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 )
142
142
}
143
143
144
144
if let ExprKind::Binary(op, left, right) = &expr.kind {
@@ -155,14 +155,10 @@ fn collect_unwrap_info<'tcx>(
155
155
} else if let ExprKind::MethodCall(method_name, receiver, [], _) = &expr.kind
156
156
&& let Some(local_id) = path_to_local(receiver)
157
157
&& let ty = cx.typeck_results().expr_ty(receiver)
158
- && let name = method_name.ident.as_str()
158
+ && let name = method_name.ident.name
159
159
&& (is_relevant_option_call(cx, ty, name) || is_relevant_result_call(cx, ty, name))
160
160
{
161
- let unwrappable = match name {
162
- "is_some" | "is_ok" => true,
163
- "is_err" | "is_none" => false,
164
- _ => unreachable!(),
165
- };
161
+ let unwrappable = matches!(name, sym::is_some | sym::is_ok);
166
162
let safe_to_unwrap = unwrappable != invert;
167
163
let kind = if is_type_diagnostic_item(cx, ty, sym::Option) {
168
164
UnwrappableKind::Option
@@ -174,7 +170,7 @@ fn collect_unwrap_info<'tcx>(
174
170
local_id,
175
171
if_expr,
176
172
check: expr,
177
- check_name: method_name ,
173
+ check_name: name ,
178
174
branch,
179
175
safe_to_unwrap,
180
176
kind,
@@ -332,8 +328,7 @@ impl<'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'_, 'tcx> {
332
328
expr.span,
333
329
format!(
334
330
"called `{}` on `{unwrappable_variable_name}` after checking its variant with `{}`",
335
- method_name.ident.name,
336
- unwrappable.check_name.ident.as_str(),
331
+ method_name.ident.name, unwrappable.check_name,
337
332
),
338
333
|diag| {
339
334
if is_entire_condition {
0 commit comments