marker_utils 0.5.0

Marker's standard library for creating lints
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use std::ops::ControlFlow;

use marker_api::{
    ast::{EnumVariant, ItemField},
    prelude::*,
};

/// This enum defines the scope that a given [`Visitor`] should traverse.
///
/// By default, it's recommended to not visit any nested bodies. Most analysis operates
/// on the item or expression level. Variables and control flow statements are only
/// effective in the current body. Visiting nested bodies, can add a bunch of noise and
/// cause confusing results. Here is an example:
///
/// ```
/// fn foo() {
///     let x = 0;
///     let a = 1;
///
///     // A function inside `foo()` with it's own body. This one will only
///     // be visited if `VisitorScope::AllBodies` is specified.
///     fn bar(x: u32) {
///         // The printed `x` is different from the `x` of `foo()`
///         // since it comes from the function parameter of `bar()`
///         println!("The magic number is {x}");
///         // The `return` statement only affects the `bar` function.
///         // `foo()` will just continue executing, when this is called
///         return;
///     }
///
///     bar(a);
/// }
/// ```
///
/// The target scope is checked when the respective `traverse_*` function is called
/// For example, [`traverse_body`] will visit a given body [`Body`](ast::Body), but
/// will not enter nested bodies, unless [`AllBodies`](VisitorScope::AllBodies) is
/// defined.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, Default)]
pub enum VisitorScope {
    /// All bodies are visited, this includes bodies from nested items and closures.
    ///
    /// Only use this, if you're sure that you need the context of nested bodies, as the
    /// traversal of everything, can be expensive in comparison to the
    /// [`NoBodies`](VisitorScope::NoBodies) scope.
    AllBodies,
    /// This visits every node, in the current scope, but won't enter nested bodies.
    ///
    /// This is a good default, if you only want to analyze the context of the current
    /// item or expression.
    #[default]
    NoBodies,
}

pub trait Visitor<B> {
    /// Defines the [`scope`](VisitorScope) this visitor should use.
    ///
    /// This should return a constant value. The `traverse_*` functions might only
    /// check the scope once and cache the result.
    fn scope(&self) -> VisitorScope {
        VisitorScope::NoBodies
    }

    fn visit_item<'ast>(&mut self, _cx: &'ast MarkerContext<'ast>, _item: ItemKind<'ast>) -> ControlFlow<B> {
        ControlFlow::Continue(())
    }

    fn visit_field<'ast>(&mut self, _cx: &'ast MarkerContext<'ast>, _field: &'ast ItemField<'ast>) -> ControlFlow<B> {
        ControlFlow::Continue(())
    }

    fn visit_variant<'ast>(
        &mut self,
        _cx: &'ast MarkerContext<'ast>,
        _variant: &'ast EnumVariant<'ast>,
    ) -> ControlFlow<B> {
        ControlFlow::Continue(())
    }

    fn visit_body<'ast>(&mut self, _cx: &'ast MarkerContext<'ast>, _body: &'ast ast::Body<'ast>) -> ControlFlow<B> {
        ControlFlow::Continue(())
    }

    fn visit_stmt<'ast>(&mut self, _cx: &'ast MarkerContext<'ast>, _stmt: StmtKind<'ast>) -> ControlFlow<B> {
        ControlFlow::Continue(())
    }

    fn visit_expr<'ast>(&mut self, _cx: &'ast MarkerContext<'ast>, _expr: ExprKind<'ast>) -> ControlFlow<B> {
        ControlFlow::Continue(())
    }
}

pub fn traverse_item<'ast, B>(
    cx: &'ast MarkerContext<'ast>,
    visitor: &mut dyn Visitor<B>,
    kind: ItemKind<'ast>,
) -> ControlFlow<B> {
    /// A small wrapper around [`traverse_body`] that checks the defined scope
    /// and validity of the [`BodyId`]
    fn traverse_body_id<'ast, B>(
        cx: &'ast MarkerContext<'ast>,
        visitor: &mut dyn Visitor<B>,
        id: Option<BodyId>,
    ) -> ControlFlow<B> {
        // Requesting the body from the API might be expensive. This check
        // prevents the requests if the body will not be used.
        if let VisitorScope::NoBodies = visitor.scope() {
            return ControlFlow::Continue(());
        }

        if let Some(body_id) = id {
            let body = cx.ast().body(body_id);
            traverse_body(cx, visitor, body)?;
        }

        ControlFlow::Continue(())
    }

    visitor.visit_item(cx, kind)?;

    match kind {
        ItemKind::Mod(module) => {
            for mod_item in module.items() {
                traverse_item(cx, visitor, *mod_item)?;
            }
        },
        ItemKind::Static(item) => {
            traverse_body_id(cx, visitor, item.body_id())?;
        },
        ItemKind::Const(item) => {
            traverse_body_id(cx, visitor, item.body_id())?;
        },
        ItemKind::Fn(item) => {
            traverse_body_id(cx, visitor, item.body_id())?;
        },
        ItemKind::Struct(item) => {
            for field in item.fields() {
                visitor.visit_field(cx, field)?;
            }
        },
        ItemKind::Union(item) => {
            for field in item.fields() {
                visitor.visit_field(cx, field)?;
            }
        },
        ItemKind::Enum(item) => {
            for variant in item.variants() {
                visitor.visit_variant(cx, variant)?;
                if let Some(const_expr) = variant.discriminant() {
                    traverse_expr(cx, visitor, const_expr.expr())?;
                }
            }
        },
        ItemKind::Trait(item) => {
            for assoc_item in item.items() {
                traverse_item(cx, visitor, assoc_item.as_item())?;
            }
        },
        ItemKind::Impl(item) => {
            for assoc_item in item.items() {
                traverse_item(cx, visitor, assoc_item.as_item())?;
            }
        },
        ItemKind::ExternBlock(item) => {
            for ext_item in item.items() {
                traverse_item(cx, visitor, ext_item.as_item())?;
            }
        },
        ItemKind::ExternCrate(_) | ItemKind::Use(_) | ItemKind::Unstable(_) | ItemKind::TyAlias(_) => {
            // These items have no sub nodes, which are visited by this visitor
        },
        _ => unreachable!("all items are covered"),
    }
    ControlFlow::Continue(())
}

pub fn traverse_body<'ast, B>(
    cx: &'ast MarkerContext<'ast>,
    visitor: &mut dyn Visitor<B>,
    body: &'ast ast::Body<'ast>,
) -> ControlFlow<B> {
    visitor.visit_body(cx, body)?;

    traverse_expr(cx, visitor, body.expr())?;

    ControlFlow::Continue(())
}

pub fn traverse_stmt<'ast, B>(
    cx: &'ast MarkerContext<'ast>,
    visitor: &mut dyn Visitor<B>,
    stmt: StmtKind<'ast>,
) -> ControlFlow<B> {
    visitor.visit_stmt(cx, stmt)?;

    match stmt {
        StmtKind::Item(item) => {
            traverse_item(cx, visitor, item.item())?;
        },
        StmtKind::Let(lt) => {
            if let Some(init) = lt.init() {
                traverse_expr(cx, visitor, init)?;
            }
            if let Some(els) = lt.els() {
                traverse_expr(cx, visitor, els)?;
            }
        },
        StmtKind::Expr(expr) => {
            traverse_expr(cx, visitor, expr.expr())?;
        },
        _ => unreachable!("all statements are covered"),
    }

    ControlFlow::Continue(())
}

#[allow(clippy::too_many_lines)]
pub fn traverse_expr<'ast, B>(
    cx: &'ast MarkerContext<'ast>,
    visitor: &mut dyn Visitor<B>,
    expr: ExprKind<'ast>,
) -> ControlFlow<B> {
    visitor.visit_expr(cx, expr)?;

    match expr {
        ExprKind::Block(e) => {
            for stmt in e.stmts() {
                traverse_stmt(cx, visitor, *stmt)?;
            }
            if let Some(block_expr) = e.expr() {
                traverse_expr(cx, visitor, block_expr)?;
            }
        },
        ExprKind::Closure(e) => {
            if let VisitorScope::AllBodies = visitor.scope() {
                let body = cx.ast().body(e.body_id());
                traverse_body(cx, visitor, body)?;
            }
        },
        ExprKind::UnaryOp(e) => {
            traverse_expr(cx, visitor, e.expr())?;
        },
        ExprKind::Ref(e) => {
            traverse_expr(cx, visitor, e.expr())?;
        },
        ExprKind::BinaryOp(e) => {
            traverse_expr(cx, visitor, e.left())?;
            traverse_expr(cx, visitor, e.right())?;
        },
        ExprKind::Try(e) => {
            traverse_expr(cx, visitor, e.expr())?;
        },
        ExprKind::Assign(e) => {
            traverse_expr(cx, visitor, e.value())?;
        },
        ExprKind::As(e) => {
            traverse_expr(cx, visitor, e.expr())?;
        },
        ExprKind::Call(e) => {
            traverse_expr(cx, visitor, e.func())?;
            for arg in e.args() {
                traverse_expr(cx, visitor, *arg)?;
            }
        },
        ExprKind::Method(e) => {
            traverse_expr(cx, visitor, e.receiver())?;
            for arg in e.args() {
                traverse_expr(cx, visitor, *arg)?;
            }
        },
        ExprKind::Array(e) => {
            for el in e.elements() {
                traverse_expr(cx, visitor, *el)?;
            }
            if let Some(len) = e.len() {
                traverse_expr(cx, visitor, len.expr())?;
            }
        },
        ExprKind::Tuple(e) => {
            for el in e.elements() {
                traverse_expr(cx, visitor, *el)?;
            }
        },
        ExprKind::Ctor(e) => {
            for field in e.fields() {
                traverse_expr(cx, visitor, field.expr())?;
            }
            if let Some(base) = e.base() {
                traverse_expr(cx, visitor, base)?;
            }
        },
        // I like the simplicity of the API, even if the dereference part of
        // slices is a bit annoying. But typing all of this out is kind of meh.
        // not super interesting and almost just copy pasta, but not enough for
        // a macro... Oh well, back to work
        ExprKind::Range(e) => {
            if let Some(start) = e.start() {
                traverse_expr(cx, visitor, start)?;
            }
            if let Some(end) = e.end() {
                traverse_expr(cx, visitor, end)?;
            }
        },
        ExprKind::Index(e) => {
            traverse_expr(cx, visitor, e.operand())?;
            traverse_expr(cx, visitor, e.index())?;
        },
        ExprKind::Field(e) => {
            traverse_expr(cx, visitor, e.operand())?;
        },
        ExprKind::If(e) => {
            traverse_expr(cx, visitor, e.condition())?;
            traverse_expr(cx, visitor, e.then())?;
            if let Some(els) = e.els() {
                traverse_expr(cx, visitor, els)?;
            }
        },
        ExprKind::Let(e) => {
            traverse_expr(cx, visitor, e.scrutinee())?;
        },
        ExprKind::Match(e) => {
            traverse_expr(cx, visitor, e.scrutinee())?;
            for arm in e.arms() {
                if let Some(guard) = arm.guard() {
                    traverse_expr(cx, visitor, guard)?;
                }
                traverse_expr(cx, visitor, arm.expr())?;
            }
        },
        ExprKind::Break(e) => {
            if let Some(val) = e.expr() {
                traverse_expr(cx, visitor, val)?;
            }
        },
        ExprKind::Return(e) => {
            if let Some(val) = e.expr() {
                traverse_expr(cx, visitor, val)?;
            }
        },
        ExprKind::For(e) => {
            traverse_expr(cx, visitor, e.iterable())?;
            traverse_expr(cx, visitor, e.block())?;
        },
        ExprKind::Loop(e) => {
            traverse_expr(cx, visitor, e.block())?;
        },
        ExprKind::While(e) => {
            traverse_expr(cx, visitor, e.condition())?;
            traverse_expr(cx, visitor, e.block())?;
        },
        ExprKind::Await(e) => {
            traverse_expr(cx, visitor, e.expr())?;
        },
        ExprKind::IntLit(_)
        | ExprKind::FloatLit(_)
        | ExprKind::StrLit(_)
        | ExprKind::CharLit(_)
        | ExprKind::BoolLit(_)
        | ExprKind::Unstable(_)
        | ExprKind::Path(_)
        | ExprKind::Continue(_) => {
            // These expressions have no sub nodes, which are visited by this visitor
        },
        _ => unreachable!("all expressions are covered"),
    }

    ControlFlow::Continue(())
}

/// This trait is implemented for nodes, that can be traversed by a [`Visitor`].
pub trait Traversable<'ast, B>
where
    Self: Sized + Copy,
{
    /// This calls the `traverse_*` function for the implementing node.
    fn traverse(self, cx: &'ast MarkerContext<'ast>, visitor: &mut dyn Visitor<B>) -> ControlFlow<B>;

    /// This function calls the given closure for every expression in the node. This
    /// traversal will not enter any nested bodies.
    ///
    /// This function is a simple wrapper around the [`Visitor`] trait, that is good
    /// for most use cases. For example, the following code counts the number of `if`s
    /// in a body:
    ///
    /// ```
    /// # use marker_api::prelude::*;
    /// # use std::ops::ControlFlow;
    /// # use marker_utils::visitor::Traversable;
    /// fn count_ifs<'ast>(cx: &'ast MarkerContext<'ast>, body: &'ast ast::Body<'ast>) -> u32 {
    ///     let mut count = 0;
    ///     let _: Option<()> = body.for_each_expr(
    ///         cx,
    ///         |expr| {
    ///             if matches!(expr, ExprKind::If(_)) {
    ///                 count += 1;
    ///             }
    ///             ControlFlow::Continue(())
    ///         }
    ///     );
    ///     count
    /// }
    /// ```
    fn for_each_expr<F: for<'a> FnMut(ExprKind<'a>) -> ControlFlow<B>>(
        self,
        cx: &'ast MarkerContext<'ast>,
        f: F,
    ) -> Option<B> {
        struct ExprVisitor<F> {
            f: F,
        }
        impl<B, F: for<'a> FnMut(ExprKind<'a>) -> ControlFlow<B>> Visitor<B> for ExprVisitor<F> {
            fn visit_expr<'v_ast>(
                &mut self,
                _cx: &'v_ast MarkerContext<'v_ast>,
                expr: ExprKind<'v_ast>,
            ) -> ControlFlow<B> {
                (self.f)(expr)
            }
        }
        let mut visitor = ExprVisitor { f };

        match self.traverse(cx, &mut visitor) {
            ControlFlow::Continue(()) => None,
            ControlFlow::Break(b) => Some(b),
        }
    }
}

/// This macro implements the [`Traversable`] trait for a given node that implements `Copy`
macro_rules! impl_traversable_for {
    ($ty:ty, $func:ident) => {
        impl<'ast, B> Traversable<'ast, B> for $ty {
            fn traverse(self, cx: &'ast MarkerContext<'ast>, visitor: &mut dyn Visitor<B>) -> ControlFlow<B> {
                $func(cx, visitor, self)
            }
        }
    };
}

impl_traversable_for!(ExprKind<'ast>, traverse_expr);
impl_traversable_for!(StmtKind<'ast>, traverse_stmt);
impl_traversable_for!(ItemKind<'ast>, traverse_item);
impl_traversable_for!(&'ast ast::Body<'ast>, traverse_body);

/// This trait extends the [`Traversable`] trait with more functions, specific to
/// the `bool` return type.
pub trait BoolTraversable<'ast>: Traversable<'ast, bool> {
    /// Checks if the given node contains an early return, in the form of an
    /// [`ReturnExpr`](marker_api::ast::ReturnExpr) or
    /// [`TryExpr`](marker_api::ast::TryExpr).
    ///
    /// This function is useful, for lints which suggest moving code snippets into
    /// a closure or different function. Return statements might prevent the suggested
    /// refactoring.
    fn contains_return(&self, cx: &'ast MarkerContext<'ast>) -> bool {
        self.for_each_expr(cx, |expr| {
            if matches!(expr, ExprKind::Return(_) | ExprKind::Try(_)) {
                ControlFlow::Break(true)
            } else {
                ControlFlow::Continue(())
            }
        })
        .is_some()
    }
}

impl<'ast, T: Traversable<'ast, bool>> BoolTraversable<'ast> for T {}