Skip to content

Commit db6ea84

Browse files
committed
Auto merge of #7046 - camsteffen:symbol-optimize, r=giraffate
Some symbol optimizations changelog: none
2 parents 624e8aa + 47f0c15 commit db6ea84

File tree

7 files changed

+67
-82
lines changed

7 files changed

+67
-82
lines changed

clippy_lints/src/attrs.rs

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,8 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
250250
fn check_attribute(&mut self, cx: &LateContext<'tcx>, attr: &'tcx Attribute) {
251251
if let Some(items) = &attr.meta_item_list() {
252252
if let Some(ident) = attr.ident() {
253-
let ident = &*ident.as_str();
254-
match ident {
255-
"allow" | "warn" | "deny" | "forbid" => {
256-
check_clippy_lint_names(cx, ident, items);
257-
},
258-
_ => {},
253+
if is_lint_level(ident.name) {
254+
check_clippy_lint_names(cx, ident.name, items);
259255
}
260256
if items.is_empty() || !attr.has_name(sym::deprecated) {
261257
return;
@@ -288,60 +284,54 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
288284
return;
289285
}
290286
if let Some(lint_list) = &attr.meta_item_list() {
291-
if let Some(ident) = attr.ident() {
292-
match &*ident.as_str() {
293-
"allow" | "warn" | "deny" | "forbid" => {
294-
// permit `unused_imports`, `deprecated`, `unreachable_pub`,
295-
// `clippy::wildcard_imports`, and `clippy::enum_glob_use` for `use` items
296-
// and `unused_imports` for `extern crate` items with `macro_use`
297-
for lint in lint_list {
298-
match item.kind {
299-
ItemKind::Use(..) => {
300-
if is_word(lint, sym!(unused_imports))
301-
|| is_word(lint, sym::deprecated)
302-
|| is_word(lint, sym!(unreachable_pub))
303-
|| is_word(lint, sym!(unused))
304-
|| extract_clippy_lint(lint)
305-
.map_or(false, |s| s == "wildcard_imports")
306-
|| extract_clippy_lint(lint).map_or(false, |s| s == "enum_glob_use")
307-
{
308-
return;
309-
}
310-
},
311-
ItemKind::ExternCrate(..) => {
312-
if is_word(lint, sym!(unused_imports)) && skip_unused_imports {
313-
return;
314-
}
315-
if is_word(lint, sym!(unused_extern_crates)) {
316-
return;
317-
}
318-
},
319-
_ => {},
287+
if attr.ident().map_or(false, |ident| is_lint_level(ident.name)) {
288+
// permit `unused_imports`, `deprecated`, `unreachable_pub`,
289+
// `clippy::wildcard_imports`, and `clippy::enum_glob_use` for `use` items
290+
// and `unused_imports` for `extern crate` items with `macro_use`
291+
for lint in lint_list {
292+
match item.kind {
293+
ItemKind::Use(..) => {
294+
if is_word(lint, sym!(unused_imports))
295+
|| is_word(lint, sym::deprecated)
296+
|| is_word(lint, sym!(unreachable_pub))
297+
|| is_word(lint, sym!(unused))
298+
|| extract_clippy_lint(lint).map_or(false, |s| s == "wildcard_imports")
299+
|| extract_clippy_lint(lint).map_or(false, |s| s == "enum_glob_use")
300+
{
301+
return;
302+
}
303+
},
304+
ItemKind::ExternCrate(..) => {
305+
if is_word(lint, sym!(unused_imports)) && skip_unused_imports {
306+
return;
320307
}
321-
}
322-
let line_span = first_line_of_span(cx, attr.span);
323-
324-
if let Some(mut sugg) = snippet_opt(cx, line_span) {
325-
if sugg.contains("#[") {
326-
span_lint_and_then(
327-
cx,
328-
USELESS_ATTRIBUTE,
308+
if is_word(lint, sym!(unused_extern_crates)) {
309+
return;
310+
}
311+
},
312+
_ => {},
313+
}
314+
}
315+
let line_span = first_line_of_span(cx, attr.span);
316+
317+
if let Some(mut sugg) = snippet_opt(cx, line_span) {
318+
if sugg.contains("#[") {
319+
span_lint_and_then(
320+
cx,
321+
USELESS_ATTRIBUTE,
322+
line_span,
323+
"useless lint attribute",
324+
|diag| {
325+
sugg = sugg.replacen("#[", "#![", 1);
326+
diag.span_suggestion(
329327
line_span,
330-
"useless lint attribute",
331-
|diag| {
332-
sugg = sugg.replacen("#[", "#![", 1);
333-
diag.span_suggestion(
334-
line_span,
335-
"if you just forgot a `!`, use",
336-
sugg,
337-
Applicability::MaybeIncorrect,
338-
);
339-
},
328+
"if you just forgot a `!`, use",
329+
sugg,
330+
Applicability::MaybeIncorrect,
340331
);
341-
}
342-
}
343-
},
344-
_ => {},
332+
},
333+
);
334+
}
345335
}
346336
}
347337
}
@@ -379,10 +369,10 @@ fn extract_clippy_lint(lint: &NestedMetaItem) -> Option<SymbolStr> {
379369
None
380370
}
381371

382-
fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMetaItem]) {
372+
fn check_clippy_lint_names(cx: &LateContext<'_>, name: Symbol, items: &[NestedMetaItem]) {
383373
for lint in items {
384374
if let Some(lint_name) = extract_clippy_lint(lint) {
385-
if lint_name == "restriction" && ident != "allow" {
375+
if lint_name == "restriction" && name != sym::allow {
386376
span_lint_and_help(
387377
cx,
388378
BLANKET_CLIPPY_RESTRICTION_LINTS,
@@ -647,3 +637,7 @@ fn check_mismatched_target_os(cx: &EarlyContext<'_>, attr: &Attribute) {
647637
}
648638
}
649639
}
640+
641+
fn is_lint_level(symbol: Symbol) -> bool {
642+
matches!(symbol, sym::allow | sym::warn | sym::deny | sym::forbid)
643+
}

clippy_lints/src/excessive_bools.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::{attr_by_name, in_macro, match_path_ast};
2+
use clippy_utils::{in_macro, match_path_ast};
33
use rustc_ast::ast::{AssocItemKind, Extern, FnKind, FnSig, ImplKind, Item, ItemKind, TraitKind, Ty, TyKind};
44
use rustc_lint::{EarlyContext, EarlyLintPass};
55
use rustc_session::{declare_tool_lint, impl_lint_pass};
6-
use rustc_span::Span;
6+
use rustc_span::{sym, Span};
77

88
use std::convert::TryInto;
99

@@ -138,7 +138,7 @@ impl EarlyLintPass for ExcessiveBools {
138138
}
139139
match &item.kind {
140140
ItemKind::Struct(variant_data, _) => {
141-
if attr_by_name(&item.attrs, "repr").is_some() {
141+
if item.attrs.iter().any(|attr| attr.has_name(sym::repr)) {
142142
return;
143143
}
144144

clippy_lints/src/functions/must_use.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use rustc_middle::{
88
lint::in_external_macro,
99
ty::{self, Ty},
1010
};
11-
use rustc_span::Span;
11+
use rustc_span::{sym, Span};
1212

1313
use clippy_utils::attrs::is_proc_macro;
1414
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
1515
use clippy_utils::source::snippet_opt;
1616
use clippy_utils::ty::is_must_use_ty;
17-
use clippy_utils::{attr_by_name, match_def_path, must_use_attr, return_ty, trait_ref_of_method};
17+
use clippy_utils::{match_def_path, must_use_attr, return_ty, trait_ref_of_method};
1818

1919
use super::{DOUBLE_MUST_USE, MUST_USE_CANDIDATE, MUST_USE_UNIT};
2020

@@ -27,7 +27,7 @@ pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
2727
if let Some(attr) = attr {
2828
check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr);
2929
return;
30-
} else if is_public && !is_proc_macro(cx.sess(), attrs) && attr_by_name(attrs, "no_mangle").is_none() {
30+
} else if is_public && !is_proc_macro(cx.sess(), attrs) && !attrs.iter().any(|a| a.has_name(sym::no_mangle)) {
3131
check_must_use_candidate(
3232
cx,
3333
sig.decl,

clippy_lints/src/macro_use.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_errors::Applicability;
99
use rustc_hir as hir;
1010
use rustc_lint::{LateContext, LateLintPass, LintContext};
1111
use rustc_session::{declare_tool_lint, impl_lint_pass};
12-
use rustc_span::{edition::Edition, Span};
12+
use rustc_span::{edition::Edition, sym, Span};
1313

1414
declare_clippy_lint! {
1515
/// **What it does:** Checks for `#[macro_use] use...`.
@@ -110,9 +110,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
110110
if cx.sess().opts.edition >= Edition::Edition2018;
111111
if let hir::ItemKind::Use(path, _kind) = &item.kind;
112112
let attrs = cx.tcx.hir().attrs(item.hir_id());
113-
if let Some(mac_attr) = attrs
114-
.iter()
115-
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
113+
if let Some(mac_attr) = attrs.iter().find(|attr| attr.has_name(sym::macro_use));
116114
if let Res::Def(DefKind::Mod, id) = path.res;
117115
then {
118116
for kid in cx.tcx.item_children(id).iter() {

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::{BlockCheckMode, UnsafeSource};
1010
use rustc_lint::LateContext;
1111
use rustc_middle::ty;
1212
use rustc_span::source_map::Span;
13-
use rustc_span::symbol::sym;
13+
use rustc_span::symbol::{kw, sym};
1414
use std::borrow::Cow;
1515

1616
use super::OR_FUN_CALL;
@@ -38,8 +38,8 @@ pub(super) fn check<'tcx>(
3838
if !or_has_args;
3939
if name == "unwrap_or";
4040
if let hir::ExprKind::Path(ref qpath) = fun.kind;
41-
let path = &*last_path_segment(qpath).ident.as_str();
42-
if ["default", "new"].contains(&path);
41+
let path = last_path_segment(qpath).ident.name;
42+
if matches!(path, kw::Default | sym::new);
4343
let arg_ty = cx.typeck_results().expr_ty(arg);
4444
if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
4545
if implements_trait(cx, arg_ty, default_trait_id, &[]);

clippy_lints/src/option_env_unwrap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use if_chain::if_chain;
44
use rustc_ast::ast::{Expr, ExprKind};
55
use rustc_lint::{EarlyContext, EarlyLintPass};
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 usage of `option_env!(...).unwrap()` and
@@ -37,8 +38,7 @@ impl EarlyLintPass for OptionEnvUnwrap {
3738
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
3839
if_chain! {
3940
if let ExprKind::MethodCall(path_segment, args, _) = &expr.kind;
40-
let method_name = path_segment.ident.as_str();
41-
if method_name == "expect" || method_name == "unwrap";
41+
if matches!(path_segment.ident.name, sym::expect | sym::unwrap);
4242
if let ExprKind::Call(caller, _) = &args[0].kind;
4343
if is_direct_expn_of(caller.span, "option_env").is_some();
4444
then {

clippy_utils/src/lib.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,16 +1203,9 @@ pub fn parent_node_is_if_expr(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
12031203
)
12041204
}
12051205

1206-
// Finds the attribute with the given name, if any
1207-
pub fn attr_by_name<'a>(attrs: &'a [Attribute], name: &'_ str) -> Option<&'a Attribute> {
1208-
attrs
1209-
.iter()
1210-
.find(|attr| attr.ident().map_or(false, |ident| ident.as_str() == name))
1211-
}
1212-
12131206
// Finds the `#[must_use]` attribute, if any
12141207
pub fn must_use_attr(attrs: &[Attribute]) -> Option<&Attribute> {
1215-
attr_by_name(attrs, "must_use")
1208+
attrs.iter().find(|a| a.has_name(sym::must_use))
12161209
}
12171210

12181211
// check if expr is calling method or function with #[must_use] attribute

0 commit comments

Comments
 (0)