mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-15 09:27:25 +00:00
Symbol optimizations
This commit is contained in:
parent
624e8aad32
commit
47f0c15f67
7 changed files with 66 additions and 81 deletions
|
@ -250,12 +250,8 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
|
|||
fn check_attribute(&mut self, cx: &LateContext<'tcx>, attr: &'tcx Attribute) {
|
||||
if let Some(items) = &attr.meta_item_list() {
|
||||
if let Some(ident) = attr.ident() {
|
||||
let ident = &*ident.as_str();
|
||||
match ident {
|
||||
"allow" | "warn" | "deny" | "forbid" => {
|
||||
check_clippy_lint_names(cx, ident, items);
|
||||
},
|
||||
_ => {},
|
||||
if is_lint_level(ident.name) {
|
||||
check_clippy_lint_names(cx, ident.name, items);
|
||||
}
|
||||
if items.is_empty() || !attr.has_name(sym::deprecated) {
|
||||
return;
|
||||
|
@ -288,9 +284,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
|
|||
return;
|
||||
}
|
||||
if let Some(lint_list) = &attr.meta_item_list() {
|
||||
if let Some(ident) = attr.ident() {
|
||||
match &*ident.as_str() {
|
||||
"allow" | "warn" | "deny" | "forbid" => {
|
||||
if attr.ident().map_or(false, |ident| is_lint_level(ident.name)) {
|
||||
// permit `unused_imports`, `deprecated`, `unreachable_pub`,
|
||||
// `clippy::wildcard_imports`, and `clippy::enum_glob_use` for `use` items
|
||||
// and `unused_imports` for `extern crate` items with `macro_use`
|
||||
|
@ -301,8 +295,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
|
|||
|| is_word(lint, sym::deprecated)
|
||||
|| is_word(lint, sym!(unreachable_pub))
|
||||
|| is_word(lint, sym!(unused))
|
||||
|| extract_clippy_lint(lint)
|
||||
.map_or(false, |s| s == "wildcard_imports")
|
||||
|| extract_clippy_lint(lint).map_or(false, |s| s == "wildcard_imports")
|
||||
|| extract_clippy_lint(lint).map_or(false, |s| s == "enum_glob_use")
|
||||
{
|
||||
return;
|
||||
|
@ -340,9 +333,6 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
|
|||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -379,10 +369,10 @@ fn extract_clippy_lint(lint: &NestedMetaItem) -> Option<SymbolStr> {
|
|||
None
|
||||
}
|
||||
|
||||
fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMetaItem]) {
|
||||
fn check_clippy_lint_names(cx: &LateContext<'_>, name: Symbol, items: &[NestedMetaItem]) {
|
||||
for lint in items {
|
||||
if let Some(lint_name) = extract_clippy_lint(lint) {
|
||||
if lint_name == "restriction" && ident != "allow" {
|
||||
if lint_name == "restriction" && name != sym::allow {
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
BLANKET_CLIPPY_RESTRICTION_LINTS,
|
||||
|
@ -647,3 +637,7 @@ fn check_mismatched_target_os(cx: &EarlyContext<'_>, attr: &Attribute) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_lint_level(symbol: Symbol) -> bool {
|
||||
matches!(symbol, sym::allow | sym::warn | sym::deny | sym::forbid)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_help;
|
||||
use clippy_utils::{attr_by_name, in_macro, match_path_ast};
|
||||
use clippy_utils::{in_macro, match_path_ast};
|
||||
use rustc_ast::ast::{AssocItemKind, Extern, FnKind, FnSig, ImplKind, Item, ItemKind, TraitKind, Ty, TyKind};
|
||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
use rustc_span::Span;
|
||||
use rustc_span::{sym, Span};
|
||||
|
||||
use std::convert::TryInto;
|
||||
|
||||
|
@ -138,7 +138,7 @@ impl EarlyLintPass for ExcessiveBools {
|
|||
}
|
||||
match &item.kind {
|
||||
ItemKind::Struct(variant_data, _) => {
|
||||
if attr_by_name(&item.attrs, "repr").is_some() {
|
||||
if item.attrs.iter().any(|attr| attr.has_name(sym::repr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,13 +8,13 @@ use rustc_middle::{
|
|||
lint::in_external_macro,
|
||||
ty::{self, Ty},
|
||||
};
|
||||
use rustc_span::Span;
|
||||
use rustc_span::{sym, Span};
|
||||
|
||||
use clippy_utils::attrs::is_proc_macro;
|
||||
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
|
||||
use clippy_utils::source::snippet_opt;
|
||||
use clippy_utils::ty::is_must_use_ty;
|
||||
use clippy_utils::{attr_by_name, match_def_path, must_use_attr, return_ty, trait_ref_of_method};
|
||||
use clippy_utils::{match_def_path, must_use_attr, return_ty, trait_ref_of_method};
|
||||
|
||||
use super::{DOUBLE_MUST_USE, MUST_USE_CANDIDATE, MUST_USE_UNIT};
|
||||
|
||||
|
@ -27,7 +27,7 @@ pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
|||
if let Some(attr) = attr {
|
||||
check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr);
|
||||
return;
|
||||
} else if is_public && !is_proc_macro(cx.sess(), attrs) && attr_by_name(attrs, "no_mangle").is_none() {
|
||||
} else if is_public && !is_proc_macro(cx.sess(), attrs) && !attrs.iter().any(|a| a.has_name(sym::no_mangle)) {
|
||||
check_must_use_candidate(
|
||||
cx,
|
||||
sig.decl,
|
||||
|
|
|
@ -9,7 +9,7 @@ use rustc_errors::Applicability;
|
|||
use rustc_hir as hir;
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
use rustc_span::{edition::Edition, Span};
|
||||
use rustc_span::{edition::Edition, sym, Span};
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** Checks for `#[macro_use] use...`.
|
||||
|
@ -110,9 +110,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
|
|||
if cx.sess().opts.edition >= Edition::Edition2018;
|
||||
if let hir::ItemKind::Use(path, _kind) = &item.kind;
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
if let Some(mac_attr) = attrs
|
||||
.iter()
|
||||
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
|
||||
if let Some(mac_attr) = attrs.iter().find(|attr| attr.has_name(sym::macro_use));
|
||||
if let Res::Def(DefKind::Mod, id) = path.res;
|
||||
then {
|
||||
for kid in cx.tcx.item_children(id).iter() {
|
||||
|
|
|
@ -10,7 +10,7 @@ use rustc_hir::{BlockCheckMode, UnsafeSource};
|
|||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty;
|
||||
use rustc_span::source_map::Span;
|
||||
use rustc_span::symbol::sym;
|
||||
use rustc_span::symbol::{kw, sym};
|
||||
use std::borrow::Cow;
|
||||
|
||||
use super::OR_FUN_CALL;
|
||||
|
@ -38,8 +38,8 @@ pub(super) fn check<'tcx>(
|
|||
if !or_has_args;
|
||||
if name == "unwrap_or";
|
||||
if let hir::ExprKind::Path(ref qpath) = fun.kind;
|
||||
let path = &*last_path_segment(qpath).ident.as_str();
|
||||
if ["default", "new"].contains(&path);
|
||||
let path = last_path_segment(qpath).ident.name;
|
||||
if matches!(path, kw::Default | sym::new);
|
||||
let arg_ty = cx.typeck_results().expr_ty(arg);
|
||||
if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
|
||||
if implements_trait(cx, arg_ty, default_trait_id, &[]);
|
||||
|
|
|
@ -4,6 +4,7 @@ use if_chain::if_chain;
|
|||
use rustc_ast::ast::{Expr, ExprKind};
|
||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::sym;
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** Checks for usage of `option_env!(...).unwrap()` and
|
||||
|
@ -37,8 +38,7 @@ impl EarlyLintPass for OptionEnvUnwrap {
|
|||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||
if_chain! {
|
||||
if let ExprKind::MethodCall(path_segment, args, _) = &expr.kind;
|
||||
let method_name = path_segment.ident.as_str();
|
||||
if method_name == "expect" || method_name == "unwrap";
|
||||
if matches!(path_segment.ident.name, sym::expect | sym::unwrap);
|
||||
if let ExprKind::Call(caller, _) = &args[0].kind;
|
||||
if is_direct_expn_of(caller.span, "option_env").is_some();
|
||||
then {
|
||||
|
|
|
@ -1203,16 +1203,9 @@ pub fn parent_node_is_if_expr(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
|
|||
)
|
||||
}
|
||||
|
||||
// Finds the attribute with the given name, if any
|
||||
pub fn attr_by_name<'a>(attrs: &'a [Attribute], name: &'_ str) -> Option<&'a Attribute> {
|
||||
attrs
|
||||
.iter()
|
||||
.find(|attr| attr.ident().map_or(false, |ident| ident.as_str() == name))
|
||||
}
|
||||
|
||||
// Finds the `#[must_use]` attribute, if any
|
||||
pub fn must_use_attr(attrs: &[Attribute]) -> Option<&Attribute> {
|
||||
attr_by_name(attrs, "must_use")
|
||||
attrs.iter().find(|a| a.has_name(sym::must_use))
|
||||
}
|
||||
|
||||
// check if expr is calling method or function with #[must_use] attribute
|
||||
|
|
Loading…
Reference in a new issue