Skip to content

Commit ca62816

Browse files
authored
Replace Symbol::as_str usage in match expressions (rust-lang#14745)
We could do with a way to make matching on the `assert_*` macro kind easier, this came up a bunch here. I'll take a look at that as a follow up changelog: none
2 parents ca78fb4 + 5aac708 commit ca62816

25 files changed

+281
-148
lines changed

clippy_lints/src/attrs/useless_attribute.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,33 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
2626

2727
if namespace.is_none()
2828
&& matches!(
29-
name.as_str(),
30-
"ambiguous_glob_reexports"
31-
| "dead_code"
32-
| "deprecated"
33-
| "hidden_glob_reexports"
34-
| "unreachable_pub"
35-
| "unused"
36-
| "unused_braces"
37-
| "unused_import_braces"
38-
| "unused_imports"
29+
name,
30+
sym::ambiguous_glob_reexports
31+
| sym::dead_code
32+
| sym::deprecated
33+
| sym::hidden_glob_reexports
34+
| sym::unreachable_pub
35+
| sym::unused
36+
| sym::unused_braces
37+
| sym::unused_import_braces
38+
| sym::unused_imports
3939
)
4040
{
4141
return;
4242
}
4343

4444
if namespace == Some(sym::clippy)
4545
&& matches!(
46-
name.as_str(),
47-
"wildcard_imports"
48-
| "enum_glob_use"
49-
| "redundant_pub_crate"
50-
| "macro_use_imports"
51-
| "unsafe_removed_from_name"
52-
| "module_name_repetitions"
53-
| "single_component_path_imports"
54-
| "disallowed_types"
55-
| "unused_trait_names"
46+
name,
47+
sym::wildcard_imports
48+
| sym::enum_glob_use
49+
| sym::redundant_pub_crate
50+
| sym::macro_use_imports
51+
| sym::unsafe_removed_from_name
52+
| sym::module_name_repetitions
53+
| sym::single_component_path_imports
54+
| sym::disallowed_types
55+
| sym::unused_trait_names
5656
)
5757
{
5858
return;

clippy_lints/src/bool_assert_comparison.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
33
use clippy_utils::sugg::Sugg;
4+
use clippy_utils::sym;
45
use clippy_utils::ty::{implements_trait, is_copy};
56
use rustc_ast::ast::LitKind;
67
use rustc_errors::Applicability;
@@ -73,10 +74,9 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
7374
let Some(macro_call) = root_macro_call_first_node(cx, expr) else {
7475
return;
7576
};
76-
let macro_name = cx.tcx.item_name(macro_call.def_id);
77-
let eq_macro = match macro_name.as_str() {
78-
"assert_eq" | "debug_assert_eq" => true,
79-
"assert_ne" | "debug_assert_ne" => false,
77+
let eq_macro = match cx.tcx.get_diagnostic_name(macro_call.def_id) {
78+
Some(sym::assert_eq_macro | sym::debug_assert_eq_macro) => true,
79+
Some(sym::assert_ne_macro | sym::debug_assert_ne_macro) => false,
8080
_ => return,
8181
};
8282
let Some((a, b, _)) = find_assert_eq_args(cx, expr, macro_call.expn) else {
@@ -115,6 +115,7 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
115115
return;
116116
}
117117

118+
let macro_name = cx.tcx.item_name(macro_call.def_id);
118119
let macro_name = macro_name.as_str();
119120
let non_eq_mac = &macro_name[..macro_name.len() - 3];
120121
span_lint_and_then(

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -759,12 +759,12 @@ impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
759759
let recv_ty = cx.typeck_results().expr_ty(receiver);
760760

761761
if recv_ty.is_floating_point() && !is_no_std_crate(cx) && is_inherent_method_call(cx, expr) {
762-
match path.ident.name.as_str() {
763-
"ln" => check_ln1p(cx, expr, receiver),
764-
"log" => check_log_base(cx, expr, receiver, args),
765-
"powf" => check_powf(cx, expr, receiver, args),
766-
"powi" => check_powi(cx, expr, receiver, args),
767-
"sqrt" => check_hypot(cx, expr, receiver),
762+
match path.ident.name {
763+
sym::ln => check_ln1p(cx, expr, receiver),
764+
sym::log => check_log_base(cx, expr, receiver, args),
765+
sym::powf => check_powf(cx, expr, receiver, args),
766+
sym::powi => check_powi(cx, expr, receiver, args),
767+
sym::sqrt => check_hypot(cx, expr, receiver),
768768
_ => {},
769769
}
770770
}

clippy_lints/src/manual_ignore_case_cmp.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::manual_ignore_case_cmp::MatchType::{Literal, ToAscii};
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::source::snippet_with_applicability;
4+
use clippy_utils::sym;
45
use clippy_utils::ty::{get_type_diagnostic_name, is_type_diagnostic_item, is_type_lang_item};
56
use rustc_ast::LitKind;
67
use rustc_errors::Applicability;
@@ -10,7 +11,7 @@ use rustc_lint::{LateContext, LateLintPass};
1011
use rustc_middle::ty;
1112
use rustc_middle::ty::{Ty, UintTy};
1213
use rustc_session::declare_lint_pass;
13-
use rustc_span::{Span, sym};
14+
use rustc_span::Span;
1415

1516
declare_clippy_lint! {
1617
/// ### What it does
@@ -47,9 +48,9 @@ enum MatchType<'a, 'b> {
4748

4849
fn get_ascii_type<'a, 'b>(cx: &LateContext<'a>, kind: rustc_hir::ExprKind<'b>) -> Option<(Span, MatchType<'a, 'b>)> {
4950
if let MethodCall(path, expr, _, _) = kind {
50-
let is_lower = match path.ident.name.as_str() {
51-
"to_ascii_lowercase" => true,
52-
"to_ascii_uppercase" => false,
51+
let is_lower = match path.ident.name {
52+
sym::to_ascii_lowercase => true,
53+
sym::to_ascii_uppercase => false,
5354
_ => return None,
5455
};
5556
let ty_raw = cx.typeck_results().expr_ty(expr);

clippy_lints/src/manual_option_as_slice.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,26 @@ impl LateLintPass<'_> for ManualOptionAsSlice {
8080
check_map(cx, callee, span, self.msrv);
8181
}
8282
},
83-
ExprKind::MethodCall(seg, callee, [or], _) => match seg.ident.name.as_str() {
84-
"unwrap_or" => {
83+
ExprKind::MethodCall(seg, callee, [or], _) => match seg.ident.name {
84+
sym::unwrap_or => {
8585
if is_empty_slice(cx, or) {
8686
check_map(cx, callee, span, self.msrv);
8787
}
8888
},
89-
"unwrap_or_else" => {
89+
sym::unwrap_or_else => {
9090
if returns_empty_slice(cx, or) {
9191
check_map(cx, callee, span, self.msrv);
9292
}
9393
},
9494
_ => {},
9595
},
96-
ExprKind::MethodCall(seg, callee, [or_else, map], _) => match seg.ident.name.as_str() {
97-
"map_or" => {
96+
ExprKind::MethodCall(seg, callee, [or_else, map], _) => match seg.ident.name {
97+
sym::map_or => {
9898
if is_empty_slice(cx, or_else) && is_slice_from_ref(cx, map) {
9999
check_as_ref(cx, callee, span, self.msrv);
100100
}
101101
},
102-
"map_or_else" => {
102+
sym::map_or_else => {
103103
if returns_empty_slice(cx, or_else) && is_slice_from_ref(cx, map) {
104104
check_as_ref(cx, callee, span, self.msrv);
105105
}

clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::msrvs::{self, Msrv};
33
use clippy_utils::source::{SpanRangeExt, indent_of, reindent_multiline};
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;
@@ -21,8 +22,8 @@ pub(super) fn check<'tcx>(
2122
) {
2223
if let ExprKind::MethodCall(path_segment, ..) = recv.kind
2324
&& matches!(
24-
path_segment.ident.name.as_str(),
25-
"to_lowercase" | "to_uppercase" | "to_ascii_lowercase" | "to_ascii_uppercase"
25+
path_segment.ident.name,
26+
sym::to_lowercase | sym::to_uppercase | sym::to_ascii_lowercase | sym::to_ascii_uppercase
2627
)
2728
{
2829
return;

clippy_lints/src/methods/manual_c_str_literals.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::get_parent_expr;
32
use clippy_utils::msrvs::{self, Msrv};
43
use clippy_utils::source::snippet;
4+
use clippy_utils::{get_parent_expr, sym};
55
use rustc_ast::{LitKind, StrStyle};
66
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind, Node, QPath, TyKind};
88
use rustc_lint::LateContext;
99
use rustc_span::edition::Edition::Edition2021;
10-
use rustc_span::{Span, Symbol, sym};
10+
use rustc_span::{Span, Symbol};
1111

1212
use super::MANUAL_C_STR_LITERALS;
1313

@@ -71,15 +71,15 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args
7171
&& cx.tcx.sess.edition() >= Edition2021
7272
&& msrv.meets(cx, msrvs::C_STR_LITERALS)
7373
{
74-
match fn_name.as_str() {
75-
name @ ("from_bytes_with_nul" | "from_bytes_with_nul_unchecked")
74+
match fn_name {
75+
sym::from_bytes_with_nul | sym::from_bytes_with_nul_unchecked
7676
if !arg.span.from_expansion()
7777
&& let ExprKind::Lit(lit) = arg.kind
7878
&& let LitKind::ByteStr(_, StrStyle::Cooked) | LitKind::Str(_, StrStyle::Cooked) = lit.node =>
7979
{
80-
check_from_bytes(cx, expr, arg, name);
80+
check_from_bytes(cx, expr, arg, fn_name);
8181
},
82-
"from_ptr" => check_from_ptr(cx, expr, arg),
82+
sym::from_ptr => check_from_ptr(cx, expr, arg),
8383
_ => {},
8484
}
8585
}
@@ -106,13 +106,13 @@ fn check_from_ptr(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>) {
106106
}
107107
}
108108
/// Checks `CStr::from_bytes_with_nul(b"foo\0")`
109-
fn check_from_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, method: &str) {
109+
fn check_from_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, method: Symbol) {
110110
let (span, applicability) = if let Some(parent) = get_parent_expr(cx, expr)
111111
&& let ExprKind::MethodCall(method, ..) = parent.kind
112112
&& [sym::unwrap, sym::expect].contains(&method.ident.name)
113113
{
114114
(parent.span, Applicability::MachineApplicable)
115-
} else if method == "from_bytes_with_nul_unchecked" {
115+
} else if method == sym::from_bytes_with_nul_unchecked {
116116
// `*_unchecked` returns `&CStr` directly, nothing needs to be changed
117117
(expr.span, Applicability::MachineApplicable)
118118
} else {

clippy_lints/src/methods/needless_collect.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,20 +357,20 @@ impl<'tcx> Visitor<'tcx> for IterFunctionVisitor<'_, 'tcx> {
357357
if let Some(hir_id) = self.current_statement_hir_id {
358358
self.hir_id_uses_map.insert(hir_id, self.uses.len());
359359
}
360-
match method_name.ident.name.as_str() {
361-
"into_iter" => self.uses.push(Some(IterFunction {
360+
match method_name.ident.name {
361+
sym::into_iter => self.uses.push(Some(IterFunction {
362362
func: IterFunctionKind::IntoIter(expr.hir_id),
363363
span: expr.span,
364364
})),
365-
"len" => self.uses.push(Some(IterFunction {
365+
sym::len => self.uses.push(Some(IterFunction {
366366
func: IterFunctionKind::Len,
367367
span: expr.span,
368368
})),
369-
"is_empty" => self.uses.push(Some(IterFunction {
369+
sym::is_empty => self.uses.push(Some(IterFunction {
370370
func: IterFunctionKind::IsEmpty,
371371
span: expr.span,
372372
})),
373-
"contains" => self.uses.push(Some(IterFunction {
373+
sym::contains => self.uses.push(Some(IterFunction {
374374
func: IterFunctionKind::Contains(args[0].span),
375375
span: expr.span,
376376
})),

clippy_lints/src/mutable_debug_assertion.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
3+
use clippy_utils::sym;
34
use rustc_hir::intravisit::{Visitor, walk_expr};
45
use rustc_hir::{BorrowKind, Expr, ExprKind, MatchSource, Mutability};
56
use rustc_lint::{LateContext, LateLintPass};
@@ -42,10 +43,9 @@ impl<'tcx> LateLintPass<'tcx> for DebugAssertWithMutCall {
4243
let Some(macro_call) = root_macro_call_first_node(cx, e) else {
4344
return;
4445
};
45-
let macro_name = cx.tcx.item_name(macro_call.def_id);
4646
if !matches!(
47-
macro_name.as_str(),
48-
"debug_assert" | "debug_assert_eq" | "debug_assert_ne"
47+
cx.tcx.get_diagnostic_name(macro_call.def_id),
48+
Some(sym::debug_assert_macro | sym::debug_assert_eq_macro | sym::debug_assert_ne_macro)
4949
) {
5050
return;
5151
}
@@ -60,7 +60,10 @@ impl<'tcx> LateLintPass<'tcx> for DebugAssertWithMutCall {
6060
cx,
6161
DEBUG_ASSERT_WITH_MUT_CALL,
6262
span,
63-
format!("do not call a function with mutable arguments inside of `{macro_name}!`"),
63+
format!(
64+
"do not call a function with mutable arguments inside of `{}!`",
65+
cx.tcx.item_name(macro_call.def_id)
66+
),
6467
);
6568
}
6669
}

clippy_lints/src/operators/eq_op.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
use clippy_utils::ast_utils::is_useless_with_eq_exprs;
22
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
33
use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace};
4-
use clippy_utils::{eq_expr_value, is_in_test_function};
4+
use clippy_utils::{eq_expr_value, is_in_test_function, sym};
55
use rustc_hir::{BinOpKind, Expr};
66
use rustc_lint::LateContext;
77

88
use super::EQ_OP;
99

1010
pub(crate) fn check_assert<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
11-
if let Some((macro_call, macro_name)) = first_node_macro_backtrace(cx, e).find_map(|macro_call| {
12-
let name = cx.tcx.item_name(macro_call.def_id);
11+
if let Some(macro_call) = first_node_macro_backtrace(cx, e).find(|macro_call| {
1312
matches!(
14-
name.as_str(),
15-
"assert_eq" | "assert_ne" | "debug_assert_eq" | "debug_assert_ne"
13+
cx.tcx.get_diagnostic_name(macro_call.def_id),
14+
Some(sym::assert_eq_macro | sym::assert_ne_macro | sym::debug_assert_eq_macro | sym::debug_assert_ne_macro)
1615
)
17-
.then(|| (macro_call, name))
1816
}) && let Some((lhs, rhs, _)) = find_assert_eq_args(cx, e, macro_call.expn)
1917
&& eq_expr_value(cx, lhs, rhs)
2018
&& macro_call.is_local()
@@ -24,7 +22,10 @@ pub(crate) fn check_assert<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
2422
cx,
2523
EQ_OP,
2624
lhs.span.to(rhs.span),
27-
format!("identical args used in this `{macro_name}!` macro call"),
25+
format!(
26+
"identical args used in this `{}!` macro call",
27+
cx.tcx.item_name(macro_call.def_id)
28+
),
2829
);
2930
}
3031
}

clippy_lints/src/panic_in_result_fn.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::macros::root_macro_call_first_node;
2+
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use clippy_utils::visitors::{Descend, for_each_expr};
55
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
6969
return ControlFlow::Continue(Descend::Yes);
7070
};
7171
if !is_inside_always_const_context(cx.tcx, e.hir_id)
72-
&& matches!(
73-
cx.tcx.item_name(macro_call.def_id).as_str(),
74-
"panic" | "assert" | "assert_eq" | "assert_ne"
75-
)
72+
&& (is_panic(cx, macro_call.def_id)
73+
|| matches!(
74+
cx.tcx.get_diagnostic_name(macro_call.def_id),
75+
Some(sym::assert_macro | sym::assert_eq_macro | sym::assert_ne_macro)
76+
))
7677
{
7778
panics.push(macro_call.span);
7879
ControlFlow::Continue(Descend::No)

clippy_lints/src/panic_unimplemented.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,24 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
113113
);
114114
return;
115115
}
116-
match cx.tcx.item_name(macro_call.def_id).as_str() {
117-
"todo" => {
116+
match cx.tcx.get_diagnostic_name(macro_call.def_id) {
117+
Some(sym::todo_macro) => {
118118
span_lint(
119119
cx,
120120
TODO,
121121
macro_call.span,
122122
"`todo` should not be present in production code",
123123
);
124124
},
125-
"unimplemented" => {
125+
Some(sym::unimplemented_macro) => {
126126
span_lint(
127127
cx,
128128
UNIMPLEMENTED,
129129
macro_call.span,
130130
"`unimplemented` should not be present in production code",
131131
);
132132
},
133-
"unreachable" => {
133+
Some(sym::unreachable_macro) => {
134134
span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");
135135
},
136136
_ => {},

0 commit comments

Comments
 (0)