factor out is_automatically_derived util fn

This commit is contained in:
Sebastian Ullrich 2016-10-29 21:13:41 -04:00
parent 8630376e71
commit 90f71be4e9
3 changed files with 12 additions and 18 deletions

View File

@ -3,10 +3,9 @@ use rustc::ty::subst::Subst;
use rustc::ty::TypeVariants;
use rustc::ty;
use rustc::hir::*;
use syntax::ast::{Attribute, MetaItemKind};
use syntax::codemap::Span;
use utils::paths;
use utils::{match_path, span_lint_and_then};
use utils::{is_automatically_derived, match_path, span_lint_and_then};
/// **What it does:** Checks for deriving `Hash` but implementing `PartialEq`
/// explicitly.
@ -75,7 +74,7 @@ impl LateLintPass for Derive {
fn check_item(&mut self, cx: &LateContext, item: &Item) {
if let ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
let ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(item.id)).ty;
let is_automatically_derived = item.attrs.iter().any(is_automatically_derived);
let is_automatically_derived = is_automatically_derived(&*item.attrs);
check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived);
@ -97,7 +96,7 @@ fn check_hash_peq<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, span: Span, trait_re
// Look for the PartialEq implementations for `ty`
peq_trait_def.for_each_relevant_impl(cx.tcx, ty, |impl_id| {
let peq_is_automatically_derived = cx.tcx.get_attrs(impl_id).iter().any(is_automatically_derived);
let peq_is_automatically_derived = is_automatically_derived(&cx.tcx.get_attrs(impl_id));
if peq_is_automatically_derived == hash_is_automatically_derived {
return;
@ -174,12 +173,3 @@ fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref
});
}
}
/// Checks for the `#[automatically_derived]` attribute all `#[derive]`d implementations have.
fn is_automatically_derived(attr: &Attribute) -> bool {
if let MetaItemKind::Word(ref word) = attr.node.value.node {
word == &"automatically_derived"
} else {
false
}
}

View File

@ -4,7 +4,8 @@
use rustc::lint::*;
use rustc::hir;
use syntax::ast::{Attribute, MetaItemKind};
use syntax::ast::Attribute;
use syntax::attr;
/// **What it does:** Dumps every ast/hir node which has the `#[clippy_dump]` attribute
///
@ -128,10 +129,7 @@ impl LateLintPass for Pass {
}
fn has_attr(attrs: &[Attribute]) -> bool {
attrs.iter().any(|attr| match attr.node.value.node {
MetaItemKind::Word(ref word) => word == "clippy_dump",
_ => false,
})
attr::contains_name(attrs, "clippy_dump")
}
fn print_decl(cx: &LateContext, decl: &hir::Decl) {

View File

@ -14,6 +14,7 @@ use std::env;
use std::mem;
use std::str::FromStr;
use syntax::ast::{self, LitKind};
use syntax::attr;
use syntax::codemap::{ExpnFormat, ExpnInfo, MultiSpan, Span, DUMMY_SP};
use syntax::errors::DiagnosticBuilder;
use syntax::ptr::P;
@ -761,3 +762,8 @@ pub fn is_refutable(cx: &LateContext, pat: &Pat) -> bool {
}
}
}
/// Checks for the `#[automatically_derived]` attribute all `#[derive]`d implementations have.
pub fn is_automatically_derived(attrs: &[ast::Attribute]) -> bool {
attr::contains_name(attrs, "automatically_derived")
}