Skip to content

Commit e8c2c3d

Browse files
committed
refactor has_repr_attr
1 parent 9c69e93 commit e8c2c3d

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

clippy_lints/src/excessive_bools.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::{get_parent_node, is_bool};
2+
use clippy_utils::{has_repr_attr, is_bool};
33
use rustc_hir::intravisit::FnKind;
4-
use rustc_hir::{Body, FnDecl, HirId, Item, ItemKind, Node, Ty};
4+
use rustc_hir::{Body, FnDecl, HirId, Item, ItemKind, Ty};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_tool_lint, impl_lint_pass};
7-
use rustc_span::{sym, Span};
7+
use rustc_span::Span;
88
use rustc_target::spec::abi::Abi;
99

1010
declare_clippy_lint! {
@@ -135,13 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
135135
return;
136136
}
137137
if let ItemKind::Struct(variant_data, _) = &item.kind {
138-
if cx
139-
.tcx
140-
.hir()
141-
.attrs(item.hir_id())
142-
.iter()
143-
.any(|attr| attr.has_name(sym::repr))
144-
{
138+
if has_repr_attr(cx, item.hir_id()) {
145139
return;
146140
}
147141

@@ -165,15 +159,10 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
165159
fn_decl: &'tcx FnDecl<'tcx>,
166160
_: &'tcx Body<'tcx>,
167161
span: Span,
168-
hir_id: HirId,
162+
_: HirId,
169163
) {
170164
if let Some(fn_header) = fn_kind.header()
171165
&& fn_header.abi == Abi::Rust
172-
&& if let Some(Node::Item(item)) = get_parent_node(cx.tcx, hir_id) {
173-
!matches!(item.kind, ItemKind::ExternCrate(..))
174-
} else {
175-
true
176-
}
177166
&& !span.from_expansion() {
178167
self.check_fn_sig(cx, fn_decl, span)
179168
}

clippy_lints/src/trailing_empty_array.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use rustc_hir::{HirId, Item, ItemKind};
2+
use clippy_utils::has_repr_attr;
3+
use rustc_hir::{Item, ItemKind};
34
use rustc_lint::{LateContext, LateLintPass};
45
use rustc_middle::ty::Const;
56
use rustc_session::{declare_lint_pass, declare_tool_lint};
6-
use rustc_span::sym;
77

88
declare_clippy_lint! {
99
/// ### What it does
@@ -72,7 +72,3 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'_>, item: &Item<'_
7272
}
7373
}
7474
}
75-
76-
fn has_repr_attr(cx: &LateContext<'_>, hir_id: HirId) -> bool {
77-
cx.tcx.hir().attrs(hir_id).iter().any(|attr| attr.has_name(sym::repr))
78-
}

clippy_utils/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,10 @@ pub fn has_attr(attrs: &[ast::Attribute], symbol: Symbol) -> bool {
17801780
attrs.iter().any(|attr| attr.has_name(symbol))
17811781
}
17821782

1783+
pub fn has_repr_attr(cx: &LateContext<'_>, hir_id: HirId) -> bool {
1784+
has_attr(cx.tcx.hir().attrs(hir_id), sym::repr)
1785+
}
1786+
17831787
pub fn any_parent_has_attr(tcx: TyCtxt<'_>, node: HirId, symbol: Symbol) -> bool {
17841788
let map = &tcx.hir();
17851789
let mut prev_enclosing_node = None;

0 commit comments

Comments
 (0)