Skip to content

Commit 668a292

Browse files
committed
unwrap.rs: replace some strings usage by interned symbols
1 parent e04158c commit 668a292

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

clippy_lints/src/unwrap.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use clippy_utils::usage::is_potentially_local_place;
44
use clippy_utils::{higher, path_to_local, sym};
55
use rustc_errors::Applicability;
66
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};
88
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceWithHirId};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::hir::nested_filter;
1111
use rustc_middle::mir::FakeReadCause;
1212
use rustc_middle::ty::{self, Ty, TyCtxt};
1313
use rustc_session::declare_lint_pass;
14-
use rustc_span::Span;
1514
use rustc_span::def_id::LocalDefId;
15+
use rustc_span::{Span, Symbol};
1616

1717
declare_clippy_lint! {
1818
/// ### What it does
@@ -111,7 +111,7 @@ struct UnwrapInfo<'tcx> {
111111
/// The check, like `x.is_ok()`
112112
check: &'tcx Expr<'tcx>,
113113
/// The check's name, like `is_ok`
114-
check_name: &'tcx PathSegment<'tcx>,
114+
check_name: Symbol,
115115
/// The branch where the check takes place, like `if x.is_ok() { .. }`
116116
branch: &'tcx Expr<'tcx>,
117117
/// 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>(
133133
invert: bool,
134134
is_entire_condition: bool,
135135
) -> 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)
138138
}
139139

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)
142142
}
143143

144144
if let ExprKind::Binary(op, left, right) = &expr.kind {
@@ -155,14 +155,10 @@ fn collect_unwrap_info<'tcx>(
155155
} else if let ExprKind::MethodCall(method_name, receiver, [], _) = &expr.kind
156156
&& let Some(local_id) = path_to_local(receiver)
157157
&& let ty = cx.typeck_results().expr_ty(receiver)
158-
&& let name = method_name.ident.as_str()
158+
&& let name = method_name.ident.name
159159
&& (is_relevant_option_call(cx, ty, name) || is_relevant_result_call(cx, ty, name))
160160
{
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);
166162
let safe_to_unwrap = unwrappable != invert;
167163
let kind = if is_type_diagnostic_item(cx, ty, sym::Option) {
168164
UnwrappableKind::Option
@@ -174,7 +170,7 @@ fn collect_unwrap_info<'tcx>(
174170
local_id,
175171
if_expr,
176172
check: expr,
177-
check_name: method_name,
173+
check_name: name,
178174
branch,
179175
safe_to_unwrap,
180176
kind,
@@ -332,8 +328,7 @@ impl<'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'_, 'tcx> {
332328
expr.span,
333329
format!(
334330
"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,
337332
),
338333
|diag| {
339334
if is_entire_condition {

0 commit comments

Comments
 (0)