Replace Symbol::as_str usage in match expressions

This commit is contained in:
Alex Macleod 2025-05-07 13:52:11 +00:00
parent cf6bebb343
commit 5aac708398
25 changed files with 281 additions and 148 deletions

View file

@ -26,16 +26,16 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
if namespace.is_none()
&& matches!(
name.as_str(),
"ambiguous_glob_reexports"
| "dead_code"
| "deprecated"
| "hidden_glob_reexports"
| "unreachable_pub"
| "unused"
| "unused_braces"
| "unused_import_braces"
| "unused_imports"
name,
sym::ambiguous_glob_reexports
| sym::dead_code
| sym::deprecated
| sym::hidden_glob_reexports
| sym::unreachable_pub
| sym::unused
| sym::unused_braces
| sym::unused_import_braces
| sym::unused_imports
)
{
return;
@ -43,16 +43,16 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
if namespace == Some(sym::clippy)
&& matches!(
name.as_str(),
"wildcard_imports"
| "enum_glob_use"
| "redundant_pub_crate"
| "macro_use_imports"
| "unsafe_removed_from_name"
| "module_name_repetitions"
| "single_component_path_imports"
| "disallowed_types"
| "unused_trait_names"
name,
sym::wildcard_imports
| sym::enum_glob_use
| sym::redundant_pub_crate
| sym::macro_use_imports
| sym::unsafe_removed_from_name
| sym::module_name_repetitions
| sym::single_component_path_imports
| sym::disallowed_types
| sym::unused_trait_names
)
{
return;

View file

@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
use clippy_utils::sugg::Sugg;
use clippy_utils::sym;
use clippy_utils::ty::{implements_trait, is_copy};
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
@ -73,10 +74,9 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
let Some(macro_call) = root_macro_call_first_node(cx, expr) else {
return;
};
let macro_name = cx.tcx.item_name(macro_call.def_id);
let eq_macro = match macro_name.as_str() {
"assert_eq" | "debug_assert_eq" => true,
"assert_ne" | "debug_assert_ne" => false,
let eq_macro = match cx.tcx.get_diagnostic_name(macro_call.def_id) {
Some(sym::assert_eq_macro | sym::debug_assert_eq_macro) => true,
Some(sym::assert_ne_macro | sym::debug_assert_ne_macro) => false,
_ => return,
};
let Some((a, b, _)) = find_assert_eq_args(cx, expr, macro_call.expn) else {
@ -115,6 +115,7 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
return;
}
let macro_name = cx.tcx.item_name(macro_call.def_id);
let macro_name = macro_name.as_str();
let non_eq_mac = &macro_name[..macro_name.len() - 3];
span_lint_and_then(

View file

@ -759,12 +759,12 @@ impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
let recv_ty = cx.typeck_results().expr_ty(receiver);
if recv_ty.is_floating_point() && !is_no_std_crate(cx) && is_inherent_method_call(cx, expr) {
match path.ident.name.as_str() {
"ln" => check_ln1p(cx, expr, receiver),
"log" => check_log_base(cx, expr, receiver, args),
"powf" => check_powf(cx, expr, receiver, args),
"powi" => check_powi(cx, expr, receiver, args),
"sqrt" => check_hypot(cx, expr, receiver),
match path.ident.name {
sym::ln => check_ln1p(cx, expr, receiver),
sym::log => check_log_base(cx, expr, receiver, args),
sym::powf => check_powf(cx, expr, receiver, args),
sym::powi => check_powi(cx, expr, receiver, args),
sym::sqrt => check_hypot(cx, expr, receiver),
_ => {},
}
}

View file

@ -1,6 +1,7 @@
use crate::manual_ignore_case_cmp::MatchType::{Literal, ToAscii};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sym;
use clippy_utils::ty::{get_type_diagnostic_name, is_type_diagnostic_item, is_type_lang_item};
use rustc_ast::LitKind;
use rustc_errors::Applicability;
@ -10,7 +11,7 @@ use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_middle::ty::{Ty, UintTy};
use rustc_session::declare_lint_pass;
use rustc_span::{Span, sym};
use rustc_span::Span;
declare_clippy_lint! {
/// ### What it does
@ -47,9 +48,9 @@ enum MatchType<'a, 'b> {
fn get_ascii_type<'a, 'b>(cx: &LateContext<'a>, kind: rustc_hir::ExprKind<'b>) -> Option<(Span, MatchType<'a, 'b>)> {
if let MethodCall(path, expr, _, _) = kind {
let is_lower = match path.ident.name.as_str() {
"to_ascii_lowercase" => true,
"to_ascii_uppercase" => false,
let is_lower = match path.ident.name {
sym::to_ascii_lowercase => true,
sym::to_ascii_uppercase => false,
_ => return None,
};
let ty_raw = cx.typeck_results().expr_ty(expr);

View file

@ -80,26 +80,26 @@ impl LateLintPass<'_> for ManualOptionAsSlice {
check_map(cx, callee, span, self.msrv);
}
},
ExprKind::MethodCall(seg, callee, [or], _) => match seg.ident.name.as_str() {
"unwrap_or" => {
ExprKind::MethodCall(seg, callee, [or], _) => match seg.ident.name {
sym::unwrap_or => {
if is_empty_slice(cx, or) {
check_map(cx, callee, span, self.msrv);
}
},
"unwrap_or_else" => {
sym::unwrap_or_else => {
if returns_empty_slice(cx, or) {
check_map(cx, callee, span, self.msrv);
}
},
_ => {},
},
ExprKind::MethodCall(seg, callee, [or_else, map], _) => match seg.ident.name.as_str() {
"map_or" => {
ExprKind::MethodCall(seg, callee, [or_else, map], _) => match seg.ident.name {
sym::map_or => {
if is_empty_slice(cx, or_else) && is_slice_from_ref(cx, map) {
check_as_ref(cx, callee, span, self.msrv);
}
},
"map_or_else" => {
sym::map_or_else => {
if returns_empty_slice(cx, or_else) && is_slice_from_ref(cx, map) {
check_as_ref(cx, callee, span, self.msrv);
}

View file

@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::{SpanRangeExt, indent_of, reindent_multiline};
use clippy_utils::sym;
use clippy_utils::ty::is_type_lang_item;
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
@ -21,8 +22,8 @@ pub(super) fn check<'tcx>(
) {
if let ExprKind::MethodCall(path_segment, ..) = recv.kind
&& matches!(
path_segment.ident.name.as_str(),
"to_lowercase" | "to_uppercase" | "to_ascii_lowercase" | "to_ascii_uppercase"
path_segment.ident.name,
sym::to_lowercase | sym::to_uppercase | sym::to_ascii_lowercase | sym::to_ascii_uppercase
)
{
return;

View file

@ -1,13 +1,13 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::get_parent_expr;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet;
use clippy_utils::{get_parent_expr, sym};
use rustc_ast::{LitKind, StrStyle};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, Node, QPath, TyKind};
use rustc_lint::LateContext;
use rustc_span::edition::Edition::Edition2021;
use rustc_span::{Span, Symbol, sym};
use rustc_span::{Span, Symbol};
use super::MANUAL_C_STR_LITERALS;
@ -71,15 +71,15 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args
&& cx.tcx.sess.edition() >= Edition2021
&& msrv.meets(cx, msrvs::C_STR_LITERALS)
{
match fn_name.as_str() {
name @ ("from_bytes_with_nul" | "from_bytes_with_nul_unchecked")
match fn_name {
sym::from_bytes_with_nul | sym::from_bytes_with_nul_unchecked
if !arg.span.from_expansion()
&& let ExprKind::Lit(lit) = arg.kind
&& let LitKind::ByteStr(_, StrStyle::Cooked) | LitKind::Str(_, StrStyle::Cooked) = lit.node =>
{
check_from_bytes(cx, expr, arg, name);
check_from_bytes(cx, expr, arg, fn_name);
},
"from_ptr" => check_from_ptr(cx, expr, arg),
sym::from_ptr => check_from_ptr(cx, expr, arg),
_ => {},
}
}
@ -106,13 +106,13 @@ fn check_from_ptr(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>) {
}
}
/// Checks `CStr::from_bytes_with_nul(b"foo\0")`
fn check_from_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, method: &str) {
fn check_from_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, method: Symbol) {
let (span, applicability) = if let Some(parent) = get_parent_expr(cx, expr)
&& let ExprKind::MethodCall(method, ..) = parent.kind
&& [sym::unwrap, sym::expect].contains(&method.ident.name)
{
(parent.span, Applicability::MachineApplicable)
} else if method == "from_bytes_with_nul_unchecked" {
} else if method == sym::from_bytes_with_nul_unchecked {
// `*_unchecked` returns `&CStr` directly, nothing needs to be changed
(expr.span, Applicability::MachineApplicable)
} else {

View file

@ -357,20 +357,20 @@ impl<'tcx> Visitor<'tcx> for IterFunctionVisitor<'_, 'tcx> {
if let Some(hir_id) = self.current_statement_hir_id {
self.hir_id_uses_map.insert(hir_id, self.uses.len());
}
match method_name.ident.name.as_str() {
"into_iter" => self.uses.push(Some(IterFunction {
match method_name.ident.name {
sym::into_iter => self.uses.push(Some(IterFunction {
func: IterFunctionKind::IntoIter(expr.hir_id),
span: expr.span,
})),
"len" => self.uses.push(Some(IterFunction {
sym::len => self.uses.push(Some(IterFunction {
func: IterFunctionKind::Len,
span: expr.span,
})),
"is_empty" => self.uses.push(Some(IterFunction {
sym::is_empty => self.uses.push(Some(IterFunction {
func: IterFunctionKind::IsEmpty,
span: expr.span,
})),
"contains" => self.uses.push(Some(IterFunction {
sym::contains => self.uses.push(Some(IterFunction {
func: IterFunctionKind::Contains(args[0].span),
span: expr.span,
})),

View file

@ -1,5 +1,6 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
use clippy_utils::sym;
use rustc_hir::intravisit::{Visitor, walk_expr};
use rustc_hir::{BorrowKind, Expr, ExprKind, MatchSource, Mutability};
use rustc_lint::{LateContext, LateLintPass};
@ -42,10 +43,9 @@ impl<'tcx> LateLintPass<'tcx> for DebugAssertWithMutCall {
let Some(macro_call) = root_macro_call_first_node(cx, e) else {
return;
};
let macro_name = cx.tcx.item_name(macro_call.def_id);
if !matches!(
macro_name.as_str(),
"debug_assert" | "debug_assert_eq" | "debug_assert_ne"
cx.tcx.get_diagnostic_name(macro_call.def_id),
Some(sym::debug_assert_macro | sym::debug_assert_eq_macro | sym::debug_assert_ne_macro)
) {
return;
}
@ -60,7 +60,10 @@ impl<'tcx> LateLintPass<'tcx> for DebugAssertWithMutCall {
cx,
DEBUG_ASSERT_WITH_MUT_CALL,
span,
format!("do not call a function with mutable arguments inside of `{macro_name}!`"),
format!(
"do not call a function with mutable arguments inside of `{}!`",
cx.tcx.item_name(macro_call.def_id)
),
);
}
}

View file

@ -1,20 +1,18 @@
use clippy_utils::ast_utils::is_useless_with_eq_exprs;
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace};
use clippy_utils::{eq_expr_value, is_in_test_function};
use clippy_utils::{eq_expr_value, is_in_test_function, sym};
use rustc_hir::{BinOpKind, Expr};
use rustc_lint::LateContext;
use super::EQ_OP;
pub(crate) fn check_assert<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if let Some((macro_call, macro_name)) = first_node_macro_backtrace(cx, e).find_map(|macro_call| {
let name = cx.tcx.item_name(macro_call.def_id);
if let Some(macro_call) = first_node_macro_backtrace(cx, e).find(|macro_call| {
matches!(
name.as_str(),
"assert_eq" | "assert_ne" | "debug_assert_eq" | "debug_assert_ne"
cx.tcx.get_diagnostic_name(macro_call.def_id),
Some(sym::assert_eq_macro | sym::assert_ne_macro | sym::debug_assert_eq_macro | sym::debug_assert_ne_macro)
)
.then(|| (macro_call, name))
}) && let Some((lhs, rhs, _)) = find_assert_eq_args(cx, e, macro_call.expn)
&& eq_expr_value(cx, lhs, rhs)
&& macro_call.is_local()
@ -24,7 +22,10 @@ pub(crate) fn check_assert<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
cx,
EQ_OP,
lhs.span.to(rhs.span),
format!("identical args used in this `{macro_name}!` macro call"),
format!(
"identical args used in this `{}!` macro call",
cx.tcx.item_name(macro_call.def_id)
),
);
}
}

View file

@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::root_macro_call_first_node;
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::visitors::{Descend, for_each_expr};
use clippy_utils::{is_inside_always_const_context, return_ty};
@ -69,10 +69,11 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir
return ControlFlow::Continue(Descend::Yes);
};
if !is_inside_always_const_context(cx.tcx, e.hir_id)
&& matches!(
cx.tcx.item_name(macro_call.def_id).as_str(),
"panic" | "assert" | "assert_eq" | "assert_ne"
)
&& (is_panic(cx, macro_call.def_id)
|| matches!(
cx.tcx.get_diagnostic_name(macro_call.def_id),
Some(sym::assert_macro | sym::assert_eq_macro | sym::assert_ne_macro)
))
{
panics.push(macro_call.span);
ControlFlow::Continue(Descend::No)

View file

@ -113,8 +113,8 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
);
return;
}
match cx.tcx.item_name(macro_call.def_id).as_str() {
"todo" => {
match cx.tcx.get_diagnostic_name(macro_call.def_id) {
Some(sym::todo_macro) => {
span_lint(
cx,
TODO,
@ -122,7 +122,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
"`todo` should not be present in production code",
);
},
"unimplemented" => {
Some(sym::unimplemented_macro) => {
span_lint(
cx,
UNIMPLEMENTED,
@ -130,7 +130,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
"`unimplemented` should not be present in production code",
);
},
"unreachable" => {
Some(sym::unreachable_macro) => {
span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");
},
_ => {},

View file

@ -5,7 +5,7 @@ use clippy_utils::visitors::for_each_expr;
use clippy_utils::{
binary_expr_needs_parentheses, fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor,
leaks_droppable_temporary_with_limited_lifetime, path_res, path_to_local_id, span_contains_cfg,
span_find_starting_semi,
span_find_starting_semi, sym,
};
use core::ops::ControlFlow;
use rustc_ast::MetaItemInner;
@ -22,7 +22,7 @@ use rustc_middle::ty::{self, GenericArgKind, Ty};
use rustc_session::declare_lint_pass;
use rustc_span::def_id::LocalDefId;
use rustc_span::edition::Edition;
use rustc_span::{BytePos, Pos, Span, sym};
use rustc_span::{BytePos, Pos, Span};
use std::borrow::Cow;
use std::fmt::Display;
@ -411,8 +411,8 @@ fn check_final_expr<'tcx>(
&& let [tool, lint_name] = meta_item.path.segments.as_slice()
&& tool.ident.name == sym::clippy
&& matches!(
lint_name.ident.name.as_str(),
"needless_return" | "style" | "all" | "warnings"
lint_name.ident.name,
sym::needless_return | sym::style | sym::all | sym::warnings
)
{
// This is an expectation of the `needless_return` lint

View file

@ -334,7 +334,7 @@ impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
if let ExprKind::MethodCall(path, recv, [], _) = &e.kind
&& path.ident.name == sym::into_bytes
&& let ExprKind::MethodCall(path, recv, [], _) = &recv.kind
&& matches!(path.ident.name.as_str(), "to_owned" | "to_string")
&& matches!(path.ident.name, sym::to_owned | sym::to_string)
&& let ExprKind::Lit(lit) = &recv.kind
&& let LitKind::Str(lit_content, _) = &lit.node
&& lit_content.as_str().is_ascii()

View file

@ -1,5 +1,6 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
use clippy_utils::sym;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::LateContext;
@ -7,11 +8,12 @@ use super::UNIT_CMP;
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
if expr.span.from_expansion() {
if let Some(macro_call) = root_macro_call_first_node(cx, expr) {
let macro_name = cx.tcx.item_name(macro_call.def_id);
let result = match macro_name.as_str() {
"assert_eq" | "debug_assert_eq" => "succeed",
"assert_ne" | "debug_assert_ne" => "fail",
if let Some(macro_call) = root_macro_call_first_node(cx, expr)
&& let Some(diag_name) = cx.tcx.get_diagnostic_name(macro_call.def_id)
{
let result = match diag_name {
sym::assert_eq_macro | sym::debug_assert_eq_macro => "succeed",
sym::assert_ne_macro | sym::debug_assert_ne_macro => "fail",
_ => return,
};
let Some((left, _, _)) = find_assert_eq_args(cx, expr, macro_call.expn) else {
@ -24,7 +26,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
cx,
UNIT_CMP,
macro_call.span,
format!("`{macro_name}` of unit values detected. This will always {result}"),
format!(
"`{}` of unit values detected. This will always {result}",
cx.tcx.item_name(macro_call.def_id)
),
);
}
return;

View file

@ -226,7 +226,7 @@ fn is_unreachable_or_panic(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
if is_panic(cx, macro_call.def_id) {
return !cx.tcx.hir_is_inside_const_context(expr.hir_id);
}
matches!(cx.tcx.item_name(macro_call.def_id).as_str(), "unreachable")
cx.tcx.is_diagnostic_item(sym::unreachable_macro, macro_call.def_id)
}
fn unpack_call_chain<'a>(mut expr: &'a hir::Expr<'a>) -> &'a hir::Expr<'a> {

View file

@ -498,9 +498,9 @@ fn check_literal(cx: &LateContext<'_>, format_args: &FormatArgs, name: &str) {
None => return,
},
LitKind::Char => (
match lit.symbol.as_str() {
"\"" => "\\\"",
"\\'" => "'",
match lit.symbol {
sym::DOUBLE_QUOTE => "\\\"",
sym::BACKSLASH_SINGLE_QUOTE => "'",
_ => match value_string.strip_prefix('\'').and_then(|s| s.strip_suffix('\'')) {
Some(stripped) => stripped,
None => return,