Skip to content

Commit 0dd9722

Browse files
authored
Replace some Symbol::as_str usage (#14679)
Follow up to #14650 Replaces uses in the form `s.as_str() == "literal"` r? @y21 changelog: none
2 parents 39a4086 + 7b337f6 commit 0dd9722

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+448
-179
lines changed

clippy_lints/src/assigning_clones.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::mir::{PossibleBorrowerMap, enclosing_mir};
44
use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::sugg::Sugg;
6-
use clippy_utils::{is_diag_trait_item, is_in_test, last_path_segment, local_is_initialized, path_to_local};
6+
use clippy_utils::{is_diag_trait_item, is_in_test, last_path_segment, local_is_initialized, path_to_local, sym};
77
use rustc_errors::Applicability;
88
use rustc_hir::{self as hir, Expr, ExprKind};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::mir;
1111
use rustc_middle::ty::{self, Instance, Mutability};
1212
use rustc_session::impl_lint_pass;
13-
use rustc_span::symbol::sym;
1413
use rustc_span::{Span, SyntaxContext};
1514

1615
declare_clippy_lint! {
@@ -86,9 +85,9 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
8685
&& ctxt.is_root()
8786
&& let which_trait = match fn_name {
8887
sym::clone if is_diag_trait_item(cx, fn_id, sym::Clone) => CloneTrait::Clone,
89-
_ if fn_name.as_str() == "to_owned"
90-
&& is_diag_trait_item(cx, fn_id, sym::ToOwned)
91-
&& self.msrv.meets(cx, msrvs::CLONE_INTO) =>
88+
sym::to_owned
89+
if is_diag_trait_item(cx, fn_id, sym::ToOwned)
90+
&& self.msrv.meets(cx, msrvs::CLONE_INTO) =>
9291
{
9392
CloneTrait::ToOwned
9493
},
@@ -112,7 +111,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
112111
&& resolved_assoc_items.in_definition_order().any(|assoc|
113112
match which_trait {
114113
CloneTrait::Clone => assoc.name() == sym::clone_from,
115-
CloneTrait::ToOwned => assoc.name().as_str() == "clone_into",
114+
CloneTrait::ToOwned => assoc.name() == sym::clone_into,
116115
}
117116
)
118117
&& !clone_source_borrows_from_dest(cx, lhs, rhs.span)

clippy_lints/src/attrs/blanket_clippy_restriction_lints.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use super::BLANKET_CLIPPY_RESTRICTION_LINTS;
22
use super::utils::extract_clippy_lint;
33
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
4+
use clippy_utils::sym;
45
use rustc_ast::MetaItemInner;
56
use rustc_lint::{EarlyContext, Level, LintContext};
7+
use rustc_span::DUMMY_SP;
68
use rustc_span::symbol::Symbol;
7-
use rustc_span::{DUMMY_SP, sym};
89

910
pub(super) fn check(cx: &EarlyContext<'_>, name: Symbol, items: &[MetaItemInner]) {
1011
for lint in items {
11-
if let Some(lint_name) = extract_clippy_lint(lint)
12-
&& lint_name.as_str() == "restriction"
13-
&& name != sym::allow
14-
{
12+
if name != sym::allow && extract_clippy_lint(lint) == Some(sym::restriction) {
1513
span_lint_and_help(
1614
cx,
1715
BLANKET_CLIPPY_RESTRICTION_LINTS,

clippy_lints/src/attrs/deprecated_cfg_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn check_deprecated_cfg_recursively(cx: &EarlyContext<'_>, attr: &rustc_ast::Met
7373
}
7474

7575
fn check_cargo_clippy_attr(cx: &EarlyContext<'_>, item: &rustc_ast::MetaItem) {
76-
if item.has_name(sym::feature) && item.value_str().is_some_and(|v| v.as_str() == "cargo-clippy") {
76+
if item.has_name(sym::feature) && item.value_str() == Some(sym::cargo_clippy) {
7777
span_lint_and_sugg(
7878
cx,
7979
DEPRECATED_CLIPPY_CFG_ATTR,

clippy_lints/src/attrs/deprecated_semver.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use super::DEPRECATED_SEMVER;
22
use clippy_utils::diagnostics::span_lint;
3+
use clippy_utils::sym;
34
use rustc_ast::{LitKind, MetaItemLit};
45
use rustc_lint::EarlyContext;
56
use rustc_span::Span;
67
use semver::Version;
78

89
pub(super) fn check(cx: &EarlyContext<'_>, span: Span, lit: &MetaItemLit) {
910
if let LitKind::Str(is, _) = lit.kind
10-
&& (is.as_str() == "TBD" || Version::parse(is.as_str()).is_ok())
11+
&& (is == sym::TBD || Version::parse(is.as_str()).is_ok())
1112
{
1213
return;
1314
}

clippy_lints/src/casts/cast_abs_to_unsigned.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::msrvs::{self, Msrv};
33
use clippy_utils::sugg::Sugg;
4+
use clippy_utils::sym;
45
use rustc_errors::Applicability;
56
use rustc_hir::{Expr, ExprKind};
67
use rustc_lint::LateContext;
@@ -19,7 +20,7 @@ pub(super) fn check(
1920
if let ty::Int(from) = cast_from.kind()
2021
&& let ty::Uint(to) = cast_to.kind()
2122
&& let ExprKind::MethodCall(method_path, receiver, [], _) = cast_expr.kind
22-
&& method_path.ident.name.as_str() == "abs"
23+
&& method_path.ident.name == sym::abs
2324
&& msrv.meets(cx, msrvs::UNSIGNED_ABS)
2425
{
2526
let span = if from.bit_width() == to.bit_width() {

clippy_lints/src/casts/cast_possible_truncation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::consts::{ConstEvalCtxt, Constant};
22
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
3-
use clippy_utils::expr_or_init;
43
use clippy_utils::source::snippet;
54
use clippy_utils::sugg::Sugg;
65
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
6+
use clippy_utils::{expr_or_init, sym};
77
use rustc_abi::IntegerType;
88
use rustc_errors::{Applicability, Diag};
99
use rustc_hir::def::{DefKind, Res};
@@ -73,7 +73,7 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
7373
nbits
7474
},
7575
ExprKind::MethodCall(method, _value, [], _) => {
76-
if method.ident.name.as_str() == "signum" {
76+
if method.ident.name == sym::signum {
7777
0 // do not lint if cast comes from a `signum` function
7878
} else {
7979
nbits

clippy_lints/src/casts/cast_ptr_alignment.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::ty::is_c_void;
3-
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant};
3+
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, sym};
44
use rustc_hir::{Expr, ExprKind, GenericArg};
55
use rustc_lint::LateContext;
66
use rustc_middle::ty::layout::LayoutOf;
77
use rustc_middle::ty::{self, Ty};
8-
use rustc_span::sym;
98

109
use super::CAST_PTR_ALIGNMENT;
1110

@@ -20,7 +19,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
2019
);
2120
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
2221
} else if let ExprKind::MethodCall(method_path, self_arg, [], _) = &expr.kind
23-
&& method_path.ident.name.as_str() == "cast"
22+
&& method_path.ident.name == sym::cast
2423
&& let Some(generic_args) = method_path.args
2524
&& let [GenericArg::Type(cast_to)] = generic_args.args
2625
// There probably is no obvious reason to do this, just to be consistent with `as` cases.

clippy_lints/src/crate_in_macro_def.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, EarlyLintPass};
77
use rustc_session::declare_lint_pass;
8-
use rustc_span::Span;
98
use rustc_span::symbol::sym;
9+
use rustc_span::{Span, kw};
1010

1111
declare_clippy_lint! {
1212
/// ### What it does
@@ -105,12 +105,11 @@ fn contains_unhygienic_crate_reference(tts: &TokenStream) -> Option<Span> {
105105
fn is_crate_keyword(tt: &TokenTree) -> Option<Span> {
106106
if let TokenTree::Token(
107107
Token {
108-
kind: TokenKind::Ident(symbol, _),
108+
kind: TokenKind::Ident(kw::Crate, _),
109109
span,
110110
},
111111
_,
112112
) = tt
113-
&& symbol.as_str() == "crate"
114113
{
115114
Some(*span)
116115
} else {

clippy_lints/src/floating_point_arithmetic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::consts::{ConstEvalCtxt, Constant};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::{
55
eq_expr_value, get_parent_expr, higher, is_in_const_context, is_inherent_method_call, is_no_std_crate,
6-
numeric_literal, peel_blocks, sugg,
6+
numeric_literal, peel_blocks, sugg, sym,
77
};
88
use rustc_errors::Applicability;
99
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
@@ -435,7 +435,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
435435
rhs,
436436
) = expr.kind
437437
&& let ExprKind::MethodCall(path, self_arg, [], _) = &lhs.kind
438-
&& path.ident.name.as_str() == "exp"
438+
&& path.ident.name == sym::exp
439439
&& cx.typeck_results().expr_ty(lhs).is_floating_point()
440440
&& let Some(value) = ConstEvalCtxt::new(cx).eval(rhs)
441441
&& (F32(1.0) == value || F64(1.0) == value)

clippy_lints/src/from_raw_with_void_ptr.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::path_def_id;
32
use clippy_utils::ty::is_c_void;
3+
use clippy_utils::{path_def_id, sym};
44
use rustc_hir::def_id::DefId;
55
use rustc_hir::{Expr, ExprKind, QPath};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_middle::ty;
88
use rustc_session::declare_lint_pass;
9-
use rustc_span::sym;
109

1110
declare_clippy_lint! {
1211
/// ### What it does
@@ -41,7 +40,7 @@ impl LateLintPass<'_> for FromRawWithVoidPtr {
4140
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
4241
if let ExprKind::Call(box_from_raw, [arg]) = expr.kind
4342
&& let ExprKind::Path(QPath::TypeRelative(ty, seg)) = box_from_raw.kind
44-
&& seg.ident.name.as_str() == "from_raw"
43+
&& seg.ident.name == sym::from_raw
4544
&& let Some(type_str) = path_def_id(cx, ty).and_then(|id| def_id_matches_type(cx, id))
4645
&& let arg_kind = cx.typeck_results().expr_ty(arg).kind()
4746
&& let ty::RawPtr(ty, _) = arg_kind

clippy_lints/src/from_str_radix_10.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::sugg::Sugg;
33
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
4-
use clippy_utils::{is_in_const_context, is_integer_literal};
4+
use clippy_utils::{is_in_const_context, is_integer_literal, sym};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, LangItem, PrimTy, QPath, TyKind, def};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty::Ty;
99
use rustc_session::declare_lint_pass;
10-
use rustc_span::symbol::sym;
1110

1211
declare_clippy_lint! {
1312
/// ### What it does
@@ -53,7 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
5352

5453
// check if the second part of the path indeed calls the associated
5554
// function `from_str_radix`
56-
&& pathseg.ident.name.as_str() == "from_str_radix"
55+
&& pathseg.ident.name == sym::from_str_radix
5756

5857
// check if the first part of the path is some integer primitive
5958
&& let TyKind::Path(ty_qpath) = &ty.kind

clippy_lints/src/implicit_hasher.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use rustc_middle::hir::nested_filter;
1010
use rustc_middle::ty::{Ty, TypeckResults};
1111
use rustc_session::declare_lint_pass;
1212
use rustc_span::Span;
13-
use rustc_span::symbol::sym;
1413

1514
use clippy_utils::diagnostics::span_lint_and_then;
1615
use clippy_utils::source::{IntoSpan, SpanRangeExt, snippet};
16+
use clippy_utils::sym;
1717
use clippy_utils::ty::is_type_diagnostic_item;
1818

1919
declare_clippy_lint! {
@@ -326,37 +326,40 @@ impl<'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'_, '_, 'tcx> {
326326
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
327327
if let ExprKind::Call(fun, args) = e.kind
328328
&& let ExprKind::Path(QPath::TypeRelative(ty, method)) = fun.kind
329+
&& matches!(method.ident.name, sym::new | sym::with_capacity)
329330
&& let TyKind::Path(QPath::Resolved(None, ty_path)) = ty.kind
330331
&& let Some(ty_did) = ty_path.res.opt_def_id()
331332
{
332333
if self.target.ty() != self.maybe_typeck_results.unwrap().expr_ty(e) {
333334
return;
334335
}
335336

336-
if self.cx.tcx.is_diagnostic_item(sym::HashMap, ty_did) {
337-
if method.ident.name == sym::new {
337+
match (self.cx.tcx.get_diagnostic_name(ty_did), method.ident.name) {
338+
(Some(sym::HashMap), sym::new) => {
338339
self.suggestions.insert(e.span, "HashMap::default()".to_string());
339-
} else if method.ident.name.as_str() == "with_capacity" {
340+
},
341+
(Some(sym::HashMap), sym::with_capacity) => {
340342
self.suggestions.insert(
341343
e.span,
342344
format!(
343345
"HashMap::with_capacity_and_hasher({}, Default::default())",
344346
snippet(self.cx, args[0].span, "capacity"),
345347
),
346348
);
347-
}
348-
} else if self.cx.tcx.is_diagnostic_item(sym::HashSet, ty_did) {
349-
if method.ident.name == sym::new {
349+
},
350+
(Some(sym::HashSet), sym::new) => {
350351
self.suggestions.insert(e.span, "HashSet::default()".to_string());
351-
} else if method.ident.name.as_str() == "with_capacity" {
352+
},
353+
(Some(sym::HashSet), sym::with_capacity) => {
352354
self.suggestions.insert(
353355
e.span,
354356
format!(
355357
"HashSet::with_capacity_and_hasher({}, Default::default())",
356358
snippet(self.cx, args[0].span, "capacity"),
357359
),
358360
);
359-
}
361+
},
362+
_ => {},
360363
}
361364
}
362365

clippy_lints/src/infinite_iter.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::higher;
32
use clippy_utils::ty::{get_type_diagnostic_name, implements_trait};
3+
use clippy_utils::{higher, sym};
44
use rustc_hir::{BorrowKind, Closure, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
7-
use rustc_span::symbol::sym;
87

98
declare_clippy_lint! {
109
/// ### What it does
@@ -156,7 +155,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
156155
.and(cap);
157156
}
158157
}
159-
if method.ident.name.as_str() == "flat_map"
158+
if method.ident.name == sym::flat_map
160159
&& args.len() == 1
161160
&& let ExprKind::Closure(&Closure { body, .. }) = args[0].kind
162161
{
@@ -224,15 +223,15 @@ fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
224223
return MaybeInfinite.and(is_infinite(cx, receiver));
225224
}
226225
}
227-
if method.ident.name.as_str() == "last" && args.is_empty() {
226+
if method.ident.name == sym::last && args.is_empty() {
228227
let not_double_ended = cx
229228
.tcx
230229
.get_diagnostic_item(sym::DoubleEndedIterator)
231230
.is_some_and(|id| !implements_trait(cx, cx.typeck_results().expr_ty(receiver), id, &[]));
232231
if not_double_ended {
233232
return is_infinite(cx, receiver);
234233
}
235-
} else if method.ident.name.as_str() == "collect" {
234+
} else if method.ident.name == sym::collect {
236235
let ty = cx.typeck_results().expr_ty(expr);
237236
if matches!(
238237
get_type_diagnostic_name(cx, ty),

clippy_lints/src/iter_without_into_iter.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::get_parent_as_impl;
32
use clippy_utils::source::snippet;
43
use clippy_utils::ty::{deref_chain, get_adt_inherent_method, implements_trait, make_normalized_projection};
4+
use clippy_utils::{get_parent_as_impl, sym};
55
use rustc_ast::Mutability;
66
use rustc_errors::Applicability;
77
use rustc_hir::{FnRetTy, ImplItemKind, ImplicitSelfKind, ItemKind, TyKind};
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_middle::ty::{self, Ty};
1010
use rustc_session::declare_lint_pass;
11-
use rustc_span::sym;
1211

1312
declare_clippy_lint! {
1413
/// ### What it does
@@ -141,7 +140,7 @@ impl LateLintPass<'_> for IterWithoutIntoIter {
141140
ty.peel_refs().is_slice() || get_adt_inherent_method(cx, ty, expected_method_name).is_some()
142141
})
143142
&& let Some(iter_assoc_span) = imp.items.iter().find_map(|item| {
144-
if item.ident.name.as_str() == "IntoIter" {
143+
if item.ident.name == sym::IntoIter {
145144
Some(cx.tcx.hir_impl_item(item.id).expect_type().span)
146145
} else {
147146
None

clippy_lints/src/len_zero.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_the
22
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
33
use clippy_utils::sugg::{Sugg, has_enclosing_paren};
44
use clippy_utils::ty::implements_trait;
5-
use clippy_utils::{fulfill_or_allowed, get_item_name, get_parent_as_impl, is_trait_method, peel_ref_operators, sym};
5+
use clippy_utils::{
6+
fulfill_or_allowed, get_parent_as_impl, is_trait_method, parent_item_name, peel_ref_operators, sym,
7+
};
68
use rustc_ast::ast::LitKind;
79
use rustc_errors::Applicability;
810
use rustc_hir::def::Res;
@@ -533,9 +535,7 @@ fn check_cmp(cx: &LateContext<'_>, span: Span, method: &Expr<'_>, lit: &Expr<'_>
533535

534536
if let (&ExprKind::MethodCall(method_path, receiver, [], _), ExprKind::Lit(lit)) = (&method.kind, &lit.kind) {
535537
// check if we are in an is_empty() method
536-
if let Some(name) = get_item_name(cx, method)
537-
&& name.as_str() == "is_empty"
538-
{
538+
if parent_item_name(cx, method) == Some(sym::is_empty) {
539539
return;
540540
}
541541

0 commit comments

Comments
 (0)