Skip to content

Commit 84ef7fb

Browse files
authored
Use interned strings when possible, for efficiency purposes (#14963)
Also, a number of unnecessary `.as_str()` calls have been removed for clarity. changelog: none
2 parents a4debd2 + 2a5a2fa commit 84ef7fb

28 files changed

+116
-94
lines changed

clippy_lints/src/attrs/deprecated_cfg_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(super) fn check_clippy(cx: &EarlyContext<'_>, attr: &Attribute) {
6161

6262
fn check_deprecated_cfg_recursively(cx: &EarlyContext<'_>, attr: &rustc_ast::MetaItem) {
6363
if let Some(ident) = attr.ident() {
64-
if ["any", "all", "not"].contains(&ident.name.as_str()) {
64+
if matches!(ident.name, sym::any | sym::all | sym::not) {
6565
let Some(list) = attr.meta_item_list() else { return };
6666
for item in list.iter().filter_map(|item| item.meta_item()) {
6767
check_deprecated_cfg_recursively(cx, item);

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
6161
};
6262
match parent.kind {
6363
ExprKind::MethodCall(name, self_arg, ..) if self_arg.hir_id == e.hir_id => {
64-
if matches!(name.ident.as_str(), "read_unaligned" | "write_unaligned")
64+
if matches!(name.ident.name, sym::read_unaligned | sym::write_unaligned)
6565
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id)
6666
&& let Some(def_id) = cx.tcx.impl_of_method(def_id)
6767
&& cx.tcx.type_of(def_id).instantiate_identity().is_raw_ptr()

clippy_lints/src/if_let_mutex.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_diagnostic_item;
33
use clippy_utils::visitors::for_each_expr_without_closures;
4-
use clippy_utils::{eq_expr_value, higher};
4+
use clippy_utils::{eq_expr_value, higher, sym};
55
use core::ops::ControlFlow;
66
use rustc_errors::Diag;
77
use rustc_hir::{Expr, ExprKind};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_session::declare_lint_pass;
1010
use rustc_span::edition::Edition::Edition2024;
11-
use rustc_span::sym;
1211

1312
declare_clippy_lint! {
1413
/// ### What it does
@@ -94,7 +93,7 @@ fn mutex_lock_call<'tcx>(
9493
op_mutex: Option<&'tcx Expr<'_>>,
9594
) -> ControlFlow<&'tcx Expr<'tcx>> {
9695
if let ExprKind::MethodCall(path, self_arg, [], _) = &expr.kind
97-
&& path.ident.as_str() == "lock"
96+
&& path.ident.name == sym::lock
9897
&& let ty = cx.typeck_results().expr_ty(self_arg).peel_refs()
9998
&& is_type_diagnostic_item(cx, ty, sym::Mutex)
10099
&& op_mutex.is_none_or(|op| eq_expr_value(cx, self_arg, op))

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::sugg::{Sugg, make_binop};
55
use clippy_utils::{
6-
SpanlessEq, eq_expr_value, higher, is_in_const_context, is_integer_literal, peel_blocks, peel_blocks_with_stmt,
6+
SpanlessEq, eq_expr_value, higher, is_in_const_context, is_integer_literal, peel_blocks, peel_blocks_with_stmt, sym,
77
};
88
use rustc_ast::ast::LitKind;
99
use rustc_data_structures::packed::Pu128;
1010
use rustc_errors::Applicability;
1111
use rustc_hir::{AssignOpKind, BinOp, BinOpKind, Expr, ExprKind, QPath};
1212
use rustc_lint::{LateContext, LateLintPass};
1313
use rustc_session::impl_lint_pass;
14-
use rustc_span::Span;
14+
use rustc_span::{Span, Symbol};
1515

1616
declare_clippy_lint! {
1717
/// ### What it does
@@ -325,7 +325,7 @@ fn check_with_condition<'tcx>(
325325
}
326326

327327
// Get the variable name
328-
let var_name = ares_path.segments[0].ident.name.as_str();
328+
let var_name = ares_path.segments[0].ident.name;
329329
match cond_num_val.kind {
330330
ExprKind::Lit(cond_lit) => {
331331
// Check if the constant is zero
@@ -337,7 +337,7 @@ fn check_with_condition<'tcx>(
337337
}
338338
},
339339
ExprKind::Path(QPath::TypeRelative(_, name)) => {
340-
if name.ident.as_str() == "MIN"
340+
if name.ident.name == sym::MIN
341341
&& let Some(const_id) = cx.typeck_results().type_dependent_def_id(cond_num_val.hir_id)
342342
&& let Some(impl_id) = cx.tcx.impl_of_method(const_id)
343343
&& let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
@@ -348,7 +348,7 @@ fn check_with_condition<'tcx>(
348348
},
349349
ExprKind::Call(func, []) => {
350350
if let ExprKind::Path(QPath::TypeRelative(_, name)) = func.kind
351-
&& name.ident.as_str() == "min_value"
351+
&& name.ident.name == sym::min_value
352352
&& let Some(func_id) = cx.typeck_results().type_dependent_def_id(func.hir_id)
353353
&& let Some(impl_id) = cx.tcx.impl_of_method(func_id)
354354
&& let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
@@ -383,7 +383,7 @@ fn subtracts_one<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a Exp
383383
}
384384
}
385385

386-
fn print_lint_and_sugg(cx: &LateContext<'_>, var_name: &str, expr: &Expr<'_>) {
386+
fn print_lint_and_sugg(cx: &LateContext<'_>, var_name: Symbol, expr: &Expr<'_>) {
387387
span_lint_and_sugg(
388388
cx,
389389
IMPLICIT_SATURATING_SUB,

clippy_lints/src/manual_clamp.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clippy_utils::ty::implements_trait;
88
use clippy_utils::visitors::is_const_evaluatable;
99
use clippy_utils::{
1010
MaybePath, eq_expr_value, is_diag_trait_item, is_in_const_context, is_trait_method, path_res, path_to_local_id,
11-
peel_blocks, peel_blocks_with_stmt,
11+
peel_blocks, peel_blocks_with_stmt, sym,
1212
};
1313
use itertools::Itertools;
1414
use rustc_errors::{Applicability, Diag};
@@ -18,7 +18,6 @@ use rustc_lint::{LateContext, LateLintPass};
1818
use rustc_middle::ty::Ty;
1919
use rustc_session::impl_lint_pass;
2020
use rustc_span::Span;
21-
use rustc_span::symbol::sym;
2221
use std::cmp::Ordering;
2322
use std::ops::Deref;
2423

@@ -299,9 +298,9 @@ fn is_max_min_pattern<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> O
299298
&& (cx.typeck_results().expr_ty_adjusted(input).is_floating_point() || is_trait_method(cx, receiver, sym::Ord))
300299
{
301300
let is_float = cx.typeck_results().expr_ty_adjusted(input).is_floating_point();
302-
let (min, max) = match (seg_first.ident.as_str(), seg_second.ident.as_str()) {
303-
("min", "max") => (arg_second, arg_first),
304-
("max", "min") => (arg_first, arg_second),
301+
let (min, max) = match (seg_first.ident.name, seg_second.ident.name) {
302+
(sym::min, sym::max) => (arg_second, arg_first),
303+
(sym::max, sym::min) => (arg_first, arg_second),
305304
_ => return None,
306305
};
307306
Some(ClampSuggestion {

clippy_lints/src/manual_string_new.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::sym;
23
use rustc_ast::LitKind;
34
use rustc_errors::Applicability::MachineApplicable;
45
use rustc_hir::{Expr, ExprKind, PathSegment, QPath, TyKind};
56
use rustc_lint::{LateContext, LateLintPass};
67
use rustc_middle::ty;
78
use rustc_session::declare_lint_pass;
8-
use rustc_span::{Span, sym};
9+
use rustc_span::Span;
910

1011
declare_clippy_lint! {
1112
/// ### What it does
@@ -89,9 +90,10 @@ fn warn_then_suggest(cx: &LateContext<'_>, span: Span) {
8990

9091
/// Tries to parse an expression as a method call, emitting the warning if necessary.
9192
fn parse_method_call(cx: &LateContext<'_>, span: Span, path_segment: &PathSegment<'_>, receiver: &Expr<'_>) {
92-
let ident = path_segment.ident.as_str();
9393
let method_arg_kind = &receiver.kind;
94-
if ["to_string", "to_owned", "into"].contains(&ident) && is_expr_kind_empty_str(method_arg_kind) {
94+
if matches!(path_segment.ident.name, sym::to_string | sym::to_owned | sym::into)
95+
&& is_expr_kind_empty_str(method_arg_kind)
96+
{
9597
warn_then_suggest(cx, span);
9698
} else if let ExprKind::Call(func, [arg]) = method_arg_kind {
9799
// If our first argument is a function call itself, it could be an `unwrap`-like function.

clippy_lints/src/match_result_ok.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_context;
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{higher, is_res_lang_ctor};
4+
use clippy_utils::{higher, is_res_lang_ctor, sym};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, LangItem, PatKind};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::declare_lint_pass;
9-
use rustc_span::sym;
109

1110
declare_clippy_lint! {
1211
/// ### What it does
@@ -57,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {
5756

5857
if let ExprKind::MethodCall(ok_path, recv, [], ..) = let_expr.kind //check is expr.ok() has type Result<T,E>.ok(, _)
5958
&& let PatKind::TupleStruct(ref pat_path, [ok_pat], _) = let_pat.kind //get operation
60-
&& ok_path.ident.as_str() == "ok"
59+
&& ok_path.ident.name == sym::ok
6160
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result)
6261
&& is_res_lang_ctor(cx, cx.qpath_res(pat_path, let_pat.hir_id), LangItem::OptionSome)
6362
&& let ctxt = expr.span.ctxt()

clippy_lints/src/matches/match_str_case_mismatch.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::ops::ControlFlow;
22

33
use clippy_utils::diagnostics::span_lint_and_sugg;
4+
use clippy_utils::sym;
45
use clippy_utils::ty::is_type_lang_item;
56
use rustc_ast::ast::LitKind;
67
use rustc_errors::Applicability;
@@ -42,7 +43,7 @@ impl<'tcx> Visitor<'tcx> for MatchExprVisitor<'_, 'tcx> {
4243
type Result = ControlFlow<CaseMethod>;
4344
fn visit_expr(&mut self, ex: &'tcx Expr<'_>) -> Self::Result {
4445
if let ExprKind::MethodCall(segment, receiver, [], _) = ex.kind {
45-
let result = self.case_altered(segment.ident.as_str(), receiver);
46+
let result = self.case_altered(segment.ident.name, receiver);
4647
if result.is_break() {
4748
return result;
4849
}
@@ -53,7 +54,7 @@ impl<'tcx> Visitor<'tcx> for MatchExprVisitor<'_, 'tcx> {
5354
}
5455

5556
impl MatchExprVisitor<'_, '_> {
56-
fn case_altered(&mut self, segment_ident: &str, receiver: &Expr<'_>) -> ControlFlow<CaseMethod> {
57+
fn case_altered(&mut self, segment_ident: Symbol, receiver: &Expr<'_>) -> ControlFlow<CaseMethod> {
5758
if let Some(case_method) = get_case_method(segment_ident) {
5859
let ty = self.cx.typeck_results().expr_ty(receiver).peel_refs();
5960

@@ -66,12 +67,12 @@ impl MatchExprVisitor<'_, '_> {
6667
}
6768
}
6869

69-
fn get_case_method(segment_ident_str: &str) -> Option<CaseMethod> {
70-
match segment_ident_str {
71-
"to_lowercase" => Some(CaseMethod::LowerCase),
72-
"to_ascii_lowercase" => Some(CaseMethod::AsciiLowerCase),
73-
"to_uppercase" => Some(CaseMethod::UpperCase),
74-
"to_ascii_uppercase" => Some(CaseMethod::AsciiUppercase),
70+
fn get_case_method(segment_ident: Symbol) -> Option<CaseMethod> {
71+
match segment_ident {
72+
sym::to_lowercase => Some(CaseMethod::LowerCase),
73+
sym::to_ascii_lowercase => Some(CaseMethod::AsciiLowerCase),
74+
sym::to_uppercase => Some(CaseMethod::UpperCase),
75+
sym::to_ascii_uppercase => Some(CaseMethod::AsciiUppercase),
7576
_ => None,
7677
}
7778
}

clippy_lints/src/methods/iter_kv_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(super) fn check<'tcx>(
4646

4747
if let ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = body_expr.kind
4848
&& let [local_ident] = path.segments
49-
&& local_ident.ident.as_str() == bound_ident.as_str()
49+
&& local_ident.ident.name == bound_ident.name
5050
{
5151
span_lint_and_sugg(
5252
cx,

clippy_lints/src/methods/manual_saturating_arithmetic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ fn is_min_or_max(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<MinMax> {
7272
if let hir::ExprKind::Call(func, []) = &expr.kind
7373
&& let hir::ExprKind::Path(hir::QPath::TypeRelative(_, segment)) = &func.kind
7474
{
75-
match segment.ident.as_str() {
76-
"max_value" => return Some(MinMax::Max),
77-
"min_value" => return Some(MinMax::Min),
75+
match segment.ident.name {
76+
sym::max_value => return Some(MinMax::Max),
77+
sym::min_value => return Some(MinMax::Min),
7878
_ => {},
7979
}
8080
}

clippy_lints/src/methods/needless_collect.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ pub(super) fn check<'tcx>(
4444

4545
if let ExprKind::MethodCall(name, _, args @ ([] | [_]), _) = parent.kind {
4646
let mut app = Applicability::MachineApplicable;
47-
let name = name.ident.as_str();
4847
let collect_ty = cx.typeck_results().expr_ty(collect_expr);
4948

50-
let sugg: String = match name {
51-
"len" => {
49+
let sugg: String = match name.ident.name {
50+
sym::len => {
5251
if let Some(adt) = collect_ty.ty_adt_def()
5352
&& matches!(
5453
cx.tcx.get_diagnostic_name(adt.did()),
@@ -60,13 +59,13 @@ pub(super) fn check<'tcx>(
6059
return;
6160
}
6261
},
63-
"is_empty"
62+
sym::is_empty
6463
if is_is_empty_sig(cx, parent.hir_id)
6564
&& iterates_same_ty(cx, cx.typeck_results().expr_ty(iter_expr), collect_ty) =>
6665
{
6766
"next().is_none()".into()
6867
},
69-
"contains" => {
68+
sym::contains => {
7069
if is_contains_sig(cx, parent.hir_id, iter_expr)
7170
&& let Some(arg) = args.first()
7271
{

clippy_lints/src/methods/open_options.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use rustc_data_structures::fx::FxHashMap;
22

33
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
4-
use clippy_utils::paths;
54
use clippy_utils::ty::is_type_diagnostic_item;
5+
use clippy_utils::{paths, sym};
66
use rustc_ast::ast::LitKind;
77
use rustc_hir::{Expr, ExprKind};
88
use rustc_lint::LateContext;
99
use rustc_middle::ty::Ty;
10+
use rustc_span::Span;
1011
use rustc_span::source_map::Spanned;
11-
use rustc_span::{Span, sym};
1212

1313
use super::{NONSENSICAL_OPEN_OPTIONS, SUSPICIOUS_OPEN_OPTIONS};
1414

@@ -87,23 +87,23 @@ fn get_open_options(
8787
_ => Argument::Unknown,
8888
};
8989

90-
match path.ident.as_str() {
91-
"create" => {
90+
match path.ident.name {
91+
sym::create => {
9292
options.push((OpenOption::Create, argument_option, span));
9393
},
94-
"create_new" => {
94+
sym::create_new => {
9595
options.push((OpenOption::CreateNew, argument_option, span));
9696
},
97-
"append" => {
97+
sym::append => {
9898
options.push((OpenOption::Append, argument_option, span));
9999
},
100-
"truncate" => {
100+
sym::truncate => {
101101
options.push((OpenOption::Truncate, argument_option, span));
102102
},
103-
"read" => {
103+
sym::read => {
104104
options.push((OpenOption::Read, argument_option, span));
105105
},
106-
"write" => {
106+
sym::write => {
107107
options.push((OpenOption::Write, argument_option, span));
108108
},
109109
_ => {

clippy_lints/src/methods/str_splitn.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ fn parse_iter_usage<'tcx>(
285285
let did = cx.typeck_results().type_dependent_def_id(e.hir_id)?;
286286
let iter_id = cx.tcx.get_diagnostic_item(sym::Iterator)?;
287287

288-
match (name.ident.as_str(), args) {
289-
("next", []) if cx.tcx.trait_of_item(did) == Some(iter_id) => (IterUsageKind::Nth(0), e.span),
290-
("next_tuple", []) => {
288+
match (name.ident.name, args) {
289+
(sym::next, []) if cx.tcx.trait_of_item(did) == Some(iter_id) => (IterUsageKind::Nth(0), e.span),
290+
(sym::next_tuple, []) => {
291291
return if paths::ITERTOOLS_NEXT_TUPLE.matches(cx, did)
292292
&& let ty::Adt(adt_def, subs) = cx.typeck_results().expr_ty(e).kind()
293293
&& cx.tcx.is_diagnostic_item(sym::Option, adt_def.did())
@@ -303,7 +303,7 @@ fn parse_iter_usage<'tcx>(
303303
None
304304
};
305305
},
306-
("nth" | "skip", [idx_expr]) if cx.tcx.trait_of_item(did) == Some(iter_id) => {
306+
(sym::nth | sym::skip, [idx_expr]) if cx.tcx.trait_of_item(did) == Some(iter_id) => {
307307
if let Some(Constant::Int(idx)) = ConstEvalCtxt::new(cx).eval(idx_expr) {
308308
let span = if name.ident.as_str() == "nth" {
309309
e.span

clippy_lints/src/multiple_bound_locations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl EarlyLintPass for MultipleBoundLocations {
4747

4848
for param in &generics.params {
4949
if !param.bounds.is_empty() {
50-
generic_params_with_bounds.insert(param.ident.name.as_str(), param.ident.span);
50+
generic_params_with_bounds.insert(param.ident.as_str(), param.ident.span);
5151
}
5252
}
5353
for clause in &generics.where_clause.predicates {
@@ -64,7 +64,7 @@ impl EarlyLintPass for MultipleBoundLocations {
6464
},
6565
WherePredicateKind::RegionPredicate(pred) => {
6666
if !pred.bounds.is_empty()
67-
&& let Some(bound_span) = generic_params_with_bounds.get(&pred.lifetime.ident.name.as_str())
67+
&& let Some(bound_span) = generic_params_with_bounds.get(&pred.lifetime.ident.as_str())
6868
{
6969
emit_lint(cx, *bound_span, pred.lifetime.ident.span);
7070
}

clippy_lints/src/pathbuf_init_then_push.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::path_to_local_id;
32
use clippy_utils::source::{SpanRangeExt, snippet};
43
use clippy_utils::ty::is_type_diagnostic_item;
4+
use clippy_utils::{path_to_local_id, sym};
55
use rustc_ast::{LitKind, StrStyle};
66
use rustc_errors::Applicability;
77
use rustc_hir::def::Res;
88
use rustc_hir::{BindingMode, Block, Expr, ExprKind, HirId, LetStmt, PatKind, QPath, Stmt, StmtKind, TyKind};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
1010
use rustc_session::impl_lint_pass;
11-
use rustc_span::{Span, Symbol, sym};
11+
use rustc_span::{Span, Symbol};
1212

1313
declare_clippy_lint! {
1414
/// ### What it does
@@ -177,7 +177,7 @@ impl<'tcx> LateLintPass<'tcx> for PathbufThenPush<'tcx> {
177177
&& let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind
178178
&& let ExprKind::MethodCall(name, self_arg, [arg_expr], _) = expr.kind
179179
&& path_to_local_id(self_arg, searcher.local_id)
180-
&& name.ident.as_str() == "push"
180+
&& name.ident.name == sym::push
181181
{
182182
searcher.err_span = searcher.err_span.to(stmt.span);
183183
searcher.arg = Some(*arg_expr);

0 commit comments

Comments
 (0)