Skip to content

Commit c2cf40c

Browse files
committed
Auto merge of #6284 - camsteffen:rustc-sym, r=flip1995
Use const sym where possible I ran a regex search and replace to use const `sym` values where possible. This should give some performance boost by avoiding string interning at runtime. Con: It is not as consistent as always using `sym!`. I also changed an internal lint to suggest using `sym::{}`, making an assumption that this will always work for diagnostic items. changelog: none
2 parents 3a34bc0 + b2332a7 commit c2cf40c

Some content is hidden

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

54 files changed

+198
-164
lines changed

clippy_lints/src/attrs.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_middle::lint::in_external_macro;
1616
use rustc_middle::ty;
1717
use rustc_session::{declare_lint_pass, declare_tool_lint};
1818
use rustc_span::source_map::Span;
19+
use rustc_span::sym;
1920
use rustc_span::symbol::{Symbol, SymbolStr};
2021
use semver::Version;
2122

@@ -286,14 +287,14 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
286287
},
287288
_ => {},
288289
}
289-
if items.is_empty() || !attr.has_name(sym!(deprecated)) {
290+
if items.is_empty() || !attr.has_name(sym::deprecated) {
290291
return;
291292
}
292293
for item in items {
293294
if_chain! {
294295
if let NestedMetaItem::MetaItem(mi) = &item;
295296
if let MetaItemKind::NameValue(lit) = &mi.kind;
296-
if mi.has_name(sym!(since));
297+
if mi.has_name(sym::since);
297298
then {
298299
check_semver(cx, item.span(), lit);
299300
}
@@ -309,7 +310,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
309310
}
310311
match item.kind {
311312
ItemKind::ExternCrate(..) | ItemKind::Use(..) => {
312-
let skip_unused_imports = item.attrs.iter().any(|attr| attr.has_name(sym!(macro_use)));
313+
let skip_unused_imports = item.attrs.iter().any(|attr| attr.has_name(sym::macro_use));
313314

314315
for attr in item.attrs {
315316
if in_external_macro(cx.sess(), attr.span) {
@@ -326,7 +327,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
326327
match item.kind {
327328
ItemKind::Use(..) => {
328329
if is_word(lint, sym!(unused_imports))
329-
|| is_word(lint, sym!(deprecated))
330+
|| is_word(lint, sym::deprecated)
330331
|| is_word(lint, sym!(unreachable_pub))
331332
|| is_word(lint, sym!(unused))
332333
|| extract_clippy_lint(lint)
@@ -411,8 +412,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMet
411412
let lint_store = cx.lints();
412413
for lint in items {
413414
if let Some(lint_name) = extract_clippy_lint(lint) {
414-
if let CheckLintNameResult::Tool(Err((None, _))) =
415-
lint_store.check_lint_name(&lint_name, Some(sym!(clippy)))
415+
if let CheckLintNameResult::Tool(Err((None, _))) = lint_store.check_lint_name(&lint_name, Some(sym::clippy))
416416
{
417417
span_lint_and_then(
418418
cx,
@@ -529,10 +529,10 @@ fn check_attrs(cx: &LateContext<'_>, span: Span, name: Symbol, attrs: &[Attribut
529529

530530
for attr in attrs {
531531
if let Some(values) = attr.meta_item_list() {
532-
if values.len() != 1 || !attr.has_name(sym!(inline)) {
532+
if values.len() != 1 || !attr.has_name(sym::inline) {
533533
continue;
534534
}
535-
if is_word(&values[0], sym!(always)) {
535+
if is_word(&values[0], sym::always) {
536536
span_lint(
537537
cx,
538538
INLINE_ALWAYS,
@@ -623,12 +623,12 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
623623
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute) {
624624
if_chain! {
625625
// check cfg_attr
626-
if attr.has_name(sym!(cfg_attr));
626+
if attr.has_name(sym::cfg_attr);
627627
if let Some(items) = attr.meta_item_list();
628628
if items.len() == 2;
629629
// check for `rustfmt`
630630
if let Some(feature_item) = items[0].meta_item();
631-
if feature_item.has_name(sym!(rustfmt));
631+
if feature_item.has_name(sym::rustfmt);
632632
// check for `rustfmt_skip` and `rustfmt::skip`
633633
if let Some(skip_item) = &items[1].meta_item();
634634
if skip_item.has_name(sym!(rustfmt_skip)) ||
@@ -690,7 +690,7 @@ fn check_mismatched_target_os(cx: &EarlyContext<'_>, attr: &Attribute) {
690690
}
691691

692692
if_chain! {
693-
if attr.has_name(sym!(cfg));
693+
if attr.has_name(sym::cfg);
694694
if let Some(list) = attr.meta_item_list();
695695
let mismatched = find_mismatched_target_os(&list);
696696
if !mismatched.is_empty();

clippy_lints/src/booleans.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_middle::hir::map::Map;
1212
use rustc_session::{declare_lint_pass, declare_tool_lint};
1313
use rustc_span::source_map::Span;
14+
use rustc_span::sym;
1415

1516
declare_clippy_lint! {
1617
/// **What it does:** Checks for boolean expressions that can be written more
@@ -253,8 +254,8 @@ fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
253254
},
254255
ExprKind::MethodCall(path, _, args, _) if args.len() == 1 => {
255256
let type_of_receiver = cx.typeck_results().expr_ty(&args[0]);
256-
if !is_type_diagnostic_item(cx, type_of_receiver, sym!(option_type))
257-
&& !is_type_diagnostic_item(cx, type_of_receiver, sym!(result_type))
257+
if !is_type_diagnostic_item(cx, type_of_receiver, sym::option_type)
258+
&& !is_type_diagnostic_item(cx, type_of_receiver, sym::result_type)
258259
{
259260
return None;
260261
}

clippy_lints/src/bytecount.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, UnOp};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::ty;
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
11+
use rustc_span::sym;
1112
use rustc_span::Symbol;
1213

1314
declare_clippy_lint! {
@@ -68,7 +69,7 @@ impl<'tcx> LateLintPass<'tcx> for ByteCount {
6869
let haystack = if let ExprKind::MethodCall(ref path, _, ref args, _) =
6970
filter_args[0].kind {
7071
let p = path.ident.name;
71-
if (p == sym!(iter) || p == sym!(iter_mut)) && args.len() == 1 {
72+
if (p == sym::iter || p == sym!(iter_mut)) && args.len() == 1 {
7273
&args[0]
7374
} else {
7475
&filter_args[0]

clippy_lints/src/cognitive_complexity.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_lint::{LateContext, LateLintPass, LintContext};
77
use rustc_middle::hir::map::Map;
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::source_map::Span;
10-
use rustc_span::BytePos;
10+
use rustc_span::{sym, BytePos};
1111

1212
use crate::utils::{is_type_diagnostic_item, snippet_opt, span_lint_and_help, LimitStack};
1313

@@ -61,7 +61,7 @@ impl CognitiveComplexity {
6161
helper.visit_expr(expr);
6262
let CCHelper { cc, returns } = helper;
6363
let ret_ty = cx.typeck_results().node_type(expr.hir_id);
64-
let ret_adjust = if is_type_diagnostic_item(cx, ret_ty, sym!(result_type)) {
64+
let ret_adjust = if is_type_diagnostic_item(cx, ret_ty, sym::result_type) {
6565
returns
6666
} else {
6767
#[allow(clippy::integer_division)]
@@ -123,7 +123,7 @@ impl<'tcx> LateLintPass<'tcx> for CognitiveComplexity {
123123
hir_id: HirId,
124124
) {
125125
let def_id = cx.tcx.hir().local_def_id(hir_id);
126-
if !cx.tcx.has_attr(def_id.to_def_id(), sym!(test)) {
126+
if !cx.tcx.has_attr(def_id.to_def_id(), sym::test) {
127127
self.check(cx, kind, decl, body, span);
128128
}
129129
}

clippy_lints/src/doc.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_parse::maybe_new_parser_from_source_str;
1515
use rustc_session::parse::ParseSess;
1616
use rustc_session::{declare_tool_lint, impl_lint_pass};
1717
use rustc_span::source_map::{BytePos, FilePathMapping, MultiSpan, SourceMap, Span};
18-
use rustc_span::{FileName, Pos};
18+
use rustc_span::{sym, FileName, Pos};
1919
use std::io;
2020
use std::ops::Range;
2121
use url::Url;
@@ -237,7 +237,7 @@ fn lint_for_missing_headers<'tcx>(
237237
);
238238
}
239239
if !headers.errors {
240-
if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type)) {
240+
if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type) {
241241
span_lint(
242242
cx,
243243
MISSING_ERRORS_DOC,
@@ -255,7 +255,7 @@ fn lint_for_missing_headers<'tcx>(
255255
if let ty::Opaque(_, subs) = ret_ty.kind();
256256
if let Some(gen) = subs.types().next();
257257
if let ty::Generator(_, subs, _) = gen.kind();
258-
if is_type_diagnostic_item(cx, subs.as_generator().return_ty(), sym!(result_type));
258+
if is_type_diagnostic_item(cx, subs.as_generator().return_ty(), sym::result_type);
259259
then {
260260
span_lint(
261261
cx,
@@ -333,7 +333,7 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs
333333
let (comment, current_spans) = strip_doc_comment_decoration(&comment.as_str(), comment_kind, attr.span);
334334
spans.extend_from_slice(&current_spans);
335335
doc.push_str(&comment);
336-
} else if attr.has_name(sym!(doc)) {
336+
} else if attr.has_name(sym::doc) {
337337
// ignore mix of sugared and non-sugared doc
338338
// don't trigger the safety or errors check
339339
return DocHeaders {
@@ -479,7 +479,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, span: Span) {
479479
| ItemKind::ExternCrate(..)
480480
| ItemKind::ForeignMod(..) => return false,
481481
// We found a main function ...
482-
ItemKind::Fn(_, sig, _, Some(block)) if item.ident.name == sym!(main) => {
482+
ItemKind::Fn(_, sig, _, Some(block)) if item.ident.name == sym::main => {
483483
let is_async = matches!(sig.header.asyncness, Async::Yes{..});
484484
let returns_nothing = match &sig.decl.output {
485485
FnRetTy::Default(..) => true,

clippy_lints/src/explicit_write.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_errors::Applicability;
55
use rustc_hir::{BorrowKind, Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_span::sym;
89

910
declare_clippy_lint! {
1011
/// **What it does:** Checks for usage of `write!()` / `writeln()!` which can be
@@ -33,7 +34,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
3334
if_chain! {
3435
// match call to unwrap
3536
if let ExprKind::MethodCall(ref unwrap_fun, _, ref unwrap_args, _) = expr.kind;
36-
if unwrap_fun.ident.name == sym!(unwrap);
37+
if unwrap_fun.ident.name == sym::unwrap;
3738
// match call to write_fmt
3839
if !unwrap_args.is_empty();
3940
if let ExprKind::MethodCall(ref write_fun, _, write_args, _) =

clippy_lints/src/fallible_impl_from.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::hir::map::Map;
77
use rustc_middle::ty;
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
9-
use rustc_span::Span;
9+
use rustc_span::{sym, Span};
1010

1111
declare_clippy_lint! {
1212
/// **What it does:** Checks for impls of `From<..>` that contain `panic!()` or `unwrap()`
@@ -95,8 +95,8 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_items: &[h
9595
// check for `unwrap`
9696
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
9797
let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
98-
if is_type_diagnostic_item(self.lcx, reciever_ty, sym!(option_type))
99-
|| is_type_diagnostic_item(self.lcx, reciever_ty, sym!(result_type))
98+
if is_type_diagnostic_item(self.lcx, reciever_ty, sym::option_type)
99+
|| is_type_diagnostic_item(self.lcx, reciever_ty, sym::result_type)
100100
{
101101
self.result.push(expr.span);
102102
}
@@ -113,7 +113,7 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_items: &[h
113113

114114
for impl_item in impl_items {
115115
if_chain! {
116-
if impl_item.ident.name == sym!(from);
116+
if impl_item.ident.name == sym::from;
117117
if let ImplItemKind::Fn(_, body_id) =
118118
cx.tcx.hir().impl_item(impl_item.id).kind;
119119
then {

clippy_lints/src/format.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, MatchSource, PatKind};
1010
use rustc_lint::{LateContext, LateLintPass, LintContext};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
1212
use rustc_span::source_map::Span;
13+
use rustc_span::sym;
1314

1415
declare_clippy_lint! {
1516
/// **What it does:** Checks for the use of `format!("string literal with no
@@ -91,7 +92,7 @@ fn on_argumentv1_new<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arms: &
9192
if pats.len() == 1;
9293
then {
9394
let ty = cx.typeck_results().pat_ty(&pats[0]).peel_refs();
94-
if *ty.kind() != rustc_middle::ty::Str && !is_type_diagnostic_item(cx, ty, sym!(string_type)) {
95+
if *ty.kind() != rustc_middle::ty::Str && !is_type_diagnostic_item(cx, ty, sym::string_type) {
9596
return None;
9697
}
9798
if let ExprKind::Lit(ref lit) = format_args.kind {
@@ -186,15 +187,15 @@ fn check_unformatted(expr: &Expr<'_>) -> bool {
186187
if exprs.len() == 1;
187188
// struct `core::fmt::rt::v1::Argument`
188189
if let ExprKind::Struct(_, ref fields, _) = exprs[0].kind;
189-
if let Some(format_field) = fields.iter().find(|f| f.ident.name == sym!(format));
190+
if let Some(format_field) = fields.iter().find(|f| f.ident.name == sym::format);
190191
// struct `core::fmt::rt::v1::FormatSpec`
191192
if let ExprKind::Struct(_, ref fields, _) = format_field.expr.kind;
192-
if let Some(precision_field) = fields.iter().find(|f| f.ident.name == sym!(precision));
193+
if let Some(precision_field) = fields.iter().find(|f| f.ident.name == sym::precision);
193194
if let ExprKind::Path(ref precision_path) = precision_field.expr.kind;
194-
if last_path_segment(precision_path).ident.name == sym!(Implied);
195-
if let Some(width_field) = fields.iter().find(|f| f.ident.name == sym!(width));
195+
if last_path_segment(precision_path).ident.name == sym::Implied;
196+
if let Some(width_field) = fields.iter().find(|f| f.ident.name == sym::width);
196197
if let ExprKind::Path(ref width_qpath) = width_field.expr.kind;
197-
if last_path_segment(width_qpath).ident.name == sym!(Implied);
198+
if last_path_segment(width_qpath).ident.name == sym::Implied;
198199
then {
199200
return true;
200201
}

clippy_lints/src/functions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_middle::lint::in_external_macro;
1616
use rustc_middle::ty::{self, Ty};
1717
use rustc_session::{declare_tool_lint, impl_lint_pass};
1818
use rustc_span::source_map::Span;
19+
use rustc_span::sym;
1920
use rustc_target::spec::abi::Abi;
2021
use rustc_typeck::hir_ty_to_ty;
2122

@@ -473,7 +474,7 @@ fn check_result_unit_err(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, item_span
473474
if !in_external_macro(cx.sess(), item_span);
474475
if let hir::FnRetTy::Return(ref ty) = decl.output;
475476
if let hir::TyKind::Path(ref qpath) = ty.kind;
476-
if is_type_diagnostic_item(cx, hir_ty_to_ty(cx.tcx, ty), sym!(result_type));
477+
if is_type_diagnostic_item(cx, hir_ty_to_ty(cx.tcx, ty), sym::result_type);
477478
if let Some(ref args) = last_path_segment(qpath).args;
478479
if let [_, hir::GenericArg::Type(ref err_ty)] = args.args;
479480
if let hir::TyKind::Tup(t) = err_ty.kind;

clippy_lints/src/get_last_with_len.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::{BinOpKind, Expr, ExprKind};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
1010
use rustc_span::source_map::Spanned;
11+
use rustc_span::sym;
1112

1213
declare_clippy_lint! {
1314
/// **What it does:** Checks for using `x.get(x.len() - 1)` instead of
@@ -55,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for GetLastWithLen {
5556
// Argument 0 (the struct we're calling the method on) is a vector
5657
if let Some(struct_calling_on) = args.get(0);
5758
let struct_ty = cx.typeck_results().expr_ty(struct_calling_on);
58-
if is_type_diagnostic_item(cx, struct_ty, sym!(vec_type));
59+
if is_type_diagnostic_item(cx, struct_ty, sym::vec_type);
5960

6061
// Argument to "get" is a subtraction
6162
if let Some(get_index_arg) = args.get(1);

clippy_lints/src/if_let_some_result.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_errors::Applicability;
44
use rustc_hir::{Expr, ExprKind, MatchSource, PatKind, QPath};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::sym;
78

89
declare_clippy_lint! {
910
/// **What it does:*** Checks for unnecessary `ok()` in if let.
@@ -45,7 +46,7 @@ impl<'tcx> LateLintPass<'tcx> for OkIfLet {
4546
if let ExprKind::MethodCall(_, ok_span, ref result_types, _) = op.kind; //check is expr.ok() has type Result<T,E>.ok(, _)
4647
if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pat.kind; //get operation
4748
if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
48-
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&result_types[0]), sym!(result_type));
49+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&result_types[0]), sym::result_type);
4950
if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some";
5051

5152
then {

clippy_lints/src/inherent_to_string.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use if_chain::if_chain;
22
use rustc_hir::{ImplItem, ImplItemKind};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
use rustc_span::sym;
56

67
use crate::utils::{
78
get_trait_def_id, implements_trait, is_type_diagnostic_item, paths, return_ty, span_lint_and_help,
@@ -107,7 +108,7 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString {
107108
if decl.inputs.len() == 1;
108109

109110
// Check if return type is String
110-
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym!(string_type));
111+
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym::string_type);
111112

112113
// Filters instances of to_string which are required by a trait
113114
if trait_ref_of_method(cx, impl_item.hir_id).is_none();

clippy_lints/src/inline_fn_without_body.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_errors::Applicability;
77
use rustc_hir::{TraitFn, TraitItem, TraitItemKind};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
10-
use rustc_span::Symbol;
10+
use rustc_span::{sym, Symbol};
1111

1212
declare_clippy_lint! {
1313
/// **What it does:** Checks for `#[inline]` on trait methods without bodies
@@ -41,7 +41,7 @@ impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody {
4141

4242
fn check_attrs(cx: &LateContext<'_>, name: Symbol, attrs: &[Attribute]) {
4343
for attr in attrs {
44-
if !attr.has_name(sym!(inline)) {
44+
if !attr.has_name(sym::inline) {
4545
continue;
4646
}
4747

0 commit comments

Comments
 (0)