X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fexcessive_bools.rs;h=476e6d23f12151e869270de3c8265bc647993d25;hb=b1786f62edf8bc20de33a36973ffb307a13962f5;hp=a1bfb0a710108e05aa617db0ea5eaf8e8adc867d;hpb=779b6aeaa62d7d832bb791c7d92ed11d43f4873a;p=rust.git diff --git a/clippy_lints/src/excessive_bools.rs b/clippy_lints/src/excessive_bools.rs index a1bfb0a7101..476e6d23f12 100644 --- a/clippy_lints/src/excessive_bools.rs +++ b/clippy_lints/src/excessive_bools.rs @@ -1,25 +1,26 @@ -use crate::utils::{attr_by_name, in_macro, match_path_ast, span_lint_and_help}; +use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::in_macro; +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 syntax::ast::{AssocItemKind, Extern, FnSig, Item, ItemKind, Ty, TyKind}; +use rustc_span::{sym, Span}; use std::convert::TryInto; declare_clippy_lint! { - /// **What it does:** Checks for excessive + /// ### What it does + /// Checks for excessive /// use of bools in structs. /// - /// **Why is this bad?** Excessive bools in a struct + /// ### Why is this bad? + /// Excessive bools in a struct /// is often a sign that it's used as a state machine, /// which is much better implemented as an enum. /// If it's not the case, excessive bools usually benefit /// from refactoring into two-variant enums for better /// readability and API. /// - /// **Known problems:** None. - /// - /// **Example:** + /// ### Example /// Bad: /// ```rust /// struct S { @@ -43,19 +44,19 @@ } declare_clippy_lint! { - /// **What it does:** Checks for excessive use of + /// ### What it does + /// Checks for excessive use of /// bools in function definitions. /// - /// **Why is this bad?** Calls to such functions + /// ### Why is this bad? + /// Calls to such functions /// are confusing and error prone, because it's /// hard to remember argument order and you have /// no type system support to back you up. Using /// two-variant enums instead of bools often makes /// API easier to use. /// - /// **Known problems:** None. - /// - /// **Example:** + /// ### Example /// Bad: /// ```rust,ignore /// fn f(is_round: bool, is_hot: bool) { ... } @@ -114,6 +115,7 @@ fn check_fn_sig(&self, cx: &EarlyContext<'_>, fn_sig: &FnSig, span: Span) { FN_PARAMS_EXCESSIVE_BOOLS, span, &format!("more than {} bools in function parameters", self.max_fn_params_bools), + None, "consider refactoring bools into two-variant enums", ); } @@ -124,7 +126,9 @@ fn check_fn_sig(&self, cx: &EarlyContext<'_>, fn_sig: &FnSig, span: Span) { fn is_bool_ty(ty: &Ty) -> bool { if let TyKind::Path(None, path) = &ty.kind { - return match_path_ast(path, &["bool"]); + if let [name] = path.segments.as_slice() { + return name.ident.name == sym::bool; + } } false } @@ -136,7 +140,7 @@ fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { } 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; } @@ -153,21 +157,22 @@ fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { STRUCT_EXCESSIVE_BOOLS, item.span, &format!("more than {} bools in a struct", self.max_struct_bools), + None, "consider using a state machine or refactoring bools into two-variant enums", ); } }, - ItemKind::Impl { + ItemKind::Impl(box ImplKind { of_trait: None, items, .. - } - | ItemKind::Trait(_, _, _, _, items) => { + }) + | ItemKind::Trait(box TraitKind(.., items)) => { for item in items { - if let AssocItemKind::Fn(fn_sig, _) = &item.kind { + if let AssocItemKind::Fn(box FnKind(_, fn_sig, _, _)) = &item.kind { self.check_fn_sig(cx, fn_sig, item.span); } } }, - ItemKind::Fn(fn_sig, _, _) => self.check_fn_sig(cx, fn_sig, item.span), + ItemKind::Fn(box FnKind(_, fn_sig, _, _)) => self.check_fn_sig(cx, fn_sig, item.span), _ => (), } }