Skip to content

Commit 8e81605

Browse files
committed
Do not store attrs in FnKind.
1 parent f5dc5dc commit 8e81605

File tree

16 files changed

+51
-70
lines changed

16 files changed

+51
-70
lines changed

compiler/rustc_hir/src/intravisit.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,21 @@ where
101101
#[derive(Copy, Clone)]
102102
pub enum FnKind<'a> {
103103
/// `#[xxx] pub async/const/extern "Abi" fn foo()`
104-
ItemFn(Ident, &'a Generics<'a>, FnHeader, &'a Visibility<'a>, &'a [Attribute]),
104+
ItemFn(Ident, &'a Generics<'a>, FnHeader, &'a Visibility<'a>),
105105

106106
/// `fn foo(&self)`
107-
Method(Ident, &'a FnSig<'a>, Option<&'a Visibility<'a>>, &'a [Attribute]),
107+
Method(Ident, &'a FnSig<'a>, Option<&'a Visibility<'a>>),
108108

109109
/// `|x, y| {}`
110-
Closure(&'a [Attribute]),
110+
Closure,
111111
}
112112

113113
impl<'a> FnKind<'a> {
114-
pub fn attrs(&self) -> &'a [Attribute] {
115-
match *self {
116-
FnKind::ItemFn(.., attrs) => attrs,
117-
FnKind::Method(.., attrs) => attrs,
118-
FnKind::Closure(attrs) => attrs,
119-
}
120-
}
121-
122114
pub fn header(&self) -> Option<&FnHeader> {
123115
match *self {
124-
FnKind::ItemFn(_, _, ref header, _, _) => Some(header),
125-
FnKind::Method(_, ref sig, _, _) => Some(&sig.header),
126-
FnKind::Closure(_) => None,
116+
FnKind::ItemFn(_, _, ref header, _) => Some(header),
117+
FnKind::Method(_, ref sig, _) => Some(&sig.header),
118+
FnKind::Closure => None,
127119
}
128120
}
129121
}
@@ -579,7 +571,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
579571
visitor.visit_nested_body(body);
580572
}
581573
ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn(
582-
FnKind::ItemFn(item.ident, generics, sig.header, &item.vis, &item.attrs),
574+
FnKind::ItemFn(item.ident, generics, sig.header, &item.vis),
583575
&sig.decl,
584576
body_id,
585577
item.span,
@@ -940,7 +932,7 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'
940932
FnKind::ItemFn(_, generics, ..) => {
941933
visitor.visit_generics(generics);
942934
}
943-
FnKind::Method(..) | FnKind::Closure(_) => {}
935+
FnKind::Method(..) | FnKind::Closure => {}
944936
}
945937
}
946938

@@ -977,7 +969,7 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
977969
}
978970
TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
979971
visitor.visit_fn(
980-
FnKind::Method(trait_item.ident, sig, None, &trait_item.attrs),
972+
FnKind::Method(trait_item.ident, sig, None),
981973
&sig.decl,
982974
body_id,
983975
trait_item.span,
@@ -1027,7 +1019,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
10271019
}
10281020
ImplItemKind::Fn(ref sig, body_id) => {
10291021
visitor.visit_fn(
1030-
FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), &impl_item.attrs),
1022+
FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis)),
10311023
&sig.decl,
10321024
body_id,
10331025
impl_item.span,
@@ -1162,7 +1154,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
11621154
}
11631155
ExprKind::Closure(_, ref function_declaration, body, _fn_decl_span, _gen) => visitor
11641156
.visit_fn(
1165-
FnKind::Closure(&expression.attrs),
1157+
FnKind::Closure,
11661158
function_declaration,
11671159
body,
11681160
expression.span,

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,15 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
400400
}
401401
_ => (),
402402
},
403-
FnKind::ItemFn(ident, _, header, _, attrs) => {
403+
FnKind::ItemFn(ident, _, header, _) => {
404+
let attrs = cx.tcx.hir().attrs(id);
404405
// Skip foreign-ABI #[no_mangle] functions (Issue #31924)
405406
if header.abi != Abi::Rust && cx.sess().contains_name(attrs, sym::no_mangle) {
406407
return;
407408
}
408409
self.check_snake_case(cx, "function", ident);
409410
}
410-
FnKind::Closure(_) => (),
411+
FnKind::Closure => (),
411412
}
412413
}
413414

compiler/rustc_middle/src/hir/map/blocks.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//! for the `Code` associated with a particular NodeId.
1313
1414
use crate::hir::map::Map;
15-
use rustc_ast::Attribute;
1615
use rustc_hir as hir;
1716
use rustc_hir::intravisit::FnKind;
1817
use rustc_hir::{Expr, FnDecl, Node};
@@ -105,7 +104,6 @@ struct ItemFnParts<'a> {
105104
body: hir::BodyId,
106105
id: hir::HirId,
107106
span: Span,
108-
attrs: &'a [Attribute],
109107
}
110108

111109
/// These are all the components one can extract from a closure expr
@@ -115,18 +113,11 @@ struct ClosureParts<'a> {
115113
body: hir::BodyId,
116114
id: hir::HirId,
117115
span: Span,
118-
attrs: &'a [Attribute],
119116
}
120117

121118
impl<'a> ClosureParts<'a> {
122-
fn new(
123-
d: &'a FnDecl<'a>,
124-
b: hir::BodyId,
125-
id: hir::HirId,
126-
s: Span,
127-
attrs: &'a [Attribute],
128-
) -> Self {
129-
ClosureParts { decl: d, body: b, id, span: s, attrs }
119+
fn new(d: &'a FnDecl<'a>, b: hir::BodyId, id: hir::HirId, s: Span) -> Self {
120+
ClosureParts { decl: d, body: b, id, span: s }
130121
}
131122
}
132123

@@ -146,31 +137,31 @@ impl<'a> FnLikeNode<'a> {
146137
pub fn body(self) -> hir::BodyId {
147138
self.handle(
148139
|i: ItemFnParts<'a>| i.body,
149-
|_, _, _: &'a hir::FnSig<'a>, _, body: hir::BodyId, _, _| body,
140+
|_, _, _: &'a hir::FnSig<'a>, _, body: hir::BodyId, _| body,
150141
|c: ClosureParts<'a>| c.body,
151142
)
152143
}
153144

154145
pub fn decl(self) -> &'a FnDecl<'a> {
155146
self.handle(
156147
|i: ItemFnParts<'a>| &*i.decl,
157-
|_, _, sig: &'a hir::FnSig<'a>, _, _, _, _| &sig.decl,
148+
|_, _, sig: &'a hir::FnSig<'a>, _, _, _| &sig.decl,
158149
|c: ClosureParts<'a>| c.decl,
159150
)
160151
}
161152

162153
pub fn span(self) -> Span {
163154
self.handle(
164155
|i: ItemFnParts<'_>| i.span,
165-
|_, _, _: &'a hir::FnSig<'a>, _, _, span, _| span,
156+
|_, _, _: &'a hir::FnSig<'a>, _, _, span| span,
166157
|c: ClosureParts<'_>| c.span,
167158
)
168159
}
169160

170161
pub fn id(self) -> hir::HirId {
171162
self.handle(
172163
|i: ItemFnParts<'_>| i.id,
173-
|id, _, _: &'a hir::FnSig<'a>, _, _, _, _| id,
164+
|id, _, _: &'a hir::FnSig<'a>, _, _, _| id,
174165
|c: ClosureParts<'_>| c.id,
175166
)
176167
}
@@ -189,12 +180,11 @@ impl<'a> FnLikeNode<'a> {
189180

190181
pub fn kind(self) -> FnKind<'a> {
191182
let item = |p: ItemFnParts<'a>| -> FnKind<'a> {
192-
FnKind::ItemFn(p.ident, p.generics, p.header, p.vis, p.attrs)
193-
};
194-
let closure = |c: ClosureParts<'a>| FnKind::Closure(c.attrs);
195-
let method = |_, ident: Ident, sig: &'a hir::FnSig<'a>, vis, _, _, attrs| {
196-
FnKind::Method(ident, sig, vis, attrs)
183+
FnKind::ItemFn(p.ident, p.generics, p.header, p.vis)
197184
};
185+
let closure = |_: ClosureParts<'a>| FnKind::Closure;
186+
let method =
187+
|_, ident: Ident, sig: &'a hir::FnSig<'a>, vis, _, _| FnKind::Method(ident, sig, vis);
198188
self.handle(item, method, closure)
199189
}
200190

@@ -208,7 +198,6 @@ impl<'a> FnLikeNode<'a> {
208198
Option<&'a hir::Visibility<'a>>,
209199
hir::BodyId,
210200
Span,
211-
&'a [Attribute],
212201
) -> A,
213202
C: FnOnce(ClosureParts<'a>) -> A,
214203
{
@@ -221,27 +210,26 @@ impl<'a> FnLikeNode<'a> {
221210
body: block,
222211
vis: &i.vis,
223212
span: i.span,
224-
attrs: &i.attrs,
225213
header: sig.header,
226214
generics,
227215
}),
228216
_ => bug!("item FnLikeNode that is not fn-like"),
229217
},
230218
Node::TraitItem(ti) => match ti.kind {
231219
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
232-
method(ti.hir_id(), ti.ident, sig, None, body, ti.span, &ti.attrs)
220+
method(ti.hir_id(), ti.ident, sig, None, body, ti.span)
233221
}
234222
_ => bug!("trait method FnLikeNode that is not fn-like"),
235223
},
236224
Node::ImplItem(ii) => match ii.kind {
237225
hir::ImplItemKind::Fn(ref sig, body) => {
238-
method(ii.hir_id(), ii.ident, sig, Some(&ii.vis), body, ii.span, &ii.attrs)
226+
method(ii.hir_id(), ii.ident, sig, Some(&ii.vis), body, ii.span)
239227
}
240228
_ => bug!("impl method FnLikeNode that is not fn-like"),
241229
},
242230
Node::Expr(e) => match e.kind {
243231
hir::ExprKind::Closure(_, ref decl, block, _fn_decl_span, _gen) => {
244-
closure(ClosureParts::new(&decl, block, e.hir_id, e.span, &e.attrs))
232+
closure(ClosureParts::new(&decl, block, e.hir_id, e.span))
245233
}
246234
_ => bug!("expr FnLikeNode that is not fn-like"),
247235
},

compiler/rustc_mir_build/src/lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
1515
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1616

1717
if let Some(fn_like_node) = FnLikeNode::from_node(tcx.hir().get(hir_id)) {
18-
if let FnKind::Closure(_) = fn_like_node.kind() {
18+
if let FnKind::Closure = fn_like_node.kind() {
1919
// closures can't recur, so they don't matter.
2020
return;
2121
}

compiler/rustc_passes/src/naked_functions.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> Visitor<'tcx> for CheckNakedFunctions<'tcx> {
4646
let fn_header;
4747

4848
match fk {
49-
FnKind::Closure(..) => {
49+
FnKind::Closure => {
5050
// Closures with a naked attribute are rejected during attribute
5151
// check. Don't validate them any further.
5252
return;
@@ -62,7 +62,8 @@ impl<'tcx> Visitor<'tcx> for CheckNakedFunctions<'tcx> {
6262
}
6363
}
6464

65-
let naked = fk.attrs().iter().any(|attr| attr.has_name(sym::naked));
65+
let attrs = self.tcx.hir().attrs(hir_id);
66+
let naked = attrs.iter().any(|attr| attr.has_name(sym::naked));
6667
if naked {
6768
let body = self.tcx.hir().body(body_id);
6869
check_abi(self.tcx, hir_id, fn_header.abi, ident_span);

compiler/rustc_typeck/src/check/regionck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionCtxt<'a, 'tcx> {
354354
hir_id: hir::HirId,
355355
) {
356356
assert!(
357-
matches!(fk, intravisit::FnKind::Closure(..)),
357+
matches!(fk, intravisit::FnKind::Closure),
358358
"visit_fn invoked for something other than a closure"
359359
);
360360

src/tools/clippy/clippy_lints/src/cognitive_complexity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ impl CognitiveComplexity {
7676

7777
if rust_cc > self.limit.limit() {
7878
let fn_span = match kind {
79-
FnKind::ItemFn(ident, _, _, _, _) | FnKind::Method(ident, _, _, _) => ident.span,
80-
FnKind::Closure(_) => {
79+
FnKind::ItemFn(ident, _, _, _) | FnKind::Method(ident, _, _) => ident.span,
80+
FnKind::Closure => {
8181
let header_span = body_span.with_hi(decl.output.span().lo());
8282
let pos = snippet_opt(cx, header_span).and_then(|snip| {
8383
let low_offset = snip.find('|')?;

src/tools/clippy/clippy_lints/src/functions.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
251251
hir_id: hir::HirId,
252252
) {
253253
let unsafety = match kind {
254-
intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _, _) => unsafety,
255-
intravisit::FnKind::Method(_, sig, _, _) => sig.header.unsafety,
256-
intravisit::FnKind::Closure(_) => return,
254+
intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _) => unsafety,
255+
intravisit::FnKind::Method(_, sig, _) => sig.header.unsafety,
256+
intravisit::FnKind::Closure => return,
257257
};
258258

259259
// don't warn for implementations, it's not their fault
@@ -267,9 +267,8 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
267267
..
268268
},
269269
_,
270-
_,
271270
)
272-
| intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }, _, _) => {
271+
| intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }, _) => {
273272
self.check_arg_number(cx, decl, span.with_hi(decl.output.span().hi()))
274273
},
275274
_ => {},

src/tools/clippy/clippy_lints/src/future_not_send.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
5858
_: Span,
5959
hir_id: HirId,
6060
) {
61-
if let FnKind::Closure(_) = kind {
61+
if let FnKind::Closure = kind {
6262
return;
6363
}
6464
let ret_ty = utils::return_ty(cx, hir_id);

src/tools/clippy/clippy_lints/src/misc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
278278
span: Span,
279279
_: HirId,
280280
) {
281-
if let FnKind::Closure(_) = k {
281+
if let FnKind::Closure = k {
282282
// Does not apply to closures
283283
return;
284284
}

src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
133133
return;
134134
}
135135
},
136-
FnKind::Closure(..) => return,
136+
FnKind::Closure => return,
137137
}
138138

139139
let mir = cx.tcx.optimized_mir(def_id);

src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
8080
}
8181

8282
match kind {
83-
FnKind::ItemFn(.., header, _, attrs) => {
83+
FnKind::ItemFn(.., header, _) => {
84+
let attrs = cx.tcx.hir().attrs(hir_id);
8485
if header.abi != Abi::Rust || requires_exact_signature(attrs) {
8586
return;
8687
}
8788
},
8889
FnKind::Method(..) => (),
89-
FnKind::Closure(..) => return,
90+
FnKind::Closure => return,
9091
}
9192

9293
// Exclude non-inherent impls

src/tools/clippy/clippy_lints/src/panic_in_result_fn.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
4343
span: Span,
4444
hir_id: hir::HirId,
4545
) {
46-
if !matches!(fn_kind, FnKind::Closure(_))
47-
&& is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type)
48-
{
46+
if !matches!(fn_kind, FnKind::Closure) && is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type) {
4947
lint_impl_body(cx, span, body);
5048
}
5149
}

src/tools/clippy/clippy_lints/src/pass_by_ref_or_value.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,11 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
224224
}
225225

226226
match kind {
227-
FnKind::ItemFn(.., header, _, attrs) => {
227+
FnKind::ItemFn(.., header, _) => {
228228
if header.abi != Abi::Rust {
229229
return;
230230
}
231+
let attrs = cx.tcx.hir().attrs(hir_id);
231232
for a in attrs {
232233
if let Some(meta_items) = a.meta_item_list() {
233234
if a.has_name(sym::proc_macro_derive)
@@ -239,7 +240,7 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
239240
}
240241
},
241242
FnKind::Method(..) => (),
242-
FnKind::Closure(..) => return,
243+
FnKind::Closure => return,
243244
}
244245

245246
// Exclude non-inherent impls

src/tools/clippy/clippy_lints/src/returns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'tcx> LateLintPass<'tcx> for Return {
131131
_: HirId,
132132
) {
133133
match kind {
134-
FnKind::Closure(_) => {
134+
FnKind::Closure => {
135135
// when returning without value in closure, replace this `return`
136136
// with an empty block to prevent invalid suggestion (see #6501)
137137
let replacement = if let ExprKind::Ret(None) = &body.value.kind {

0 commit comments

Comments
 (0)