clippy_utils/
higher.rs

1//! This module contains functions that retrieve specific elements.
2
3#![deny(clippy::missing_docs_in_private_items)]
4
5use crate::consts::{ConstEvalCtxt, Constant};
6use crate::res::MaybeDef;
7use crate::{is_expn_of, sym};
8
9use rustc_ast::ast;
10use rustc_hir as hir;
11use rustc_hir::{Arm, Block, Expr, ExprKind, HirId, LoopSource, MatchSource, Node, Pat, QPath, StructTailExpr};
12use rustc_lint::LateContext;
13use rustc_span::{Span, symbol};
14
15/// The essential nodes of a desugared for loop as well as the entire span:
16/// `for pat in arg { body }` becomes `(pat, arg, body)`. Returns `(pat, arg, body, span)`.
17#[derive(Debug)]
18pub struct ForLoop<'tcx> {
19    /// `for` loop item
20    pub pat: &'tcx Pat<'tcx>,
21    /// `IntoIterator` argument
22    pub arg: &'tcx Expr<'tcx>,
23    /// `for` loop body
24    pub body: &'tcx Expr<'tcx>,
25    /// Compare this against `hir::Destination.target`
26    pub loop_id: HirId,
27    /// entire `for` loop span
28    pub span: Span,
29    /// label
30    pub label: Option<ast::Label>,
31}
32
33impl<'tcx> ForLoop<'tcx> {
34    /// Parses a desugared `for` loop
35    pub fn hir(expr: &Expr<'tcx>) -> Option<Self> {
36        if let ExprKind::DropTemps(e) = expr.kind
37            && let ExprKind::Match(iterexpr, [arm], MatchSource::ForLoopDesugar) = e.kind
38            && let ExprKind::Call(_, [arg]) = iterexpr.kind
39            && let ExprKind::Loop(block, label, ..) = arm.body.kind
40            && let [stmt] = block.stmts
41            && let hir::StmtKind::Expr(e) = stmt.kind
42            && let ExprKind::Match(_, [_, some_arm], _) = e.kind
43            && let hir::PatKind::Struct(_, [field], _) = some_arm.pat.kind
44        {
45            return Some(Self {
46                pat: field.pat,
47                arg,
48                body: some_arm.body,
49                loop_id: arm.body.hir_id,
50                span: expr.span.ctxt().outer_expn_data().call_site,
51                label,
52            });
53        }
54        None
55    }
56}
57
58/// An `if` expression without `let`
59pub struct If<'hir> {
60    /// `if` condition
61    pub cond: &'hir Expr<'hir>,
62    /// `if` then expression
63    pub then: &'hir Expr<'hir>,
64    /// `else` expression
65    pub r#else: Option<&'hir Expr<'hir>>,
66}
67
68impl<'hir> If<'hir> {
69    #[inline]
70    /// Parses an `if` expression without `let`
71    pub const fn hir(expr: &Expr<'hir>) -> Option<Self> {
72        if let ExprKind::If(cond, then, r#else) = expr.kind
73            && !has_let_expr(cond)
74        {
75            Some(Self { cond, then, r#else })
76        } else {
77            None
78        }
79    }
80}
81
82/// An `if let` expression
83pub struct IfLet<'hir> {
84    /// `if let` pattern
85    pub let_pat: &'hir Pat<'hir>,
86    /// `if let` scrutinee
87    pub let_expr: &'hir Expr<'hir>,
88    /// `if let` then expression
89    pub if_then: &'hir Expr<'hir>,
90    /// `if let` else expression
91    pub if_else: Option<&'hir Expr<'hir>>,
92    /// `if let PAT = EXPR`
93    ///     ^^^^^^^^^^^^^^
94    pub let_span: Span,
95}
96
97impl<'hir> IfLet<'hir> {
98    /// Parses an `if let` expression
99    pub fn hir(cx: &LateContext<'_>, expr: &Expr<'hir>) -> Option<Self> {
100        if let ExprKind::If(
101            &Expr {
102                kind:
103                    ExprKind::Let(&hir::LetExpr {
104                        pat: let_pat,
105                        init: let_expr,
106                        span: let_span,
107                        ..
108                    }),
109                ..
110            },
111            if_then,
112            if_else,
113        ) = expr.kind
114        {
115            let mut iter = cx.tcx.hir_parent_iter(expr.hir_id);
116            if let Some((_, Node::Block(Block { stmts: [], .. }))) = iter.next()
117                && let Some((
118                    _,
119                    Node::Expr(Expr {
120                        kind: ExprKind::Loop(_, _, LoopSource::While, _),
121                        ..
122                    }),
123                )) = iter.next()
124            {
125                // while loop desugar
126                return None;
127            }
128            return Some(Self {
129                let_pat,
130                let_expr,
131                if_then,
132                if_else,
133                let_span,
134            });
135        }
136        None
137    }
138}
139
140/// An `if let` or `match` expression. Useful for lints that trigger on one or the other.
141#[derive(Debug)]
142pub enum IfLetOrMatch<'hir> {
143    /// Any `match` expression
144    Match(&'hir Expr<'hir>, &'hir [Arm<'hir>], MatchSource),
145    /// scrutinee, pattern, then block, else block
146    IfLet(
147        &'hir Expr<'hir>,
148        &'hir Pat<'hir>,
149        &'hir Expr<'hir>,
150        Option<&'hir Expr<'hir>>,
151        /// `if let PAT = EXPR`
152        ///     ^^^^^^^^^^^^^^
153        Span,
154    ),
155}
156
157impl<'hir> IfLetOrMatch<'hir> {
158    /// Parses an `if let` or `match` expression
159    pub fn parse(cx: &LateContext<'_>, expr: &Expr<'hir>) -> Option<Self> {
160        match expr.kind {
161            ExprKind::Match(expr, arms, source) => Some(Self::Match(expr, arms, source)),
162            _ => IfLet::hir(cx, expr).map(
163                |IfLet {
164                     let_expr,
165                     let_pat,
166                     if_then,
167                     if_else,
168                     let_span,
169                 }| { Self::IfLet(let_expr, let_pat, if_then, if_else, let_span) },
170            ),
171        }
172    }
173
174    pub fn scrutinee(&self) -> &'hir Expr<'hir> {
175        match self {
176            Self::Match(scrutinee, _, _) | Self::IfLet(scrutinee, _, _, _, _) => scrutinee,
177        }
178    }
179}
180
181/// An `if` or `if let` expression
182pub struct IfOrIfLet<'hir> {
183    /// `if` condition that is maybe a `let` expression
184    pub cond: &'hir Expr<'hir>,
185    /// `if` then expression
186    pub then: &'hir Expr<'hir>,
187    /// `else` expression
188    pub r#else: Option<&'hir Expr<'hir>>,
189}
190
191impl<'hir> IfOrIfLet<'hir> {
192    #[inline]
193    /// Parses an `if` or `if let` expression
194    pub const fn hir(expr: &Expr<'hir>) -> Option<Self> {
195        if let ExprKind::If(cond, then, r#else) = expr.kind {
196            Some(Self { cond, then, r#else })
197        } else {
198            None
199        }
200    }
201}
202
203/// Represent a range akin to `ast::ExprKind::Range`.
204#[derive(Debug, Copy, Clone)]
205pub struct Range<'a> {
206    /// The lower bound of the range, or `None` for ranges such as `..X`.
207    pub start: Option<&'a Expr<'a>>,
208    /// The upper bound of the range, or `None` for ranges such as `X..`.
209    pub end: Option<&'a Expr<'a>>,
210    /// Whether the interval is open or closed.
211    pub limits: ast::RangeLimits,
212    pub span: Span,
213}
214
215impl<'a> Range<'a> {
216    /// Higher a `hir` range to something similar to `ast::ExprKind::Range`.
217    #[expect(clippy::similar_names)]
218    pub fn hir(cx: &LateContext<'_>, expr: &'a Expr<'_>) -> Option<Range<'a>> {
219        let span = expr.range_span()?;
220        match expr.kind {
221            ExprKind::Call(path, [arg1, arg2])
222                if let ExprKind::Path(qpath) = path.kind
223                    && cx.tcx.qpath_is_lang_item(qpath, hir::LangItem::RangeInclusiveNew) =>
224            {
225                Some(Range {
226                    start: Some(arg1),
227                    end: Some(arg2),
228                    limits: ast::RangeLimits::Closed,
229                    span,
230                })
231            },
232            ExprKind::Struct(&qpath, fields, StructTailExpr::None) => match (cx.tcx.qpath_lang_item(qpath)?, fields) {
233                (hir::LangItem::RangeFull, []) => Some(Range {
234                    start: None,
235                    end: None,
236                    limits: ast::RangeLimits::HalfOpen,
237                    span,
238                }),
239                (hir::LangItem::RangeFrom, [field]) if field.ident.name == sym::start => Some(Range {
240                    start: Some(field.expr),
241                    end: None,
242                    limits: ast::RangeLimits::HalfOpen,
243                    span,
244                }),
245                (hir::LangItem::Range, [field1, field2]) => {
246                    let (start, end) = match (field1.ident.name, field2.ident.name) {
247                        (sym::start, sym::end) => (field1.expr, field2.expr),
248                        (sym::end, sym::start) => (field2.expr, field1.expr),
249                        _ => return None,
250                    };
251                    Some(Range {
252                        start: Some(start),
253                        end: Some(end),
254                        limits: ast::RangeLimits::HalfOpen,
255                        span,
256                    })
257                },
258                (hir::LangItem::RangeToInclusive, [field]) if field.ident.name == sym::end => Some(Range {
259                    start: None,
260                    end: Some(field.expr),
261                    limits: ast::RangeLimits::Closed,
262                    span,
263                }),
264                (hir::LangItem::RangeTo, [field]) if field.ident.name == sym::end => Some(Range {
265                    start: None,
266                    end: Some(field.expr),
267                    limits: ast::RangeLimits::HalfOpen,
268                    span,
269                }),
270                _ => None,
271            },
272            _ => None,
273        }
274    }
275}
276
277/// Represents the pre-expansion arguments of a `vec!` invocation.
278pub enum VecArgs<'a> {
279    /// `vec![elem; len]`
280    Repeat(&'a Expr<'a>, &'a Expr<'a>),
281    /// `vec![a, b, c]`
282    Vec(&'a [Expr<'a>]),
283}
284
285impl<'a> VecArgs<'a> {
286    /// Returns the arguments of the `vec!` macro if this expression was expanded
287    /// from `vec!`.
288    pub fn hir(cx: &LateContext<'_>, expr: &'a Expr<'_>) -> Option<VecArgs<'a>> {
289        if let ExprKind::Call(fun, args) = expr.kind
290            && let ExprKind::Path(ref qpath) = fun.kind
291            && is_expn_of(fun.span, sym::vec).is_some()
292            && let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id()
293            && let Some(name) = cx.tcx.get_diagnostic_name(fun_def_id)
294        {
295            return match (name, args) {
296                (sym::vec_from_elem, [elem, size]) => {
297                    // `vec![elem; size]` case
298                    Some(VecArgs::Repeat(elem, size))
299                },
300                (sym::slice_into_vec, [slice])
301                    if let ExprKind::Call(_, [arg]) = slice.kind
302                        && let ExprKind::Array(args) = arg.kind =>
303                {
304                    // `vec![a, b, c]` case
305                    Some(VecArgs::Vec(args))
306                },
307                (sym::vec_new, []) => Some(VecArgs::Vec(&[])),
308                _ => None,
309            };
310        }
311
312        None
313    }
314}
315
316/// A desugared `while` loop
317pub struct While<'hir> {
318    /// `while` loop condition
319    pub condition: &'hir Expr<'hir>,
320    /// `while` loop body
321    pub body: &'hir Expr<'hir>,
322    /// Span of the loop header
323    pub span: Span,
324    pub label: Option<ast::Label>,
325}
326
327impl<'hir> While<'hir> {
328    #[inline]
329    /// Parses a desugared `while` loop
330    pub const fn hir(expr: &Expr<'hir>) -> Option<Self> {
331        if let ExprKind::Loop(
332            Block {
333                expr:
334                    Some(Expr {
335                        kind: ExprKind::If(condition, body, _),
336                        ..
337                    }),
338                ..
339            },
340            label,
341            LoopSource::While,
342            span,
343        ) = expr.kind
344            && !has_let_expr(condition)
345        {
346            return Some(Self {
347                condition,
348                body,
349                span,
350                label,
351            });
352        }
353        None
354    }
355}
356
357/// A desugared `while let` loop
358pub struct WhileLet<'hir> {
359    /// `while let` loop item pattern
360    pub let_pat: &'hir Pat<'hir>,
361    /// `while let` loop scrutinee
362    pub let_expr: &'hir Expr<'hir>,
363    /// `while let` loop body
364    pub if_then: &'hir Expr<'hir>,
365    pub label: Option<ast::Label>,
366    /// `while let PAT = EXPR`
367    ///        ^^^^^^^^^^^^^^
368    pub let_span: Span,
369}
370
371impl<'hir> WhileLet<'hir> {
372    #[inline]
373    /// Parses a desugared `while let` loop
374    pub const fn hir(expr: &Expr<'hir>) -> Option<Self> {
375        if let ExprKind::Loop(
376            &Block {
377                expr:
378                    Some(&Expr {
379                        kind:
380                            ExprKind::If(
381                                &Expr {
382                                    kind:
383                                        ExprKind::Let(&hir::LetExpr {
384                                            pat: let_pat,
385                                            init: let_expr,
386                                            span: let_span,
387                                            ..
388                                        }),
389                                    ..
390                                },
391                                if_then,
392                                _,
393                            ),
394                        ..
395                    }),
396                ..
397            },
398            label,
399            LoopSource::While,
400            _,
401        ) = expr.kind
402        {
403            return Some(Self {
404                let_pat,
405                let_expr,
406                if_then,
407                label,
408                let_span,
409            });
410        }
411        None
412    }
413}
414
415/// Converts a `hir` binary operator to the corresponding `ast` type.
416#[must_use]
417pub fn binop(op: hir::BinOpKind) -> ast::BinOpKind {
418    match op {
419        hir::BinOpKind::Eq => ast::BinOpKind::Eq,
420        hir::BinOpKind::Ge => ast::BinOpKind::Ge,
421        hir::BinOpKind::Gt => ast::BinOpKind::Gt,
422        hir::BinOpKind::Le => ast::BinOpKind::Le,
423        hir::BinOpKind::Lt => ast::BinOpKind::Lt,
424        hir::BinOpKind::Ne => ast::BinOpKind::Ne,
425        hir::BinOpKind::Or => ast::BinOpKind::Or,
426        hir::BinOpKind::Add => ast::BinOpKind::Add,
427        hir::BinOpKind::And => ast::BinOpKind::And,
428        hir::BinOpKind::BitAnd => ast::BinOpKind::BitAnd,
429        hir::BinOpKind::BitOr => ast::BinOpKind::BitOr,
430        hir::BinOpKind::BitXor => ast::BinOpKind::BitXor,
431        hir::BinOpKind::Div => ast::BinOpKind::Div,
432        hir::BinOpKind::Mul => ast::BinOpKind::Mul,
433        hir::BinOpKind::Rem => ast::BinOpKind::Rem,
434        hir::BinOpKind::Shl => ast::BinOpKind::Shl,
435        hir::BinOpKind::Shr => ast::BinOpKind::Shr,
436        hir::BinOpKind::Sub => ast::BinOpKind::Sub,
437    }
438}
439
440/// A parsed `Vec` initialization expression
441#[derive(Clone, Copy)]
442pub enum VecInitKind {
443    /// `Vec::new()`
444    New,
445    /// `Vec::default()` or `Default::default()`
446    Default,
447    /// `Vec::with_capacity(123)`
448    WithConstCapacity(u128),
449    /// `Vec::with_capacity(slice.len())`
450    WithExprCapacity(HirId),
451}
452
453/// Checks if the given expression is an initialization of `Vec` and returns its kind.
454pub fn get_vec_init_kind<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<VecInitKind> {
455    if let ExprKind::Call(func, args) = expr.kind {
456        match func.kind {
457            ExprKind::Path(QPath::TypeRelative(ty, name))
458                if cx.typeck_results().node_type(ty.hir_id).is_diag_item(cx, sym::Vec) =>
459            {
460                if name.ident.name == sym::new {
461                    return Some(VecInitKind::New);
462                } else if name.ident.name == symbol::kw::Default {
463                    return Some(VecInitKind::Default);
464                } else if name.ident.name == sym::with_capacity {
465                    let arg = args.first()?;
466                    return match ConstEvalCtxt::new(cx).eval_local(arg, expr.span.ctxt()) {
467                        Some(Constant::Int(num)) => Some(VecInitKind::WithConstCapacity(num)),
468                        _ => Some(VecInitKind::WithExprCapacity(arg.hir_id)),
469                    };
470                }
471            },
472            ExprKind::Path(QPath::Resolved(_, path))
473                if cx.tcx.is_diagnostic_item(sym::default_fn, path.res.opt_def_id()?)
474                    && cx.typeck_results().expr_ty(expr).is_diag_item(cx, sym::Vec) =>
475            {
476                return Some(VecInitKind::Default);
477            },
478            _ => (),
479        }
480    }
481    None
482}
483
484/// Checks that a condition doesn't have a `let` expression, to keep `If` and `While` from accepting
485/// `if let` and `while let`.
486pub const fn has_let_expr<'tcx>(cond: &'tcx Expr<'tcx>) -> bool {
487    match &cond.kind {
488        ExprKind::Let(_) => true,
489        ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs),
490        _ => false,
491    }
492}