Skip to content

Commit 847a95b

Browse files
committed
Refactor utils on checking attribute
Moved out reusable pieces from `is_automatically_derived` and `any_parent_is_automatically_derived`.
1 parent 2e17035 commit 847a95b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

clippy_utils/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ pub fn recurse_or_patterns<'tcx, F: FnMut(&'tcx Pat<'tcx>)>(pat: &'tcx Pat<'tcx>
14021402
/// Checks for the `#[automatically_derived]` attribute all `#[derive]`d
14031403
/// implementations have.
14041404
pub fn is_automatically_derived(attrs: &[ast::Attribute]) -> bool {
1405-
attrs.iter().any(|attr| attr.has_name(sym::automatically_derived))
1405+
has_attr(attrs, sym::automatically_derived)
14061406
}
14071407

14081408
/// Remove blocks around an expression.
@@ -1524,20 +1524,29 @@ pub fn clip(tcx: TyCtxt<'_>, u: u128, ity: rustc_ty::UintTy) -> u128 {
15241524
(u << amt) >> amt
15251525
}
15261526

1527-
pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_>, node: HirId) -> bool {
1527+
pub fn has_attr(attrs: &[ast::Attribute], symbol: Symbol) -> bool {
1528+
attrs.iter().any(|attr| attr.has_name(symbol))
1529+
}
1530+
1531+
pub fn any_parent_has_attr(tcx: TyCtxt<'_>, node: HirId, symbol: Symbol) -> bool {
15281532
let map = &tcx.hir();
15291533
let mut prev_enclosing_node = None;
15301534
let mut enclosing_node = node;
15311535
while Some(enclosing_node) != prev_enclosing_node {
1532-
if is_automatically_derived(map.attrs(enclosing_node)) {
1536+
if has_attr(map.attrs(enclosing_node), symbol) {
15331537
return true;
15341538
}
15351539
prev_enclosing_node = Some(enclosing_node);
15361540
enclosing_node = map.get_parent_item(enclosing_node);
15371541
}
1542+
15381543
false
15391544
}
15401545

1546+
pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_>, node: HirId) -> bool {
1547+
any_parent_has_attr(tcx, node, sym::automatically_derived)
1548+
}
1549+
15411550
/// Matches a function call with the given path and returns the arguments.
15421551
///
15431552
/// Usage:

0 commit comments

Comments
 (0)