Skip to content

Commit

Permalink
implement collect handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lucarlig committed Mar 12, 2024
1 parent fd53ee4 commit 6d628e1
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 52 deletions.
15 changes: 6 additions & 9 deletions lints/par_iter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ mod variable_check;
use clippy_utils::{get_parent_expr, get_trait_def_id};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir::intravisit::Visitor;
use rustc_hir::intravisit::{walk_expr, Visitor};
use rustc_hir::{self as hir};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::{self, ty_kind::TyKind, Ty};
use rustc_middle::ty::{self, Ty};
use rustc_span::sym;
use variable_check::{
check_implements_par_iter, check_trait_impl, check_variables, generate_suggestion,
is_type_valid,
};

dylint_linting::declare_late_lint! {
/// ### What it does
/// parallelize iterators using rayon
Expand Down Expand Up @@ -89,11 +88,6 @@ impl<'tcx> LateLintPass<'tcx> for ParIter {
return;
}

// TODO: this needs to change and find a better solutions for returns
if let TyKind::Adt(_, _) = ty.kind() {
return;
}

let mut validator = Validator { cx, is_valid: true };
validator.visit_expr(top_expr);
if !validator.is_valid {
Expand Down Expand Up @@ -128,14 +122,17 @@ impl<'a, 'tcx> hir::intravisit::Visitor<'_> for Validator<'a, 'tcx> {
if !self.is_valid {
return;
}
let ex_ty = self.cx.typeck_results().expr_ty(ex);
self.is_valid &= is_type_valid(self.cx, ex_ty);

for arg in args {
if let hir::ExprKind::Closure(closure) = arg.kind {
let body = self.cx.tcx.hir().body(closure.body);

self.is_valid &= check_variables(self.cx, closure.def_id, body);
}
}
}
walk_expr(self, ex)
}
}

Expand Down
47 changes: 47 additions & 0 deletions lints/par_iter/ui/main.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,50 @@ impl ApplicationState {
self.foreground_high_qos_cases.extend(change_state_cases);
}
}

// should parallelize
fn collect_at_end() {
let people = vec![
Person {
name: "Alice".to_string(),
age: 25,
},
Person {
name: "Bob".to_string(),
age: 35,
},
Person {
name: "Carol".to_string(),
age: 32,
},
];

let names: Vec<String> = people.par_iter().map(|p| p.name.clone()).collect();

println!("{:?}", names);
}

struct Tsize {
send: usize,
}

impl Tsize {
fn to_no_send(&self) -> TsizeNoSend {
TsizeNoSend {
no_send: Rc::new(self.send),
}
}
}

#[derive(Debug)]
struct TsizeNoSend {
no_send: Rc<usize>,
}

// no
fn collect_at_end_no_par() {
let t_size_vec: Vec<Tsize> = vec![Tsize { send: 32 }, Tsize { send: 42 }];
let t_size_vec_no_send: Vec<TsizeNoSend> = t_size_vec.iter().map(|t| t.to_no_send()).collect();

println!("{:?}", t_size_vec_no_send);
}
47 changes: 47 additions & 0 deletions lints/par_iter/ui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,50 @@ impl ApplicationState {
self.foreground_high_qos_cases.extend(change_state_cases);
}
}

// should parallelize
fn collect_at_end() {
let people = vec![
Person {
name: "Alice".to_string(),
age: 25,
},
Person {
name: "Bob".to_string(),
age: 35,
},
Person {
name: "Carol".to_string(),
age: 32,
},
];

let names: Vec<String> = people.iter().map(|p| p.name.clone()).collect();

println!("{:?}", names);
}

struct Tsize {
send: usize,
}

impl Tsize {
fn to_no_send(&self) -> TsizeNoSend {
TsizeNoSend {
no_send: Rc::new(self.send),
}
}
}

#[derive(Debug)]
struct TsizeNoSend {
no_send: Rc<usize>,
}

// no
fn collect_at_end_no_par() {
let t_size_vec: Vec<Tsize> = vec![Tsize { send: 32 }, Tsize { send: 42 }];
let t_size_vec_no_send: Vec<TsizeNoSend> = t_size_vec.iter().map(|t| t.to_no_send()).collect();

println!("{:?}", t_size_vec_no_send);
}
8 changes: 7 additions & 1 deletion lints/par_iter/ui/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,11 @@ error: found iterator that can be parallelized
LL | numbers.iter().enumerate().for_each(|t| {
| ^^^^^^^^^^^^^^ help: try using a parallel iterator: `numbers.par_iter()`

error: aborting due to 6 previous errors
error: found iterator that can be parallelized
--> $DIR/main.rs:323:30
|
LL | let names: Vec<String> = people.iter().map(|p| p.name.clone()).collect();
| ^^^^^^^^^^^^^ help: try using a parallel iterator: `people.par_iter()`

error: aborting due to 7 previous errors

21 changes: 0 additions & 21 deletions lints/par_iter/ui/main2.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,3 @@ fn main() {}
// println!("{:?}", doubled_numbers);
// }
//
// // should parallelize
// fn collect_at_end() {
// let people = vec![
// Person {
// name: "Alice".to_string(),
// age: 25,
// },
// Person {
// name: "Bob".to_string(),
// age: 35,
// },
// Person {
// name: "Carol".to_string(),
// age: 32,
// },
// ];

// let names: Vec<String> = people.iter().map(|p| p.name.clone()).collect();

// println!("{:?}", names);
// }
21 changes: 0 additions & 21 deletions lints/par_iter/ui/main2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,3 @@ fn main() {}
// println!("{:?}", doubled_numbers);
// }
//
// // should parallelize
// fn collect_at_end() {
// let people = vec![
// Person {
// name: "Alice".to_string(),
// age: 25,
// },
// Person {
// name: "Bob".to_string(),
// age: 35,
// },
// Person {
// name: "Carol".to_string(),
// age: 32,
// },
// ];

// let names: Vec<String> = people.iter().map(|p| p.name.clone()).collect();

// println!("{:?}", names);
// }

0 comments on commit 6d628e1

Please sign in to comment.