Skip to content

Add ast ident iterator #6100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 52 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
2ec310c
copy in-progress IdentIter over from add-suspicious_chained_operators…
Ryan1729 Sep 30, 2020
5ff8eb2
rename IdentIter struct to ExprIdentIter, and make `IdentIter` mean a…
Ryan1729 Oct 1, 2020
7ade515
add initial version of `TyIdentIter`
Ryan1729 Oct 1, 2020
0a234a2
handle `TyKind::Array`
Ryan1729 Oct 2, 2020
c5cc4db
handle `TyKind::Ptr` and `TyKind::Paren`
Ryan1729 Oct 2, 2020
65239bc
handle `TyKind::Rptr`
Ryan1729 Oct 2, 2020
8d5d93e
handle `TyKind::Tup`
Ryan1729 Oct 2, 2020
6bdf624
handle `TyKind::ImplicitSelf` and `TyKind::Typeof`
Ryan1729 Oct 2, 2020
a7810ec
handle `TyKind::Path` and `TyKind::MacCall`
Ryan1729 Oct 2, 2020
0b579c9
create `GenericBoundIdentIter` and have it handle `TyKind::TraitObjec…
Ryan1729 Oct 2, 2020
f27e2fd
handle `GenericBound::Trait`
Ryan1729 Oct 2, 2020
229c1b1
handle `TyKind::BareFn`
Ryan1729 Oct 2, 2020
be5a4c7
handle `PatKind::Ident`
Ryan1729 Oct 4, 2020
7396d62
handle `PatKind::Struct`
Ryan1729 Oct 4, 2020
d6b6976
handle `PatKind::TupleStruct`
Ryan1729 Oct 4, 2020
f04852a
handle `PatKind::Or`, `PatKind::Tuple` and `PatKind::Slice`
Ryan1729 Oct 4, 2020
507045a
handle `PatKind::Box`, `PatKind::Ref` and `PatKind::Paren`
Ryan1729 Oct 4, 2020
4b7ac8c
handle `PatKind::Path`, `PatKind::MacCall` and `PatKind::Lit`
Ryan1729 Oct 4, 2020
36863c6
handle `PatKind::Range`
Ryan1729 Oct 4, 2020
8bffebe
handle `ExprKind::Let`
Ryan1729 Oct 4, 2020
207f007
handle `ExprKind::If` and create initial version of `StmtIdentIter`
Ryan1729 Oct 4, 2020
7822ddd
handle `StmtKind::Local`
Ryan1729 Oct 5, 2020
85d1c86
handle `StmtKind::MacCall`
Ryan1729 Oct 5, 2020
5971e15
handle `StmtKind::Item` and create `ItemIdentIter`
Ryan1729 Oct 5, 2020
7fb2daf
handle `ItemKind::Use`
Ryan1729 Oct 5, 2020
ec60753
handle `ItemKind::Static` and `ItemKind::Const`
Ryan1729 Oct 5, 2020
cf6295e
handle `ExprKind::While`
Ryan1729 Oct 5, 2020
4cc78c3
handle `ExprKind::ForLoop`
Ryan1729 Oct 5, 2020
434475c
handle `ItemKind::Fn` and start on `generics_iter`
Ryan1729 Oct 5, 2020
f28805f
handle `WherePredicate::BoundPredicate`
Ryan1729 Oct 6, 2020
565a9e1
handle `WherePredicate::RegionPredicate`
Ryan1729 Oct 6, 2020
80e0903
handle `ItemKind::Mod`
Ryan1729 Oct 6, 2020
a815745
include `Item`'s own `Ident` in iteration
Ryan1729 Oct 6, 2020
02792a1
handle `ItemKind::ForeignMod` and add `ForeignItemIdentIter`
Ryan1729 Oct 6, 2020
1592c50
handle `ItemKind::GlobalAsm` and `ItemKind::MacroDef`
Ryan1729 Oct 7, 2020
68a1a45
handle `ItemKind::TyAlias` and `ForeignItemKind::TyAlias`
Ryan1729 Oct 7, 2020
8d95da8
include visibility from `Item`s and `ForeignItem`s
Ryan1729 Oct 7, 2020
0a7e524
handle `ItemKind::Enum`
Ryan1729 Oct 9, 2020
56fc9b5
handle `ItemKind::Struct` and `ItemKind::Union`
Ryan1729 Oct 9, 2020
f76112b
handle `ItemKind::Trait`
Ryan1729 Oct 9, 2020
12700d1
handle `MacCall` in the three Item kinds
Ryan1729 Oct 9, 2020
05c77b9
handle `ItemKind::TraitAlias`
Ryan1729 Oct 9, 2020
c751029
use glob import and allow `enum_glob_use`
Ryan1729 Oct 11, 2020
0da001f
use the From trait instead of `from_expr/ty`
Ryan1729 Oct 11, 2020
d28d899
Massively simplify `ident_iter` module by using `Visitor::visit_ident`
Ryan1729 Oct 12, 2020
2a0d2e0
run `cargo dev fmt`
Ryan1729 Oct 12, 2020
f81a4cc
fix typo
Ryan1729 Oct 13, 2020
de6fd37
correct capacity calculation math, and adjust estimated const value
Ryan1729 Oct 13, 2020
326be80
rename `IdentIterator` to `IdentIter` and refactor `IdentCollector` c…
Ryan1729 Oct 13, 2020
08c18ac
add `From<&Attribute> for IdentIter` implementation
Ryan1729 Oct 13, 2020
61bfe87
respond to `trivially_copy_pass_by_ref` lint + run `cargo dev fmt`
Ryan1729 Oct 14, 2020
6b3ebfe
simplify `IdentCollector`
Ryan1729 Oct 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clippy_lints/src/utils/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use rustc_ast::{self as ast, *};
use rustc_span::symbol::Ident;
use std::mem;

pub mod ident_iter;

/// Checks if each element in the first slice is contained within the latter as per `eq_fn`.
pub fn unordered_over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
left.len() == right.len() && left.iter().all(|l| right.iter().any(|r| eq_fn(l, r)))
Expand Down
45 changes: 45 additions & 0 deletions clippy_lints/src/utils/ast_utils/ident_iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use core::iter::FusedIterator;
use rustc_ast::visit::{walk_attribute, walk_expr, Visitor};
use rustc_ast::{Attribute, Expr};
use rustc_span::symbol::Ident;

pub struct IdentIter(std::vec::IntoIter<Ident>);

impl Iterator for IdentIter {
type Item = Ident;

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}

impl FusedIterator for IdentIter {}

impl From<&Expr> for IdentIter {
fn from(expr: &Expr) -> Self {
let mut visitor = IdentCollector::default();

walk_expr(&mut visitor, expr);

IdentIter(visitor.0.into_iter())
}
}

impl From<&Attribute> for IdentIter {
fn from(attr: &Attribute) -> Self {
let mut visitor = IdentCollector::default();

walk_attribute(&mut visitor, attr);

IdentIter(visitor.0.into_iter())
}
}

#[derive(Default)]
struct IdentCollector(Vec<Ident>);

impl Visitor<'_> for IdentCollector {
fn visit_ident(&mut self, ident: Ident) {
self.0.push(ident);
}
}