|
| 1 | +use clippy_utils::paths::{PathLookup, PathNS, lookup_path_str}; |
| 2 | +use clippy_utils::sym::Symbol; |
| 3 | +use clippy_utils::ty::implements_trait; |
| 4 | +use clippy_utils::usage::local_used_after_expr; |
| 5 | +use clippy_utils::{diagnostics::span_lint_and_help, is_self}; |
| 6 | +use clippy_utils::{ |
| 7 | + get_expr_use_or_unification_node, get_parent_expr, is_expr_final_block_expr, is_trait_method, |
| 8 | + path_def_id, peel_blocks, potential_return_of_enclosing_body, |
| 9 | +}; |
| 10 | +use rustc_hir::{Body, FnDecl, def_id::LocalDefId, intravisit::FnKind}; |
| 11 | +use rustc_hir::{Expr, ExprKind, LetStmt, Node}; |
| 12 | +use rustc_lint::{LateContext, LateLintPass}; |
| 13 | +use rustc_span::symbol::Symbol; |
| 14 | + |
| 15 | +use crate::{is_scoped_ty, method_call}; |
| 16 | + |
| 17 | +dylint_linting::declare_late_lint! { |
| 18 | + /// ### What it does |
| 19 | + /// |
| 20 | + /// Makes sure that the user immediately binds `Scoped<Value>::get` results. |
| 21 | + /// |
| 22 | + /// ### Why is this bad? |
| 23 | + /// |
| 24 | + /// TODO: Write an explanation of why this is bad. |
| 25 | + /// |
| 26 | + /// ### Example |
| 27 | + /// |
| 28 | + /// ``` |
| 29 | + /// let a = scoped_a.get(agent); |
| 30 | + /// ``` |
| 31 | + /// |
| 32 | + /// Use instead: |
| 33 | + /// |
| 34 | + /// ``` |
| 35 | + /// let a = scoped_a.get(agent).bind(gc.nogc()); |
| 36 | + /// ``` |
| 37 | + /// |
| 38 | + /// Which ensures that no odd bugs occur. |
| 39 | + /// |
| 40 | + /// ### Exception: If the result is immediately used without assigning to a |
| 41 | + /// variable, binding can be skipped. |
| 42 | + /// |
| 43 | + /// ``` |
| 44 | + /// scoped_a.get(agent).internal_delete(agent, scoped_b.get(agent), gc.reborrow()); |
| 45 | + /// ``` |
| 46 | + /// |
| 47 | + /// Here it is perfectly okay to skip the binding for both `scoped_a` and |
| 48 | + /// `scoped_b` as the borrow checker would force you to again unbind both |
| 49 | + /// `Value`s immediately. |
| 50 | + pub IMMEDIATELY_BIND_SCOPED, |
| 51 | + Deny, |
| 52 | + "the result of `Scoped<Value>::get` should be immediately bound" |
| 53 | +} |
| 54 | + |
| 55 | +impl<'tcx> LateLintPass<'tcx> for ImmediatelyBindScoped { |
| 56 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 57 | + // First we check if we have found a `Scoped<Value>::get` call |
| 58 | + if let Some((method, recv, _, _, _)) = method_call(expr) |
| 59 | + && method == "get" |
| 60 | + && let typeck_results = cx.typeck_results() |
| 61 | + && let recv_ty = typeck_results.expr_ty(recv) |
| 62 | + && is_scoped_ty(cx, &recv_ty) |
| 63 | + { |
| 64 | + // Which is followed by a trait method call to `bind` in which case |
| 65 | + // it is all done properly and we can exit out of the lint |
| 66 | + if let Some(parent) = get_parent_expr(cx, expr) |
| 67 | + && let Some((parent_method, _, _, _, _)) = method_call(parent) |
| 68 | + && parent_method == "bind" |
| 69 | + && let parent_ty = typeck_results.expr_ty(parent) |
| 70 | + && let Some(&trait_def_id) = |
| 71 | + lookup_path_str(cx.tcx, PathNS::Type, "nova_vm::engine::context::Bindable") |
| 72 | + .first() |
| 73 | + && implements_trait(cx, parent_ty, trait_def_id, &[]) |
| 74 | + { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Now we are onto something! If the expression is returned, used |
| 79 | + // after the expression or assigned to a variable we might have |
| 80 | + // found an issue. |
| 81 | + if let Some((usage, hir_id)) = get_expr_use_or_unification_node(cx.tcx, expr) |
| 82 | + && (potential_return_of_enclosing_body(cx, expr) |
| 83 | + || local_used_after_expr(cx, hir_id, expr) |
| 84 | + || matches!( |
| 85 | + usage, |
| 86 | + Node::LetStmt(LetStmt { |
| 87 | + init: Some(hir_id), |
| 88 | + .. |
| 89 | + }) |
| 90 | + )) |
| 91 | + { |
| 92 | + span_lint_and_help( |
| 93 | + cx, |
| 94 | + IMMEDIATELY_BIND_SCOPED, |
| 95 | + expr.span, |
| 96 | + "the result of `Scoped<Value>::get` should be immediately bound", |
| 97 | + None, |
| 98 | + "immediately bind the value", |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments