Skip to content

Commit 3f0c7e3

Browse files
committed
Use interned symbols instead of strings in more places
1 parent 6753e16 commit 3f0c7e3

File tree

6 files changed

+72
-52
lines changed

6 files changed

+72
-52
lines changed

clippy_lints/src/arbitrary_source_item_ordering.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl ArbitrarySourceItemOrdering {
204204
self.assoc_types_order
205205
),
206206
Some(before_item.span),
207-
format!("should be placed before `{}`", before_item.ident.as_str(),),
207+
format!("should be placed before `{}`", before_item.ident.name),
208208
);
209209
}
210210

@@ -216,7 +216,7 @@ impl ArbitrarySourceItemOrdering {
216216
ident.span,
217217
"incorrect ordering of items (must be alphabetically ordered)",
218218
Some(before_ident.span),
219-
format!("should be placed before `{}`", before_ident.as_str(),),
219+
format!("should be placed before `{}`", before_ident.name),
220220
);
221221
}
222222

@@ -228,7 +228,7 @@ impl ArbitrarySourceItemOrdering {
228228
};
229229

230230
let (before_span, note) = if let Some(ident) = before_item.kind.ident() {
231-
(ident.span, format!("should be placed before `{}`", ident.as_str(),))
231+
(ident.span, format!("should be placed before `{}`", ident.name))
232232
} else {
233233
(
234234
before_item.span,
@@ -255,7 +255,7 @@ impl ArbitrarySourceItemOrdering {
255255
self.assoc_types_order
256256
),
257257
Some(before_item.span),
258-
format!("should be placed before `{}`", before_item.ident.as_str(),),
258+
format!("should be placed before `{}`", before_item.ident.name),
259259
);
260260
}
261261
}

clippy_lints/src/attrs/duplicated_attributes.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ use clippy_utils::diagnostics::span_lint_and_then;
33
use rustc_ast::{Attribute, MetaItem};
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_lint::EarlyContext;
6-
use rustc_span::{Span, sym};
6+
use rustc_span::{Span, Symbol, sym};
77
use std::collections::hash_map::Entry;
88

99
fn emit_if_duplicated(
1010
cx: &EarlyContext<'_>,
1111
attr: &MetaItem,
1212
attr_paths: &mut FxHashMap<String, Span>,
13-
complete_path: String,
13+
parent: &[Symbol],
14+
complete_path: impl FnOnce(&str) -> String,
1415
) {
15-
match attr_paths.entry(complete_path) {
16+
let parent = parent.iter().map(Symbol::to_string).collect::<Vec<_>>().join(":");
17+
match attr_paths.entry(complete_path(&parent)) {
1618
Entry::Vacant(v) => {
1719
v.insert(attr.span);
1820
},
@@ -29,7 +31,7 @@ fn check_duplicated_attr(
2931
cx: &EarlyContext<'_>,
3032
attr: &MetaItem,
3133
attr_paths: &mut FxHashMap<String, Span>,
32-
parent: &mut Vec<String>,
34+
parent: &mut Vec<Symbol>,
3335
) {
3436
if attr.span.from_expansion() {
3537
return;
@@ -43,25 +45,27 @@ fn check_duplicated_attr(
4345
return;
4446
}
4547
if let Some(direct_parent) = parent.last()
46-
&& direct_parent == sym::cfg_trace.as_str()
48+
&& *direct_parent == sym::cfg_trace
4749
&& [sym::all, sym::not, sym::any].contains(&name)
4850
{
4951
// FIXME: We don't correctly check `cfg`s for now, so if it's more complex than just a one
5052
// level `cfg`, we leave.
5153
return;
5254
}
5355
if let Some(value) = attr.value_str() {
54-
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}={value}", parent.join(":")));
56+
emit_if_duplicated(cx, attr, attr_paths, parent, |parent| {
57+
format!("{parent}:{name}={value}")
58+
});
5559
} else if let Some(sub_attrs) = attr.meta_item_list() {
56-
parent.push(name.as_str().to_string());
60+
parent.push(name);
5761
for sub_attr in sub_attrs {
5862
if let Some(meta) = sub_attr.meta_item() {
5963
check_duplicated_attr(cx, meta, attr_paths, parent);
6064
}
6165
}
6266
parent.pop();
6367
} else {
64-
emit_if_duplicated(cx, attr, attr_paths, format!("{}:{name}", parent.join(":")));
68+
emit_if_duplicated(cx, attr, attr_paths, parent, |parent| format!("{parent}:{name}"));
6569
}
6670
}
6771

clippy_lints/src/booleans.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
3-
use clippy_utils::eq_expr_value;
43
use clippy_utils::msrvs::{self, Msrv};
54
use clippy_utils::source::SpanRangeExt;
65
use clippy_utils::sugg::Sugg;
76
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
7+
use clippy_utils::{eq_expr_value, sym};
88
use rustc_ast::ast::LitKind;
99
use rustc_attr_parsing::RustcVersion;
1010
use rustc_errors::Applicability;
@@ -13,7 +13,7 @@ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
1313
use rustc_lint::{LateContext, LateLintPass, Level};
1414
use rustc_session::impl_lint_pass;
1515
use rustc_span::def_id::LocalDefId;
16-
use rustc_span::{Span, SyntaxContext, sym};
16+
use rustc_span::{Span, Symbol, SyntaxContext};
1717

1818
declare_clippy_lint! {
1919
/// ### What it does
@@ -73,10 +73,10 @@ declare_clippy_lint! {
7373
}
7474

7575
// For each pairs, both orders are considered.
76-
const METHODS_WITH_NEGATION: [(Option<RustcVersion>, &str, &str); 3] = [
77-
(None, "is_some", "is_none"),
78-
(None, "is_err", "is_ok"),
79-
(Some(msrvs::IS_NONE_OR), "is_some_and", "is_none_or"),
76+
const METHODS_WITH_NEGATION: [(Option<RustcVersion>, Symbol, Symbol); 3] = [
77+
(None, sym::is_some, sym::is_none),
78+
(None, sym::is_err, sym::is_ok),
79+
(Some(msrvs::IS_NONE_OR), sym::is_some_and, sym::is_none_or),
8080
];
8181

8282
pub struct NonminimalBool {
@@ -440,9 +440,7 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: Msrv, expr: &Expr<'_>) -> Optio
440440
.iter()
441441
.copied()
442442
.flat_map(|(msrv, a, b)| vec![(msrv, a, b), (msrv, b, a)])
443-
.find(|&(msrv, a, _)| {
444-
a == path.ident.name.as_str() && msrv.is_none_or(|msrv| curr_msrv.meets(cx, msrv))
445-
})
443+
.find(|&(msrv, a, _)| a == path.ident.name && msrv.is_none_or(|msrv| curr_msrv.meets(cx, msrv)))
446444
.and_then(|(_, _, neg_method)| {
447445
let negated_args = args
448446
.iter()

clippy_lints/src/checked_conversions.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::source::snippet_with_applicability;
5-
use clippy_utils::{SpanlessEq, is_in_const_context, is_integer_literal};
5+
use clippy_utils::{SpanlessEq, is_in_const_context, is_integer_literal, sym};
66
use rustc_errors::Applicability;
77
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, TyKind};
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_session::impl_lint_pass;
10+
use rustc_span::Symbol;
1011

1112
declare_clippy_lint! {
1213
/// ### What it does
@@ -98,7 +99,7 @@ impl LateLintPass<'_> for CheckedConversions {
9899
struct Conversion<'a> {
99100
cvt: ConversionType,
100101
expr_to_cast: &'a Expr<'a>,
101-
to_type: Option<&'a str>,
102+
to_type: Option<Symbol>,
102103
}
103104

104105
/// The kind of conversion that is checked
@@ -150,7 +151,7 @@ impl<'a> Conversion<'a> {
150151
}
151152

152153
/// Try to construct a new conversion if the conversion type is valid
153-
fn try_new(expr_to_cast: &'a Expr<'_>, from_type: &str, to_type: &'a str) -> Option<Conversion<'a>> {
154+
fn try_new(expr_to_cast: &'a Expr<'_>, from_type: Symbol, to_type: Symbol) -> Option<Conversion<'a>> {
154155
ConversionType::try_new(from_type, to_type).map(|cvt| Conversion {
155156
cvt,
156157
expr_to_cast,
@@ -171,7 +172,7 @@ impl<'a> Conversion<'a> {
171172
impl ConversionType {
172173
/// Creates a conversion type if the type is allowed & conversion is valid
173174
#[must_use]
174-
fn try_new(from: &str, to: &str) -> Option<Self> {
175+
fn try_new(from: Symbol, to: Symbol) -> Option<Self> {
175176
if UINTS.contains(&from) {
176177
Some(Self::FromUnsigned)
177178
} else if SINTS.contains(&from) {
@@ -190,7 +191,7 @@ impl ConversionType {
190191

191192
/// Check for `expr <= (to_type::MAX as from_type)`
192193
fn check_upper_bound<'tcx>(lt: &'tcx Expr<'tcx>, gt: &'tcx Expr<'tcx>) -> Option<Conversion<'tcx>> {
193-
if let Some((from, to)) = get_types_from_cast(gt, INTS, "max_value", "MAX") {
194+
if let Some((from, to)) = get_types_from_cast(gt, INTS, sym::max_value, sym::MAX) {
194195
Conversion::try_new(lt, from, to)
195196
} else {
196197
None
@@ -209,23 +210,23 @@ fn check_lower_bound_zero<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> O
209210

210211
/// Check for `expr >= (to_type::MIN as from_type)`
211212
fn check_lower_bound_min<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> Option<Conversion<'a>> {
212-
if let Some((from, to)) = get_types_from_cast(check, SINTS, "min_value", "MIN") {
213+
if let Some((from, to)) = get_types_from_cast(check, SINTS, sym::min_value, sym::MIN) {
213214
Conversion::try_new(candidate, from, to)
214215
} else {
215216
None
216217
}
217218
}
218219

219220
/// Tries to extract the from- and to-type from a cast expression
220-
fn get_types_from_cast<'a>(
221-
expr: &'a Expr<'_>,
222-
types: &'a [&str],
223-
func: &'a str,
224-
assoc_const: &'a str,
225-
) -> Option<(&'a str, &'a str)> {
221+
fn get_types_from_cast(
222+
expr: &Expr<'_>,
223+
types: &[Symbol],
224+
func: Symbol,
225+
assoc_const: Symbol,
226+
) -> Option<(Symbol, Symbol)> {
226227
// `to_type::max_value() as from_type`
227228
// or `to_type::MAX as from_type`
228-
let call_from_cast: Option<(&Expr<'_>, &str)> = if let ExprKind::Cast(limit, from_type) = &expr.kind
229+
let call_from_cast: Option<(&Expr<'_>, Symbol)> = if let ExprKind::Cast(limit, from_type) = &expr.kind
229230
// to_type::max_value(), from_type
230231
&& let TyKind::Path(from_type_path) = &from_type.kind
231232
&& let Some(from_sym) = int_ty_to_sym(from_type_path)
@@ -236,12 +237,12 @@ fn get_types_from_cast<'a>(
236237
};
237238

238239
// `from_type::from(to_type::max_value())`
239-
let limit_from: Option<(&Expr<'_>, &str)> = call_from_cast.or_else(|| {
240+
let limit_from: Option<(&Expr<'_>, Symbol)> = call_from_cast.or_else(|| {
240241
if let ExprKind::Call(from_func, [limit]) = &expr.kind
241242
// `from_type::from, to_type::max_value()`
242243
// `from_type::from`
243244
&& let ExprKind::Path(path) = &from_func.kind
244-
&& let Some(from_sym) = get_implementing_type(path, INTS, "from")
245+
&& let Some(from_sym) = get_implementing_type(path, INTS, sym::from)
245246
{
246247
Some((limit, from_sym))
247248
} else {
@@ -273,32 +274,41 @@ fn get_types_from_cast<'a>(
273274
}
274275

275276
/// Gets the type which implements the called function
276-
fn get_implementing_type<'a>(path: &QPath<'_>, candidates: &'a [&str], function: &str) -> Option<&'a str> {
277+
fn get_implementing_type(path: &QPath<'_>, candidates: &[Symbol], function: Symbol) -> Option<Symbol> {
277278
if let QPath::TypeRelative(ty, path) = &path
278-
&& path.ident.name.as_str() == function
279+
&& path.ident.name == function
279280
&& let TyKind::Path(QPath::Resolved(None, tp)) = &ty.kind
280281
&& let [int] = tp.segments
281282
{
282-
let name = int.ident.name.as_str();
283-
candidates.iter().find(|c| &name == *c).copied()
283+
candidates.iter().find(|c| int.ident.name == **c).copied()
284284
} else {
285285
None
286286
}
287287
}
288288

289289
/// Gets the type as a string, if it is a supported integer
290-
fn int_ty_to_sym<'tcx>(path: &QPath<'_>) -> Option<&'tcx str> {
290+
fn int_ty_to_sym(path: &QPath<'_>) -> Option<Symbol> {
291291
if let QPath::Resolved(_, path) = *path
292292
&& let [ty] = path.segments
293293
{
294-
let name = ty.ident.name.as_str();
295-
INTS.iter().find(|c| &name == *c).copied()
294+
INTS.iter().find(|c| ty.ident.name == **c).copied()
296295
} else {
297296
None
298297
}
299298
}
300299

301300
// Constants
302-
const UINTS: &[&str] = &["u8", "u16", "u32", "u64", "usize"];
303-
const SINTS: &[&str] = &["i8", "i16", "i32", "i64", "isize"];
304-
const INTS: &[&str] = &["u8", "u16", "u32", "u64", "usize", "i8", "i16", "i32", "i64", "isize"];
301+
const UINTS: &[Symbol] = &[sym::u8, sym::u16, sym::u32, sym::u64, sym::usize];
302+
const SINTS: &[Symbol] = &[sym::i8, sym::i16, sym::i32, sym::i64, sym::isize];
303+
const INTS: &[Symbol] = &[
304+
sym::u8,
305+
sym::u16,
306+
sym::u32,
307+
sym::u64,
308+
sym::usize,
309+
sym::i8,
310+
sym::i16,
311+
sym::i32,
312+
sym::i64,
313+
sym::isize,
314+
];

clippy_lints/src/endian_bytes.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::Lint;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::is_lint_allowed;
3+
use clippy_utils::{is_lint_allowed, sym};
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass, LintContext};
66
use rustc_middle::ty::Ty;
@@ -65,9 +65,9 @@ declare_clippy_lint! {
6565

6666
declare_lint_pass!(EndianBytes => [HOST_ENDIAN_BYTES, LITTLE_ENDIAN_BYTES, BIG_ENDIAN_BYTES]);
6767

68-
const HOST_NAMES: [&str; 2] = ["from_ne_bytes", "to_ne_bytes"];
69-
const LITTLE_NAMES: [&str; 2] = ["from_le_bytes", "to_le_bytes"];
70-
const BIG_NAMES: [&str; 2] = ["from_be_bytes", "to_be_bytes"];
68+
const HOST_NAMES: [Symbol; 2] = [sym::from_ne_bytes, sym::to_ne_bytes];
69+
const LITTLE_NAMES: [Symbol; 2] = [sym::from_le_bytes, sym::to_le_bytes];
70+
const BIG_NAMES: [Symbol; 2] = [sym::from_be_bytes, sym::to_be_bytes];
7171

7272
#[derive(Clone, Debug)]
7373
enum LintKind {
@@ -95,7 +95,7 @@ impl LintKind {
9595
}
9696
}
9797

98-
fn as_name(&self, prefix: Prefix) -> &str {
98+
fn as_name(&self, prefix: Prefix) -> Symbol {
9999
let index = usize::from(prefix == Prefix::To);
100100

101101
match self {
@@ -133,7 +133,7 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix
133133
let le = LintKind::Little.as_name(prefix);
134134
let be = LintKind::Big.as_name(prefix);
135135

136-
let (lint, other_lints) = match name.as_str() {
136+
let (lint, other_lints) = match name {
137137
name if name == ne => ((&LintKind::Host), [(&LintKind::Little), (&LintKind::Big)]),
138138
name if name == le => ((&LintKind::Little), [(&LintKind::Host), (&LintKind::Big)]),
139139
name if name == be => ((&LintKind::Big), [(&LintKind::Host), (&LintKind::Little)]),

clippy_utils/src/sym.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,11 @@ generate! {
150150
floor_char_boundary,
151151
fold,
152152
for_each,
153+
from_be_bytes,
153154
from_bytes_with_nul,
154155
from_bytes_with_nul_unchecked,
156+
from_le_bytes,
157+
from_ne_bytes,
155158
from_ptr,
156159
from_raw,
157160
from_ref,
@@ -184,8 +187,10 @@ generate! {
184187
is_err,
185188
is_file,
186189
is_none,
190+
is_none_or,
187191
is_ok,
188192
is_some,
193+
is_some_and,
189194
isqrt,
190195
itertools,
191196
join,
@@ -323,8 +328,11 @@ generate! {
323328
then_some,
324329
to_ascii_lowercase,
325330
to_ascii_uppercase,
331+
to_be_bytes,
326332
to_digit,
333+
to_le_bytes,
327334
to_lowercase,
335+
to_ne_bytes,
328336
to_os_string,
329337
to_owned,
330338
to_path_buf,

0 commit comments

Comments
 (0)