|
| 1 | +//! Utilities for analyzing the contents of expressions and other [visitable](Visitable) items. |
| 2 | +
|
1 | 3 | use crate::get_enclosing_block; |
2 | 4 | use crate::msrvs::Msrv; |
3 | 5 | use crate::qualify_min_const_fn::is_stable_const_fn; |
@@ -36,7 +38,9 @@ impl Continue for () { |
36 | 38 | /// descending into child nodes. |
37 | 39 | #[derive(Clone, Copy)] |
38 | 40 | pub enum Descend { |
| 41 | + /// Controlled [`Visitor`] should descend into child nodes. |
39 | 42 | Yes, |
| 43 | + /// Controlled [`Visitor`] should not descend into child nodes. |
40 | 44 | No, |
41 | 45 | } |
42 | 46 | impl From<bool> for Descend { |
@@ -204,6 +208,9 @@ fn contains_try(expr: &Expr<'_>) -> bool { |
204 | 208 | .is_some() |
205 | 209 | } |
206 | 210 |
|
| 211 | +/// Calls a given function of the form `|return_expr: &Expr| -> bool` for all `return` expressions |
| 212 | +/// in the given [expression](Expr), and returns whether that callback returned true for all found |
| 213 | +/// `return`s. |
207 | 214 | pub fn find_all_ret_expressions<'hir, F>(_cx: &LateContext<'_>, expr: &'hir Expr<'hir>, callback: F) -> bool |
208 | 215 | where |
209 | 216 | F: FnMut(&'hir Expr<'hir>) -> bool, |
@@ -589,9 +596,11 @@ pub fn for_each_local_use_after_expr<'tcx, B>( |
589 | 596 | } |
590 | 597 | } |
591 | 598 |
|
592 | | -// Calls the given function for every unconsumed temporary created by the expression. Note the |
593 | | -// function is only guaranteed to be called for types which need to be dropped, but it may be called |
594 | | -// for other types. |
| 599 | +/// Calls a given function of the form `|temporary_type: Ty| -> ControlFlow<B>` for every unconsumed |
| 600 | +/// temporary created by the given [expression](Expr), and returns the result of that function. |
| 601 | +/// |
| 602 | +/// Note the function is only guaranteed to be called for types which need to be dropped, but it may |
| 603 | +/// be called for other types. |
595 | 604 | #[expect(clippy::too_many_lines)] |
596 | 605 | pub fn for_each_unconsumed_temporary<'tcx, B>( |
597 | 606 | cx: &LateContext<'tcx>, |
@@ -708,6 +717,8 @@ pub fn for_each_unconsumed_temporary<'tcx, B>( |
708 | 717 | helper(cx.typeck_results(), true, e, &mut f) |
709 | 718 | } |
710 | 719 |
|
| 720 | +/// Checks whether the drop order matters for any unconsumed temporary created by the given |
| 721 | +/// [expression](Expr). |
711 | 722 | pub fn any_temporaries_need_ordered_drop<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool { |
712 | 723 | for_each_unconsumed_temporary(cx, e, |ty| { |
713 | 724 | if needs_ordered_drop(cx, ty) { |
@@ -765,6 +776,8 @@ pub fn for_each_local_assignment<'tcx, B>( |
765 | 776 | } |
766 | 777 | } |
767 | 778 |
|
| 779 | +/// Checks whether the given [expression](Expr) contains any `break` or `continue` expressions. This |
| 780 | +/// does not enter any bodies or nested items. |
768 | 781 | pub fn contains_break_or_continue(expr: &Expr<'_>) -> bool { |
769 | 782 | for_each_expr_without_closures(expr, |e| { |
770 | 783 | if matches!(e.kind, ExprKind::Break(..) | ExprKind::Continue(..)) { |
|
0 commit comments