Skip to content

Use interned strings when possible, for efficiency purposes #14963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/attrs/deprecated_cfg_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(super) fn check_clippy(cx: &EarlyContext<'_>, attr: &Attribute) {

fn check_deprecated_cfg_recursively(cx: &EarlyContext<'_>, attr: &rustc_ast::MetaItem) {
if let Some(ident) = attr.ident() {
if ["any", "all", "not"].contains(&ident.name.as_str()) {
if matches!(ident.name, sym::any | sym::all | sym::not) {
let Some(list) = attr.meta_item_list() else { return };
for item in list.iter().filter_map(|item| item.meta_item()) {
check_deprecated_cfg_recursively(cx, item);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/casts/cast_ptr_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
};
match parent.kind {
ExprKind::MethodCall(name, self_arg, ..) if self_arg.hir_id == e.hir_id => {
if matches!(name.ident.as_str(), "read_unaligned" | "write_unaligned")
if matches!(name.ident.name, sym::read_unaligned | sym::write_unaligned)
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id)
&& let Some(def_id) = cx.tcx.impl_of_method(def_id)
&& cx.tcx.type_of(def_id).instantiate_identity().is_raw_ptr()
Expand Down
5 changes: 2 additions & 3 deletions clippy_lints/src/if_let_mutex.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::visitors::for_each_expr_without_closures;
use clippy_utils::{eq_expr_value, higher};
use clippy_utils::{eq_expr_value, higher, sym};
use core::ops::ControlFlow;
use rustc_errors::Diag;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
use rustc_span::edition::Edition::Edition2024;
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -94,7 +93,7 @@ fn mutex_lock_call<'tcx>(
op_mutex: Option<&'tcx Expr<'_>>,
) -> ControlFlow<&'tcx Expr<'tcx>> {
if let ExprKind::MethodCall(path, self_arg, [], _) = &expr.kind
&& path.ident.as_str() == "lock"
&& path.ident.name == sym::lock
&& let ty = cx.typeck_results().expr_ty(self_arg).peel_refs()
&& is_type_diagnostic_item(cx, ty, sym::Mutex)
&& op_mutex.is_none_or(|op| eq_expr_value(cx, self_arg, op))
Expand Down
12 changes: 6 additions & 6 deletions clippy_lints/src/implicit_saturating_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::sugg::{Sugg, make_binop};
use clippy_utils::{
SpanlessEq, eq_expr_value, higher, is_in_const_context, is_integer_literal, peel_blocks, peel_blocks_with_stmt,
SpanlessEq, eq_expr_value, higher, is_in_const_context, is_integer_literal, peel_blocks, peel_blocks_with_stmt, sym,
};
use rustc_ast::ast::LitKind;
use rustc_data_structures::packed::Pu128;
use rustc_errors::Applicability;
use rustc_hir::{AssignOpKind, BinOp, BinOpKind, Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::impl_lint_pass;
use rustc_span::Span;
use rustc_span::{Span, Symbol};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -325,7 +325,7 @@ fn check_with_condition<'tcx>(
}

// Get the variable name
let var_name = ares_path.segments[0].ident.name.as_str();
let var_name = ares_path.segments[0].ident.name;
match cond_num_val.kind {
ExprKind::Lit(cond_lit) => {
// Check if the constant is zero
Expand All @@ -337,7 +337,7 @@ fn check_with_condition<'tcx>(
}
},
ExprKind::Path(QPath::TypeRelative(_, name)) => {
if name.ident.as_str() == "MIN"
if name.ident.name == sym::MIN
&& let Some(const_id) = cx.typeck_results().type_dependent_def_id(cond_num_val.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(const_id)
&& let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
Expand All @@ -348,7 +348,7 @@ fn check_with_condition<'tcx>(
},
ExprKind::Call(func, []) => {
if let ExprKind::Path(QPath::TypeRelative(_, name)) = func.kind
&& name.ident.as_str() == "min_value"
&& name.ident.name == sym::min_value
&& let Some(func_id) = cx.typeck_results().type_dependent_def_id(func.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(func_id)
&& let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
Expand Down Expand Up @@ -383,7 +383,7 @@ fn subtracts_one<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a Exp
}
}

fn print_lint_and_sugg(cx: &LateContext<'_>, var_name: &str, expr: &Expr<'_>) {
fn print_lint_and_sugg(cx: &LateContext<'_>, var_name: Symbol, expr: &Expr<'_>) {
span_lint_and_sugg(
cx,
IMPLICIT_SATURATING_SUB,
Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/manual_clamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use clippy_utils::ty::implements_trait;
use clippy_utils::visitors::is_const_evaluatable;
use clippy_utils::{
MaybePath, eq_expr_value, is_diag_trait_item, is_in_const_context, is_trait_method, path_res, path_to_local_id,
peel_blocks, peel_blocks_with_stmt,
peel_blocks, peel_blocks_with_stmt, sym,
};
use itertools::Itertools;
use rustc_errors::{Applicability, Diag};
Expand All @@ -18,7 +18,6 @@ use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::Ty;
use rustc_session::impl_lint_pass;
use rustc_span::Span;
use rustc_span::symbol::sym;
use std::cmp::Ordering;
use std::ops::Deref;

Expand Down Expand Up @@ -299,9 +298,9 @@ fn is_max_min_pattern<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> O
&& (cx.typeck_results().expr_ty_adjusted(input).is_floating_point() || is_trait_method(cx, receiver, sym::Ord))
{
let is_float = cx.typeck_results().expr_ty_adjusted(input).is_floating_point();
let (min, max) = match (seg_first.ident.as_str(), seg_second.ident.as_str()) {
("min", "max") => (arg_second, arg_first),
("max", "min") => (arg_first, arg_second),
let (min, max) = match (seg_first.ident.name, seg_second.ident.name) {
(sym::min, sym::max) => (arg_second, arg_first),
(sym::max, sym::min) => (arg_first, arg_second),
_ => return None,
};
Some(ClampSuggestion {
Expand Down
8 changes: 5 additions & 3 deletions clippy_lints/src/manual_string_new.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::sym;
use rustc_ast::LitKind;
use rustc_errors::Applicability::MachineApplicable;
use rustc_hir::{Expr, ExprKind, PathSegment, QPath, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::declare_lint_pass;
use rustc_span::{Span, sym};
use rustc_span::Span;

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

/// Tries to parse an expression as a method call, emitting the warning if necessary.
fn parse_method_call(cx: &LateContext<'_>, span: Span, path_segment: &PathSegment<'_>, receiver: &Expr<'_>) {
let ident = path_segment.ident.as_str();
let method_arg_kind = &receiver.kind;
if ["to_string", "to_owned", "into"].contains(&ident) && is_expr_kind_empty_str(method_arg_kind) {
if matches!(path_segment.ident.name, sym::to_string | sym::to_owned | sym::into)
&& is_expr_kind_empty_str(method_arg_kind)
{
warn_then_suggest(cx, span);
} else if let ExprKind::Call(func, [arg]) = method_arg_kind {
// If our first argument is a function call itself, it could be an `unwrap`-like function.
Expand Down
5 changes: 2 additions & 3 deletions clippy_lints/src/match_result_ok.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_context;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{higher, is_res_lang_ctor};
use clippy_utils::{higher, is_res_lang_ctor, sym};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, LangItem, PatKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -57,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for MatchResultOk {

if let ExprKind::MethodCall(ok_path, recv, [], ..) = let_expr.kind //check is expr.ok() has type Result<T,E>.ok(, _)
&& let PatKind::TupleStruct(ref pat_path, [ok_pat], _) = let_pat.kind //get operation
&& ok_path.ident.as_str() == "ok"
&& ok_path.ident.name == sym::ok
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result)
&& is_res_lang_ctor(cx, cx.qpath_res(pat_path, let_pat.hir_id), LangItem::OptionSome)
&& let ctxt = expr.span.ctxt()
Expand Down
17 changes: 9 additions & 8 deletions clippy_lints/src/matches/match_str_case_mismatch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::ops::ControlFlow;

use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::sym;
use clippy_utils::ty::is_type_lang_item;
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -42,7 +43,7 @@ impl<'tcx> Visitor<'tcx> for MatchExprVisitor<'_, 'tcx> {
type Result = ControlFlow<CaseMethod>;
fn visit_expr(&mut self, ex: &'tcx Expr<'_>) -> Self::Result {
if let ExprKind::MethodCall(segment, receiver, [], _) = ex.kind {
let result = self.case_altered(segment.ident.as_str(), receiver);
let result = self.case_altered(segment.ident.name, receiver);
if result.is_break() {
return result;
}
Expand All @@ -53,7 +54,7 @@ impl<'tcx> Visitor<'tcx> for MatchExprVisitor<'_, 'tcx> {
}

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

Expand All @@ -66,12 +67,12 @@ impl MatchExprVisitor<'_, '_> {
}
}

fn get_case_method(segment_ident_str: &str) -> Option<CaseMethod> {
match segment_ident_str {
"to_lowercase" => Some(CaseMethod::LowerCase),
"to_ascii_lowercase" => Some(CaseMethod::AsciiLowerCase),
"to_uppercase" => Some(CaseMethod::UpperCase),
"to_ascii_uppercase" => Some(CaseMethod::AsciiUppercase),
fn get_case_method(segment_ident: Symbol) -> Option<CaseMethod> {
match segment_ident {
sym::to_lowercase => Some(CaseMethod::LowerCase),
sym::to_ascii_lowercase => Some(CaseMethod::AsciiLowerCase),
sym::to_uppercase => Some(CaseMethod::UpperCase),
sym::to_ascii_uppercase => Some(CaseMethod::AsciiUppercase),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/iter_kv_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(super) fn check<'tcx>(

if let ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = body_expr.kind
&& let [local_ident] = path.segments
&& local_ident.ident.as_str() == bound_ident.as_str()
&& local_ident.ident.name == bound_ident.name
{
span_lint_and_sugg(
cx,
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/methods/manual_saturating_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ fn is_min_or_max(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<MinMax> {
if let hir::ExprKind::Call(func, []) = &expr.kind
&& let hir::ExprKind::Path(hir::QPath::TypeRelative(_, segment)) = &func.kind
{
match segment.ident.as_str() {
"max_value" => return Some(MinMax::Max),
"min_value" => return Some(MinMax::Min),
match segment.ident.name {
sym::max_value => return Some(MinMax::Max),
sym::min_value => return Some(MinMax::Min),
_ => {},
}
}
Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/methods/needless_collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ pub(super) fn check<'tcx>(

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

let sugg: String = match name {
"len" => {
let sugg: String = match name.ident.name {
sym::len => {
if let Some(adt) = collect_ty.ty_adt_def()
&& matches!(
cx.tcx.get_diagnostic_name(adt.did()),
Expand All @@ -60,13 +59,13 @@ pub(super) fn check<'tcx>(
return;
}
},
"is_empty"
sym::is_empty
if is_is_empty_sig(cx, parent.hir_id)
&& iterates_same_ty(cx, cx.typeck_results().expr_ty(iter_expr), collect_ty) =>
{
"next().is_none()".into()
},
"contains" => {
sym::contains => {
if is_contains_sig(cx, parent.hir_id, iter_expr)
&& let Some(arg) = args.first()
{
Expand Down
18 changes: 9 additions & 9 deletions clippy_lints/src/methods/open_options.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use rustc_data_structures::fx::FxHashMap;

use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::paths;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{paths, sym};
use rustc_ast::ast::LitKind;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext;
use rustc_middle::ty::Ty;
use rustc_span::Span;
use rustc_span::source_map::Spanned;
use rustc_span::{Span, sym};

use super::{NONSENSICAL_OPEN_OPTIONS, SUSPICIOUS_OPEN_OPTIONS};

Expand Down Expand Up @@ -87,23 +87,23 @@ fn get_open_options(
_ => Argument::Unknown,
};

match path.ident.as_str() {
"create" => {
match path.ident.name {
sym::create => {
options.push((OpenOption::Create, argument_option, span));
},
"create_new" => {
sym::create_new => {
options.push((OpenOption::CreateNew, argument_option, span));
},
"append" => {
sym::append => {
options.push((OpenOption::Append, argument_option, span));
},
"truncate" => {
sym::truncate => {
options.push((OpenOption::Truncate, argument_option, span));
},
"read" => {
sym::read => {
options.push((OpenOption::Read, argument_option, span));
},
"write" => {
sym::write => {
options.push((OpenOption::Write, argument_option, span));
},
_ => {
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/methods/str_splitn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ fn parse_iter_usage<'tcx>(
let did = cx.typeck_results().type_dependent_def_id(e.hir_id)?;
let iter_id = cx.tcx.get_diagnostic_item(sym::Iterator)?;

match (name.ident.as_str(), args) {
("next", []) if cx.tcx.trait_of_item(did) == Some(iter_id) => (IterUsageKind::Nth(0), e.span),
("next_tuple", []) => {
match (name.ident.name, args) {
(sym::next, []) if cx.tcx.trait_of_item(did) == Some(iter_id) => (IterUsageKind::Nth(0), e.span),
(sym::next_tuple, []) => {
return if paths::ITERTOOLS_NEXT_TUPLE.matches(cx, did)
&& let ty::Adt(adt_def, subs) = cx.typeck_results().expr_ty(e).kind()
&& cx.tcx.is_diagnostic_item(sym::Option, adt_def.did())
Expand All @@ -303,7 +303,7 @@ fn parse_iter_usage<'tcx>(
None
};
},
("nth" | "skip", [idx_expr]) if cx.tcx.trait_of_item(did) == Some(iter_id) => {
(sym::nth | sym::skip, [idx_expr]) if cx.tcx.trait_of_item(did) == Some(iter_id) => {
if let Some(Constant::Int(idx)) = ConstEvalCtxt::new(cx).eval(idx_expr) {
let span = if name.ident.as_str() == "nth" {
e.span
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/multiple_bound_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl EarlyLintPass for MultipleBoundLocations {

for param in &generics.params {
if !param.bounds.is_empty() {
generic_params_with_bounds.insert(param.ident.name.as_str(), param.ident.span);
generic_params_with_bounds.insert(param.ident.as_str(), param.ident.span);
}
}
for clause in &generics.where_clause.predicates {
Expand All @@ -64,7 +64,7 @@ impl EarlyLintPass for MultipleBoundLocations {
},
WherePredicateKind::RegionPredicate(pred) => {
if !pred.bounds.is_empty()
&& let Some(bound_span) = generic_params_with_bounds.get(&pred.lifetime.ident.name.as_str())
&& let Some(bound_span) = generic_params_with_bounds.get(&pred.lifetime.ident.as_str())
{
emit_lint(cx, *bound_span, pred.lifetime.ident.span);
}
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/pathbuf_init_then_push.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::path_to_local_id;
use clippy_utils::source::{SpanRangeExt, snippet};
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{path_to_local_id, sym};
use rustc_ast::{LitKind, StrStyle};
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::{BindingMode, Block, Expr, ExprKind, HirId, LetStmt, PatKind, QPath, Stmt, StmtKind, TyKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::impl_lint_pass;
use rustc_span::{Span, Symbol, sym};
use rustc_span::{Span, Symbol};

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