|
1 |
| -#![allow(warnings)] |
2 |
| - |
3 | 1 | use rustc_hir::{Body, Item, ItemKind, OwnerNode, Path, QPath, TyKind};
|
4 | 2 | use rustc_span::def_id::{DefId, LOCAL_CRATE};
|
5 | 3 | use rustc_span::{sym, symbol::kw, symbol::Ident, ExpnKind, MacroKind};
|
@@ -94,130 +92,130 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
|
94 | 92 | };
|
95 | 93 |
|
96 | 94 | match item.kind {
|
97 |
| - // ItemKind::Impl(impl_) => { |
98 |
| - // // The RFC states: |
99 |
| - // // |
100 |
| - // // > An item nested inside an expression-containing item (through any |
101 |
| - // // > level of nesting) may not define an impl Trait for Type unless |
102 |
| - // // > either the **Trait** or the **Type** is also nested inside the |
103 |
| - // // > same expression-containing item. |
104 |
| - // // |
105 |
| - // // To achieve this we get try to get the paths of the _Trait_ and |
106 |
| - // // _Type_, and we look inside thoses paths to try a find in one |
107 |
| - // // of them a type whose parent is the same as the impl definition. |
108 |
| - // // |
109 |
| - // // If that's the case this means that this impl block declaration |
110 |
| - // // is using local items and so we don't lint on it. |
111 |
| - // |
112 |
| - // let mut parent_node_is_anon_const = { |
113 |
| - // let mut parent_node_is_anon_const = None; |
114 |
| - // move || { |
115 |
| - // *parent_node_is_anon_const.get_or_insert_with(|| { |
116 |
| - // matches!( |
117 |
| - // parent_node(), |
118 |
| - // OwnerNode::Item(Item { |
119 |
| - // ident: Ident { name: kw::Underscore, .. }, |
120 |
| - // kind: ItemKind::Const(..), |
121 |
| - // .. |
122 |
| - // }) |
123 |
| - // ) |
124 |
| - // }) |
125 |
| - // } |
126 |
| - // }; |
127 |
| - // let mut local_parent = { |
128 |
| - // let mut local_parent_cache = None; |
129 |
| - // move || { |
130 |
| - // *local_parent_cache |
131 |
| - // .get_or_insert_with(|| cx.tcx.parent(item.owner_id.to_def_id())) |
132 |
| - // } |
133 |
| - // }; |
134 |
| - // let mut extra_local_parent = { |
135 |
| - // let mut extra_parent_cache = None; |
136 |
| - // move |did| { |
137 |
| - // *extra_parent_cache.get_or_insert_with(|| { |
138 |
| - // parent_node_is_anon_const().then(|| cx.tcx.parent(did)) |
139 |
| - // }) |
140 |
| - // } |
141 |
| - // }; |
142 |
| - // |
143 |
| - // let self_ty_has_local_parent = match impl_.self_ty.kind { |
144 |
| - // TyKind::Path(QPath::Resolved(_, ty_path)) => path_has_local_parent( |
145 |
| - // ty_path, |
146 |
| - // cx, |
147 |
| - // &mut local_parent, |
148 |
| - // &mut extra_local_parent, |
149 |
| - // ), |
150 |
| - // TyKind::TraitObject([principle_poly_trait_ref, ..], _, _) => { |
151 |
| - // path_has_local_parent( |
152 |
| - // principle_poly_trait_ref.trait_ref.path, |
153 |
| - // cx, |
154 |
| - // &mut local_parent, |
155 |
| - // &mut extra_local_parent, |
156 |
| - // ) |
157 |
| - // } |
158 |
| - // TyKind::TraitObject([], _, _) |
159 |
| - // | TyKind::InferDelegation(_, _) |
160 |
| - // | TyKind::Slice(_) |
161 |
| - // | TyKind::Array(_, _) |
162 |
| - // | TyKind::Ptr(_) |
163 |
| - // | TyKind::Ref(_, _) |
164 |
| - // | TyKind::BareFn(_) |
165 |
| - // | TyKind::Never |
166 |
| - // | TyKind::Tup(_) |
167 |
| - // | TyKind::Path(_) |
168 |
| - // | TyKind::AnonAdt(_) |
169 |
| - // | TyKind::OpaqueDef(_, _, _) |
170 |
| - // | TyKind::Typeof(_) |
171 |
| - // | TyKind::Infer |
172 |
| - // | TyKind::Err(_) => false, |
173 |
| - // }; |
174 |
| - // |
175 |
| - // let of_trait_has_local_parent = impl_ |
176 |
| - // .of_trait |
177 |
| - // .map(|of_trait| { |
178 |
| - // path_has_local_parent( |
179 |
| - // of_trait.path, |
180 |
| - // cx, |
181 |
| - // &mut local_parent, |
182 |
| - // &mut extra_local_parent, |
183 |
| - // ) |
184 |
| - // }) |
185 |
| - // .unwrap_or(false); |
186 |
| - // |
187 |
| - // // If none of them have a local parent (LOGICAL NOR) this means that |
188 |
| - // // this impl definition is a non-local definition and so we lint on it. |
189 |
| - // if !(self_ty_has_local_parent || of_trait_has_local_parent) { |
190 |
| - // // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module. |
191 |
| - // if parent_node_is_anon_const() && self.body_depth == 1 { |
192 |
| - // return; |
193 |
| - // } |
194 |
| - // |
195 |
| - // let const_anon = if self.body_depth == 1 |
196 |
| - // && let OwnerNode::Item(item) = parent_node() |
197 |
| - // && let ItemKind::Const(ty, _, _) = item.kind |
198 |
| - // && let TyKind::Tup(&[]) = ty.kind |
199 |
| - // { |
200 |
| - // Some(item.ident.span) |
201 |
| - // } else { |
202 |
| - // None |
203 |
| - // }; |
204 |
| - // |
205 |
| - // cx.emit_span_lint( |
206 |
| - // NON_LOCAL_DEFINITIONS, |
207 |
| - // item.span, |
208 |
| - // NonLocalDefinitionsDiag::Impl { |
209 |
| - // depth: self.body_depth, |
210 |
| - // body_kind_descr: "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */, |
211 |
| - // body_name: parent_node() |
212 |
| - // .ident() |
213 |
| - // .map(|s| s.name.to_ident_string()) |
214 |
| - // .unwrap_or_else(|| "<unnameable>".to_string()), |
215 |
| - // cargo_update: cargo_update(), |
216 |
| - // const_anon, |
217 |
| - // }, |
218 |
| - // ) |
219 |
| - // } |
220 |
| - // } |
| 95 | + ItemKind::Impl(impl_) => { |
| 96 | + // The RFC states: |
| 97 | + // |
| 98 | + // > An item nested inside an expression-containing item (through any |
| 99 | + // > level of nesting) may not define an impl Trait for Type unless |
| 100 | + // > either the **Trait** or the **Type** is also nested inside the |
| 101 | + // > same expression-containing item. |
| 102 | + // |
| 103 | + // To achieve this we get try to get the paths of the _Trait_ and |
| 104 | + // _Type_, and we look inside thoses paths to try a find in one |
| 105 | + // of them a type whose parent is the same as the impl definition. |
| 106 | + // |
| 107 | + // If that's the case this means that this impl block declaration |
| 108 | + // is using local items and so we don't lint on it. |
| 109 | + |
| 110 | + let mut parent_node_is_anon_const = { |
| 111 | + let mut parent_node_is_anon_const = None; |
| 112 | + move || { |
| 113 | + *parent_node_is_anon_const.get_or_insert_with(|| { |
| 114 | + matches!( |
| 115 | + parent_node(), |
| 116 | + OwnerNode::Item(Item { |
| 117 | + ident: Ident { name: kw::Underscore, .. }, |
| 118 | + kind: ItemKind::Const(..), |
| 119 | + .. |
| 120 | + }) |
| 121 | + ) |
| 122 | + }) |
| 123 | + } |
| 124 | + }; |
| 125 | + let mut local_parent = { |
| 126 | + let mut local_parent_cache = None; |
| 127 | + move || { |
| 128 | + *local_parent_cache |
| 129 | + .get_or_insert_with(|| cx.tcx.parent(item.owner_id.to_def_id())) |
| 130 | + } |
| 131 | + }; |
| 132 | + let mut extra_local_parent = { |
| 133 | + let mut extra_parent_cache = None; |
| 134 | + move |did| { |
| 135 | + *extra_parent_cache.get_or_insert_with(|| { |
| 136 | + parent_node_is_anon_const().then(|| cx.tcx.parent(did)) |
| 137 | + }) |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + let self_ty_has_local_parent = match impl_.self_ty.kind { |
| 142 | + TyKind::Path(QPath::Resolved(_, ty_path)) => path_has_local_parent( |
| 143 | + ty_path, |
| 144 | + cx, |
| 145 | + &mut local_parent, |
| 146 | + &mut extra_local_parent, |
| 147 | + ), |
| 148 | + TyKind::TraitObject([principle_poly_trait_ref, ..], _, _) => { |
| 149 | + path_has_local_parent( |
| 150 | + principle_poly_trait_ref.trait_ref.path, |
| 151 | + cx, |
| 152 | + &mut local_parent, |
| 153 | + &mut extra_local_parent, |
| 154 | + ) |
| 155 | + } |
| 156 | + TyKind::TraitObject([], _, _) |
| 157 | + | TyKind::InferDelegation(_, _) |
| 158 | + | TyKind::Slice(_) |
| 159 | + | TyKind::Array(_, _) |
| 160 | + | TyKind::Ptr(_) |
| 161 | + | TyKind::Ref(_, _) |
| 162 | + | TyKind::BareFn(_) |
| 163 | + | TyKind::Never |
| 164 | + | TyKind::Tup(_) |
| 165 | + | TyKind::Path(_) |
| 166 | + | TyKind::AnonAdt(_) |
| 167 | + | TyKind::OpaqueDef(_, _, _) |
| 168 | + | TyKind::Typeof(_) |
| 169 | + | TyKind::Infer |
| 170 | + | TyKind::Err(_) => false, |
| 171 | + }; |
| 172 | + |
| 173 | + let of_trait_has_local_parent = impl_ |
| 174 | + .of_trait |
| 175 | + .map(|of_trait| { |
| 176 | + path_has_local_parent( |
| 177 | + of_trait.path, |
| 178 | + cx, |
| 179 | + &mut local_parent, |
| 180 | + &mut extra_local_parent, |
| 181 | + ) |
| 182 | + }) |
| 183 | + .unwrap_or(false); |
| 184 | + |
| 185 | + // If none of them have a local parent (LOGICAL NOR) this means that |
| 186 | + // this impl definition is a non-local definition and so we lint on it. |
| 187 | + if !(self_ty_has_local_parent || of_trait_has_local_parent) { |
| 188 | + // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module. |
| 189 | + if parent_node_is_anon_const() && self.body_depth == 1 { |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + let const_anon = if self.body_depth == 1 |
| 194 | + && let OwnerNode::Item(item) = parent_node() |
| 195 | + && let ItemKind::Const(ty, _, _) = item.kind |
| 196 | + && let TyKind::Tup(&[]) = ty.kind |
| 197 | + { |
| 198 | + Some(item.ident.span) |
| 199 | + } else { |
| 200 | + None |
| 201 | + }; |
| 202 | + |
| 203 | + cx.emit_span_lint( |
| 204 | + NON_LOCAL_DEFINITIONS, |
| 205 | + item.span, |
| 206 | + NonLocalDefinitionsDiag::Impl { |
| 207 | + depth: self.body_depth, |
| 208 | + body_kind_descr: "?" /* FIXME: cx.tcx.def_kind_descr(parent_def_kind, parent) */, |
| 209 | + body_name: parent_node() |
| 210 | + .ident() |
| 211 | + .map(|s| s.name.to_ident_string()) |
| 212 | + .unwrap_or_else(|| "<unnameable>".to_string()), |
| 213 | + cargo_update: cargo_update(), |
| 214 | + const_anon, |
| 215 | + }, |
| 216 | + ) |
| 217 | + } |
| 218 | + } |
221 | 219 | ItemKind::Macro(_macro, MacroKind::Bang)
|
222 | 220 | if cx.tcx.has_attr(item.owner_id.def_id, sym::macro_export) =>
|
223 | 221 | {
|
|
0 commit comments