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,

View file

@ -4,7 +4,7 @@ use rustc_ast::LitKind;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Expr, ExprKind};
use rustc_hir::{Expr, ExprKind, Lit, Node, Pat, PatExprKind, PatKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_lint_defs::declare_tool_lint;
use rustc_middle::mir::ConstValue;
@ -65,6 +65,39 @@ pub struct Symbols {
impl_lint_pass!(Symbols => [INTERNING_LITERALS, SYMBOL_AS_STR]);
impl Symbols {
fn lit_suggestion(&self, lit: &Lit) -> Option<(Span, String)> {
if let LitKind::Str(name, _) = lit.node {
let sugg = if let Some((prefix, name)) = self.symbol_map.get(&name.as_u32()) {
format!("{prefix}::{name}")
} else {
format!("sym::{}", name.as_str().replace(|ch: char| !ch.is_alphanumeric(), "_"))
};
Some((lit.span, sugg))
} else {
None
}
}
fn expr_suggestion(&self, expr: &Expr<'_>) -> Option<(Span, String)> {
if let ExprKind::Lit(lit) = expr.kind {
self.lit_suggestion(lit)
} else {
None
}
}
fn pat_suggestions(&self, pat: &Pat<'_>, suggestions: &mut Vec<(Span, String)>) {
pat.walk_always(|pat| {
if let PatKind::Expr(pat_expr) = pat.kind
&& let PatExprKind::Lit { lit, .. } = pat_expr.kind
{
suggestions.extend(self.lit_suggestion(lit));
}
});
}
}
impl<'tcx> LateLintPass<'tcx> for Symbols {
fn check_crate(&mut self, cx: &LateContext<'_>) {
let modules = [
@ -75,7 +108,7 @@ impl<'tcx> LateLintPass<'tcx> for Symbols {
for (prefix, module) in modules {
for def_id in module.get(cx) {
// When linting `clippy_utils` itself we can't use `module_children` as it's a local def id. It will
// still lint but the suggestion will say to add it to `sym.rs` even if it's already there
// still lint but the suggestion may suggest the incorrect name for symbols such as `sym::CRLF`
if def_id.is_local() {
continue;
}
@ -98,8 +131,7 @@ impl<'tcx> LateLintPass<'tcx> for Symbols {
if let ExprKind::Call(func, [arg]) = &expr.kind
&& let ty::FnDef(def_id, _) = cx.typeck_results().expr_ty(func).kind()
&& cx.tcx.is_diagnostic_item(sym::SymbolIntern, *def_id)
&& let ExprKind::Lit(lit) = arg.kind
&& let LitKind::Str(name, _) = lit.node
&& let Some((_, sugg)) = self.expr_suggestion(arg)
{
span_lint_and_then(
cx,
@ -107,48 +139,55 @@ impl<'tcx> LateLintPass<'tcx> for Symbols {
expr.span,
"interning a string literal",
|diag| {
let (message, path) = suggestion(&mut self.symbol_map, name);
diag.span_suggestion_verbose(expr.span, message, path, Applicability::MaybeIncorrect);
diag.span_suggestion_verbose(
expr.span,
"use a preinterned symbol instead",
sugg,
Applicability::MaybeIncorrect,
);
diag.help("add the symbol to `clippy_utils/src/sym.rs` if needed");
},
);
}
if let ExprKind::Binary(_, lhs, rhs) = expr.kind {
check_binary(cx, lhs, rhs, &mut self.symbol_map);
check_binary(cx, rhs, lhs, &mut self.symbol_map);
}
}
}
if let Some(as_str) = as_str_span(cx, expr)
&& let Node::Expr(parent) = cx.tcx.parent_hir_node(expr.hir_id)
{
let mut suggestions = Vec::new();
fn check_binary(
cx: &LateContext<'_>,
lhs: &Expr<'_>,
rhs: &Expr<'_>,
symbols: &mut FxHashMap<u32, (&'static str, Symbol)>,
) {
if let Some(removal_span) = as_str_span(cx, lhs)
&& let ExprKind::Lit(lit) = rhs.kind
&& let LitKind::Str(name, _) = lit.node
{
span_lint_and_then(cx, SYMBOL_AS_STR, lhs.span, "converting a Symbol to a string", |diag| {
let (message, path) = suggestion(symbols, name);
diag.multipart_suggestion_verbose(
message,
vec![(removal_span, String::new()), (rhs.span, path)],
Applicability::MachineApplicable,
match parent.kind {
ExprKind::Binary(_, lhs, rhs) => {
suggestions.extend(self.expr_suggestion(lhs));
suggestions.extend(self.expr_suggestion(rhs));
},
ExprKind::Match(_, arms, _) => {
for arm in arms {
self.pat_suggestions(arm.pat, &mut suggestions);
}
},
_ => {},
}
if suggestions.is_empty() {
return;
}
span_lint_and_then(
cx,
SYMBOL_AS_STR,
expr.span,
"converting a Symbol to a string",
|diag| {
suggestions.push((as_str, String::new()));
diag.multipart_suggestion(
"use preinterned symbols instead",
suggestions,
Applicability::MaybeIncorrect,
);
diag.help("add the symbols to `clippy_utils/src/sym.rs` if needed");
},
);
});
}
}
fn suggestion(symbols: &mut FxHashMap<u32, (&'static str, Symbol)>, name: Symbol) -> (&'static str, String) {
if let Some((prefix, name)) = symbols.get(&name.as_u32()) {
("use the preinterned symbol", format!("{prefix}::{name}"))
} else {
(
"add the symbol to `clippy_utils/src/sym.rs` and use it",
format!("sym::{}", name.as_str().replace(|ch: char| !ch.is_alphanumeric(), "_")),
)
}
}
}

View file

@ -32,12 +32,14 @@ macro_rules! generate {
generate! {
abs,
align_of,
ambiguous_glob_reexports,
as_bytes,
as_deref_mut,
as_deref,
as_mut,
AsyncReadExt,
AsyncWriteExt,
BACKSLASH_SINGLE_QUOTE: r"\'",
Binary,
build_hasher,
bytes,
@ -59,8 +61,11 @@ generate! {
de,
Deserialize,
diagnostics,
disallowed_types,
DOUBLE_QUOTE: "\"",
EarlyLintPass,
ends_with,
enum_glob_use,
error,
ErrorKind,
exp,
@ -69,12 +74,16 @@ generate! {
finish,
flat_map,
for_each,
from_bytes_with_nul_unchecked,
from_bytes_with_nul,
from_ptr,
from_raw,
from_ref,
from_str_radix,
fs,
futures_util,
get,
hidden_glob_reexports,
hygiene,
insert,
int_roundings,
@ -96,20 +105,27 @@ generate! {
Lazy,
LF: "\n",
Lint,
ln,
lock_api,
log,
LowerExp,
LowerHex,
macro_use_imports,
map_or_else,
map_or,
max,
MAX,
mem,
min,
MIN,
mode,
module_name_repetitions,
msrv,
msrvs,
MsrvStack,
mut_ptr,
mutex,
needless_return,
next_tuple,
Octal,
once_cell,
@ -119,7 +135,10 @@ generate! {
parse,
PathLookup,
paths,
powf,
powi,
push,
redundant_pub_crate,
regex,
Regex,
RegexBuilder,
@ -137,29 +156,45 @@ generate! {
set_mode,
set_readonly,
signum,
single_component_path_imports,
span_lint_and_then,
split_whitespace,
split,
sqrt,
Start,
Step,
style,
symbol,
Symbol,
SyntaxContext,
take,
TBD,
then_some,
to_ascii_lowercase,
to_ascii_uppercase,
to_digit,
to_lowercase,
to_owned,
to_uppercase,
tokio,
unreachable_pub,
unsafe_removed_from_name,
unused_braces,
unused_extern_crates,
unused_import_braces,
unused_trait_names,
unused,
unwrap_err,
unwrap_or_default,
unwrap_or_else,
UpperExp,
UpperHex,
V4,
V6,
Visitor,
warnings,
Weak,
wildcard_imports,
with_capacity,
wrapping_offset,
}

View file

@ -4,9 +4,10 @@ error: interning a string literal
LL | let _ = Symbol::intern("f32");
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: add the symbol to `clippy_utils/src/sym.rs` if needed
= note: `-D clippy::interning-literals` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::interning_literals)]`
help: use the preinterned symbol
help: use a preinterned symbol instead
|
LL - let _ = Symbol::intern("f32");
LL + let _ = sym::f32;
@ -18,7 +19,8 @@ error: interning a string literal
LL | let _ = Symbol::intern("proc-macro");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use the preinterned symbol
= help: add the symbol to `clippy_utils/src/sym.rs` if needed
help: use a preinterned symbol instead
|
LL - let _ = Symbol::intern("proc-macro");
LL + let _ = sym::proc_dash_macro;
@ -30,7 +32,8 @@ error: interning a string literal
LL | let _ = Symbol::intern("self");
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: use the preinterned symbol
= help: add the symbol to `clippy_utils/src/sym.rs` if needed
help: use a preinterned symbol instead
|
LL - let _ = Symbol::intern("self");
LL + let _ = kw::SelfLower;
@ -42,7 +45,8 @@ error: interning a string literal
LL | let _ = Symbol::intern("msrv");
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: use the preinterned symbol
= help: add the symbol to `clippy_utils/src/sym.rs` if needed
help: use a preinterned symbol instead
|
LL - let _ = Symbol::intern("msrv");
LL + let _ = sym::msrv;
@ -54,7 +58,8 @@ error: interning a string literal
LL | let _ = Symbol::intern("Cargo.toml");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use the preinterned symbol
= help: add the symbol to `clippy_utils/src/sym.rs` if needed
help: use a preinterned symbol instead
|
LL - let _ = Symbol::intern("Cargo.toml");
LL + let _ = sym::Cargo_toml;

View file

@ -4,9 +4,10 @@ error: interning a string literal
LL | let _ = Symbol::intern("xyz123");
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add the symbol to `clippy_utils/src/sym.rs` if needed
= note: `-D clippy::interning-literals` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::interning_literals)]`
help: add the symbol to `clippy_utils/src/sym.rs` and use it
help: use a preinterned symbol instead
|
LL - let _ = Symbol::intern("xyz123");
LL + let _ = sym::xyz123;
@ -18,7 +19,8 @@ error: interning a string literal
LL | let _ = Symbol::intern("with-dash");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: add the symbol to `clippy_utils/src/sym.rs` and use it
= help: add the symbol to `clippy_utils/src/sym.rs` if needed
help: use a preinterned symbol instead
|
LL - let _ = Symbol::intern("with-dash");
LL + let _ = sym::with_dash;
@ -30,7 +32,8 @@ error: interning a string literal
LL | let _ = Symbol::intern("with.dot");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: add the symbol to `clippy_utils/src/sym.rs` and use it
= help: add the symbol to `clippy_utils/src/sym.rs` if needed
help: use a preinterned symbol instead
|
LL - let _ = Symbol::intern("with.dot");
LL + let _ = sym::with_dot;

View file

@ -18,4 +18,11 @@ fn f(s: Symbol) {
//~^ symbol_as_str
sym::get == s;
//~^ symbol_as_str
let _ = match s {
//~^ symbol_as_str
sym::unwrap_err => 1,
sym::unwrap_or_default | sym::unwrap_or_else => 2,
_ => 3,
};
}

View file

@ -18,4 +18,11 @@ fn f(s: Symbol) {
//~^ symbol_as_str
"get" == s.as_str();
//~^ symbol_as_str
let _ = match s.as_str() {
//~^ symbol_as_str
"unwrap_err" => 1,
"unwrap_or_default" | "unwrap_or_else" => 2,
_ => 3,
};
}

View file

@ -4,9 +4,10 @@ error: converting a Symbol to a string
LL | s.as_str() == "f32";
| ^^^^^^^^^^
|
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
= note: `-D clippy::symbol-as-str` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::symbol_as_str)]`
help: use the preinterned symbol
help: use preinterned symbols instead
|
LL - s.as_str() == "f32";
LL + s == sym::f32;
@ -18,7 +19,8 @@ error: converting a Symbol to a string
LL | s.as_str() == "proc-macro";
| ^^^^^^^^^^
|
help: use the preinterned symbol
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
help: use preinterned symbols instead
|
LL - s.as_str() == "proc-macro";
LL + s == sym::proc_dash_macro;
@ -30,7 +32,8 @@ error: converting a Symbol to a string
LL | s.as_str() == "self";
| ^^^^^^^^^^
|
help: use the preinterned symbol
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
help: use preinterned symbols instead
|
LL - s.as_str() == "self";
LL + s == kw::SelfLower;
@ -42,7 +45,8 @@ error: converting a Symbol to a string
LL | s.as_str() == "msrv";
| ^^^^^^^^^^
|
help: use the preinterned symbol
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
help: use preinterned symbols instead
|
LL - s.as_str() == "msrv";
LL + s == sym::msrv;
@ -54,7 +58,8 @@ error: converting a Symbol to a string
LL | s.as_str() == "Cargo.toml";
| ^^^^^^^^^^
|
help: use the preinterned symbol
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
help: use preinterned symbols instead
|
LL - s.as_str() == "Cargo.toml";
LL + s == sym::Cargo_toml;
@ -66,11 +71,27 @@ error: converting a Symbol to a string
LL | "get" == s.as_str();
| ^^^^^^^^^^
|
help: use the preinterned symbol
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
help: use preinterned symbols instead
|
LL - "get" == s.as_str();
LL + sym::get == s;
|
error: aborting due to 6 previous errors
error: converting a Symbol to a string
--> tests/ui-internal/symbol_as_str.rs:22:19
|
LL | let _ = match s.as_str() {
| ^^^^^^^^^^
|
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
help: use preinterned symbols instead
|
LL ~ let _ = match s {
LL |
LL ~ sym::unwrap_err => 1,
LL ~ sym::unwrap_or_default | sym::unwrap_or_else => 2,
|
error: aborting due to 7 previous errors

View file

@ -4,9 +4,10 @@ error: converting a Symbol to a string
LL | s.as_str() == "xyz123";
| ^^^^^^^^^^
|
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
= note: `-D clippy::symbol-as-str` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::symbol_as_str)]`
help: add the symbol to `clippy_utils/src/sym.rs` and use it
help: use preinterned symbols instead
|
LL - s.as_str() == "xyz123";
LL + s == sym::xyz123;
@ -18,7 +19,8 @@ error: converting a Symbol to a string
LL | s.as_str() == "with-dash";
| ^^^^^^^^^^
|
help: add the symbol to `clippy_utils/src/sym.rs` and use it
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
help: use preinterned symbols instead
|
LL - s.as_str() == "with-dash";
LL + s == sym::with_dash;
@ -30,7 +32,8 @@ error: converting a Symbol to a string
LL | s.as_str() == "with.dot";
| ^^^^^^^^^^
|
help: add the symbol to `clippy_utils/src/sym.rs` and use it
= help: add the symbols to `clippy_utils/src/sym.rs` if needed
help: use preinterned symbols instead
|
LL - s.as_str() == "with.dot";
LL + s == sym::with_dot;