]> git.lizzy.rs Git - rust.git/blob - clippy_utils/src/ast_utils/ident_iter.rs
Auto merge of #7774 - dswij:useless-exponent, r=llogiq
[rust.git] / clippy_utils / src / ast_utils / ident_iter.rs
1 use core::iter::FusedIterator;
2 use rustc_ast::visit::{walk_attribute, walk_expr, Visitor};
3 use rustc_ast::{Attribute, Expr};
4 use rustc_span::symbol::Ident;
5
6 pub struct IdentIter(std::vec::IntoIter<Ident>);
7
8 impl Iterator for IdentIter {
9     type Item = Ident;
10
11     fn next(&mut self) -> Option<Self::Item> {
12         self.0.next()
13     }
14 }
15
16 impl FusedIterator for IdentIter {}
17
18 impl From<&Expr> for IdentIter {
19     fn from(expr: &Expr) -> Self {
20         let mut visitor = IdentCollector::default();
21
22         walk_expr(&mut visitor, expr);
23
24         IdentIter(visitor.0.into_iter())
25     }
26 }
27
28 impl From<&Attribute> for IdentIter {
29     fn from(attr: &Attribute) -> Self {
30         let mut visitor = IdentCollector::default();
31
32         walk_attribute(&mut visitor, attr);
33
34         IdentIter(visitor.0.into_iter())
35     }
36 }
37
38 #[derive(Default)]
39 struct IdentCollector(Vec<Ident>);
40
41 impl Visitor<'_> for IdentCollector {
42     fn visit_ident(&mut self, ident: Ident) {
43         self.0.push(ident);
44     }
45 }