Skip to content

Commit 35e93a1

Browse files
committed
factor out is_automatically_derived util fn
1 parent 502416f commit 35e93a1

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

clippy_lints/src/derive.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ use rustc::ty::subst::Subst;
33
use rustc::ty::TypeVariants;
44
use rustc::ty;
55
use rustc::hir::*;
6-
use syntax::ast::{Attribute, MetaItemKind};
76
use syntax::codemap::Span;
87
use utils::paths;
9-
use utils::{match_path, span_lint_and_then};
8+
use utils::{is_automatically_derived, match_path, span_lint_and_then};
109

1110
/// **What it does:** Checks for deriving `Hash` but implementing `PartialEq`
1211
/// explicitly.
@@ -75,7 +74,7 @@ impl LateLintPass for Derive {
7574
fn check_item(&mut self, cx: &LateContext, item: &Item) {
7675
if let ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
7776
let ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(item.id)).ty;
78-
let is_automatically_derived = item.attrs.iter().any(is_automatically_derived);
77+
let is_automatically_derived = is_automatically_derived(&*item.attrs);
7978

8079
check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived);
8180

@@ -97,7 +96,7 @@ fn check_hash_peq<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, span: Span, trait_re
9796

9897
// Look for the PartialEq implementations for `ty`
9998
peq_trait_def.for_each_relevant_impl(cx.tcx, ty, |impl_id| {
100-
let peq_is_automatically_derived = cx.tcx.get_attrs(impl_id).iter().any(is_automatically_derived);
99+
let peq_is_automatically_derived = is_automatically_derived(&cx.tcx.get_attrs(impl_id));
101100

102101
if peq_is_automatically_derived == hash_is_automatically_derived {
103102
return;
@@ -174,12 +173,3 @@ fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref
174173
});
175174
}
176175
}
177-
178-
/// Checks for the `#[automatically_derived]` attribute all `#[derive]`d implementations have.
179-
fn is_automatically_derived(attr: &Attribute) -> bool {
180-
if let MetaItemKind::Word(ref word) = attr.node.value.node {
181-
word == &"automatically_derived"
182-
} else {
183-
false
184-
}
185-
}

clippy_lints/src/utils/inspector.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
55
use rustc::lint::*;
66
use rustc::hir;
7-
use syntax::ast::{Attribute, MetaItemKind};
7+
use syntax::ast::Attribute;
8+
use syntax::attr;
89

910
/// **What it does:** Dumps every ast/hir node which has the `#[clippy_dump]` attribute
1011
///
@@ -128,10 +129,7 @@ impl LateLintPass for Pass {
128129
}
129130

130131
fn has_attr(attrs: &[Attribute]) -> bool {
131-
attrs.iter().any(|attr| match attr.node.value.node {
132-
MetaItemKind::Word(ref word) => word == "clippy_dump",
133-
_ => false,
134-
})
132+
attr::contains_name(attrs, "clippy_dump")
135133
}
136134

137135
fn print_decl(cx: &LateContext, decl: &hir::Decl) {

clippy_lints/src/utils/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::env;
1414
use std::mem;
1515
use std::str::FromStr;
1616
use syntax::ast::{self, LitKind};
17+
use syntax::attr;
1718
use syntax::codemap::{ExpnFormat, ExpnInfo, MultiSpan, Span, DUMMY_SP};
1819
use syntax::errors::DiagnosticBuilder;
1920
use syntax::ptr::P;
@@ -761,3 +762,8 @@ pub fn is_refutable(cx: &LateContext, pat: &Pat) -> bool {
761762
}
762763
}
763764
}
765+
766+
/// Checks for the `#[automatically_derived]` attribute all `#[derive]`d implementations have.
767+
pub fn is_automatically_derived(attrs: &[ast::Attribute]) -> bool {
768+
attr::contains_name(attrs, "automatically_derived")
769+
}

0 commit comments

Comments
 (0)