|
| 1 | +#![feature(rustc_private)] |
| 2 | +#![warn(unused_extern_crates)] |
| 3 | +#![feature(let_chains)] |
| 4 | + |
| 5 | +extern crate rustc_errors; |
| 6 | +extern crate rustc_hir; |
| 7 | +extern crate rustc_span; |
| 8 | + |
| 9 | +use clippy_utils::{get_trait_def_id, ty::implements_trait}; |
| 10 | +use rustc_errors::Applicability; |
| 11 | +use rustc_hir::{ |
| 12 | + def::Res, |
| 13 | + intravisit::{walk_expr, Visitor}, |
| 14 | + Expr, ExprKind, Node, |
| 15 | +}; |
| 16 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 17 | +use rustc_span::sym; |
| 18 | +use utils::span_to_snippet_macro; |
| 19 | + |
| 20 | +dylint_linting::declare_late_lint! { |
| 21 | + /// ### What it does |
| 22 | + /// |
| 23 | + /// ### Why is this bad? |
| 24 | + /// |
| 25 | + /// ### Known problems |
| 26 | + /// Remove if none. |
| 27 | + /// |
| 28 | + /// ### Example |
| 29 | + /// ```rust |
| 30 | + /// // example code where a warning is issued |
| 31 | + /// ``` |
| 32 | + /// Use instead: |
| 33 | + /// ```rust |
| 34 | + /// // example code that does not raise a warning |
| 35 | + /// ``` |
| 36 | + pub PAR_ITER, |
| 37 | + Warn, |
| 38 | + "suggest using par iter" |
| 39 | +} |
| 40 | + |
| 41 | +struct ClosureVisitor<'a, 'tcx> { |
| 42 | + cx: &'a LateContext<'tcx>, |
| 43 | + is_valid: bool, |
| 44 | +} |
| 45 | + |
| 46 | +struct Validator<'a, 'tcx> { |
| 47 | + cx: &'a LateContext<'tcx>, |
| 48 | + is_valid: bool, |
| 49 | +} |
| 50 | +impl<'a, 'tcx> Visitor<'_> for ClosureVisitor<'a, 'tcx> { |
| 51 | + fn visit_expr(&mut self, ex: &Expr) { |
| 52 | + match ex.kind { |
| 53 | + ExprKind::Path(path) => { |
| 54 | + let res: Res = self.cx.typeck_results().qpath_res(&path, ex.hir_id); |
| 55 | + |
| 56 | + if let Res::Local(hir_id) = res { |
| 57 | + let parent = self.cx.tcx.hir().get_parent(hir_id); |
| 58 | + match parent { |
| 59 | + Node::Local(local) => { |
| 60 | + if let Some(expr) = local.init { |
| 61 | + let ty = |
| 62 | + self.cx.tcx.typeck(expr.hir_id.owner).node_type(expr.hir_id); |
| 63 | + let implements_send = self |
| 64 | + .cx |
| 65 | + .tcx |
| 66 | + .get_diagnostic_item(sym::Send) |
| 67 | + .map_or(false, |id| implements_trait(self.cx, ty, id, &[])); |
| 68 | + let implements_sync = self |
| 69 | + .cx |
| 70 | + .tcx |
| 71 | + .get_diagnostic_item(sym::Sync) |
| 72 | + .map_or(false, |id| implements_trait(self.cx, ty, id, &[])); |
| 73 | + let is_copy: bool = self |
| 74 | + .cx |
| 75 | + .tcx |
| 76 | + .get_diagnostic_item(sym::Copy) |
| 77 | + .map_or(false, |id| implements_trait(self.cx, ty, id, &[])); |
| 78 | + let valid = is_copy || (implements_send && implements_sync); |
| 79 | + self.is_valid = self.is_valid && valid; |
| 80 | + }; |
| 81 | + } |
| 82 | + _ => {} |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + _ => walk_expr(self, ex), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl<'a, 'tcx> Visitor<'_> for Validator<'a, 'tcx> { |
| 92 | + fn visit_expr(&mut self, ex: &Expr) { |
| 93 | + match ex.kind { |
| 94 | + ExprKind::Closure(closure) => { |
| 95 | + let hir = self.cx.tcx.hir(); |
| 96 | + let node = hir.get(closure.body.hir_id); |
| 97 | + if let Node::Expr(expr) = node { |
| 98 | + let mut closure_visitor = ClosureVisitor { |
| 99 | + cx: self.cx, |
| 100 | + is_valid: true, |
| 101 | + }; |
| 102 | + closure_visitor.visit_expr(expr); |
| 103 | + self.is_valid = self.is_valid && closure_visitor.is_valid; |
| 104 | + } |
| 105 | + } |
| 106 | + _ => walk_expr(self, ex), |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl<'tcx> LateLintPass<'tcx> for ParIter { |
| 112 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 113 | + if let ExprKind::MethodCall(path, _recv, _args, _span) = &expr.kind { |
| 114 | + let ident_name = &path.ident.name.to_string()[..]; |
| 115 | + let src_map = cx.sess().source_map(); |
| 116 | + let mut suggestion = span_to_snippet_macro(src_map, expr.span); |
| 117 | + match ident_name { |
| 118 | + "into_iter" => suggestion = suggestion.replace("into_iter", "into_par_iter"), |
| 119 | + |
| 120 | + "iter" => suggestion = suggestion.replace("iter", "par_iter"), |
| 121 | + "iter_mut" => suggestion = suggestion.replace("iter_mut", "par_iter_mut"), |
| 122 | + _ => return, |
| 123 | + } |
| 124 | + |
| 125 | + let ty = cx.typeck_results().expr_ty(expr); |
| 126 | + |
| 127 | + let mut implements_par_iter = false; |
| 128 | + |
| 129 | + let trait_defs = vec![ |
| 130 | + get_trait_def_id(cx, &["rayon", "iter", "IntoParallelIterator"]), |
| 131 | + get_trait_def_id(cx, &["rayon", "iter", "ParallelIterator"]), |
| 132 | + // @todo get_trait_def_id(cx, &["rayon", "iter", "IndexedParallelIterator"]), |
| 133 | + // @todo get_trait_def_id(cx, &["rayon", "iter", "IntoParallelRefIterator"]), |
| 134 | + // @todo get_trait_def_id(cx, &["rayon", "iter", "IntoParallelRefMutIterator"]), |
| 135 | + ]; |
| 136 | + |
| 137 | + for t in trait_defs { |
| 138 | + if let Some(trait_def_id) = t { |
| 139 | + implements_par_iter = |
| 140 | + implements_par_iter || implements_trait(cx, ty, trait_def_id, &[]); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if !implements_par_iter { |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + // check that all types inside the closures are Send and sync or Copy |
| 149 | + let mut validator = Validator { cx, is_valid: true }; |
| 150 | + |
| 151 | + let parent_node = cx.tcx.hir().get_parent(expr.hir_id); |
| 152 | + match parent_node { |
| 153 | + Node::Expr(expr) => { |
| 154 | + validator.visit_expr(expr); |
| 155 | + } |
| 156 | + // Handle other kinds of parent nodes as needed |
| 157 | + _ => {} |
| 158 | + } |
| 159 | + if !validator.is_valid { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + cx.struct_span_lint( |
| 164 | + PAR_ITER, |
| 165 | + expr.span, |
| 166 | + "found iterator that can be parallelized", |
| 167 | + |diag| { |
| 168 | + diag.multipart_suggestion( |
| 169 | + "try using a parallel iterator", |
| 170 | + vec![(expr.span, suggestion)], |
| 171 | + Applicability::MachineApplicable, |
| 172 | + ) |
| 173 | + }, |
| 174 | + ); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +#[test] |
| 180 | +fn ui() { |
| 181 | + dylint_testing::ui_test_examples(env!("CARGO_PKG_NAME")); |
| 182 | +} |
0 commit comments