Skip to content

Commit 6d628e1

Browse files
committed
implement collect handling
1 parent fd53ee4 commit 6d628e1

File tree

6 files changed

+107
-52
lines changed

6 files changed

+107
-52
lines changed

lints/par_iter/src/lib.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ mod variable_check;
1717
use clippy_utils::{get_parent_expr, get_trait_def_id};
1818
use rustc_data_structures::fx::FxHashSet;
1919
use rustc_errors::Applicability;
20-
use rustc_hir::intravisit::Visitor;
20+
use rustc_hir::intravisit::{walk_expr, Visitor};
2121
use rustc_hir::{self as hir};
2222
use rustc_lint::{LateContext, LateLintPass, LintContext};
23-
use rustc_middle::ty::{self, ty_kind::TyKind, Ty};
23+
use rustc_middle::ty::{self, Ty};
2424
use rustc_span::sym;
2525
use variable_check::{
2626
check_implements_par_iter, check_trait_impl, check_variables, generate_suggestion,
2727
is_type_valid,
2828
};
29-
3029
dylint_linting::declare_late_lint! {
3130
/// ### What it does
3231
/// parallelize iterators using rayon
@@ -89,11 +88,6 @@ impl<'tcx> LateLintPass<'tcx> for ParIter {
8988
return;
9089
}
9190

92-
// TODO: this needs to change and find a better solutions for returns
93-
if let TyKind::Adt(_, _) = ty.kind() {
94-
return;
95-
}
96-
9791
let mut validator = Validator { cx, is_valid: true };
9892
validator.visit_expr(top_expr);
9993
if !validator.is_valid {
@@ -128,14 +122,17 @@ impl<'a, 'tcx> hir::intravisit::Visitor<'_> for Validator<'a, 'tcx> {
128122
if !self.is_valid {
129123
return;
130124
}
125+
let ex_ty = self.cx.typeck_results().expr_ty(ex);
126+
self.is_valid &= is_type_valid(self.cx, ex_ty);
127+
131128
for arg in args {
132129
if let hir::ExprKind::Closure(closure) = arg.kind {
133130
let body = self.cx.tcx.hir().body(closure.body);
134-
135131
self.is_valid &= check_variables(self.cx, closure.def_id, body);
136132
}
137133
}
138134
}
135+
walk_expr(self, ex)
139136
}
140137
}
141138

lints/par_iter/ui/main.fixed

+47
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,50 @@ impl ApplicationState {
302302
self.foreground_high_qos_cases.extend(change_state_cases);
303303
}
304304
}
305+
306+
// should parallelize
307+
fn collect_at_end() {
308+
let people = vec![
309+
Person {
310+
name: "Alice".to_string(),
311+
age: 25,
312+
},
313+
Person {
314+
name: "Bob".to_string(),
315+
age: 35,
316+
},
317+
Person {
318+
name: "Carol".to_string(),
319+
age: 32,
320+
},
321+
];
322+
323+
let names: Vec<String> = people.par_iter().map(|p| p.name.clone()).collect();
324+
325+
println!("{:?}", names);
326+
}
327+
328+
struct Tsize {
329+
send: usize,
330+
}
331+
332+
impl Tsize {
333+
fn to_no_send(&self) -> TsizeNoSend {
334+
TsizeNoSend {
335+
no_send: Rc::new(self.send),
336+
}
337+
}
338+
}
339+
340+
#[derive(Debug)]
341+
struct TsizeNoSend {
342+
no_send: Rc<usize>,
343+
}
344+
345+
// no
346+
fn collect_at_end_no_par() {
347+
let t_size_vec: Vec<Tsize> = vec![Tsize { send: 32 }, Tsize { send: 42 }];
348+
let t_size_vec_no_send: Vec<TsizeNoSend> = t_size_vec.iter().map(|t| t.to_no_send()).collect();
349+
350+
println!("{:?}", t_size_vec_no_send);
351+
}

lints/par_iter/ui/main.rs

+47
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,50 @@ impl ApplicationState {
302302
self.foreground_high_qos_cases.extend(change_state_cases);
303303
}
304304
}
305+
306+
// should parallelize
307+
fn collect_at_end() {
308+
let people = vec![
309+
Person {
310+
name: "Alice".to_string(),
311+
age: 25,
312+
},
313+
Person {
314+
name: "Bob".to_string(),
315+
age: 35,
316+
},
317+
Person {
318+
name: "Carol".to_string(),
319+
age: 32,
320+
},
321+
];
322+
323+
let names: Vec<String> = people.iter().map(|p| p.name.clone()).collect();
324+
325+
println!("{:?}", names);
326+
}
327+
328+
struct Tsize {
329+
send: usize,
330+
}
331+
332+
impl Tsize {
333+
fn to_no_send(&self) -> TsizeNoSend {
334+
TsizeNoSend {
335+
no_send: Rc::new(self.send),
336+
}
337+
}
338+
}
339+
340+
#[derive(Debug)]
341+
struct TsizeNoSend {
342+
no_send: Rc<usize>,
343+
}
344+
345+
// no
346+
fn collect_at_end_no_par() {
347+
let t_size_vec: Vec<Tsize> = vec![Tsize { send: 32 }, Tsize { send: 42 }];
348+
let t_size_vec_no_send: Vec<TsizeNoSend> = t_size_vec.iter().map(|t| t.to_no_send()).collect();
349+
350+
println!("{:?}", t_size_vec_no_send);
351+
}

lints/par_iter/ui/main.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,11 @@ error: found iterator that can be parallelized
4444
LL | numbers.iter().enumerate().for_each(|t| {
4545
| ^^^^^^^^^^^^^^ help: try using a parallel iterator: `numbers.par_iter()`
4646

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

lints/par_iter/ui/main2.fixed

-21
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,3 @@ fn main() {}
100100
// println!("{:?}", doubled_numbers);
101101
// }
102102
//
103-
// // should parallelize
104-
// fn collect_at_end() {
105-
// let people = vec![
106-
// Person {
107-
// name: "Alice".to_string(),
108-
// age: 25,
109-
// },
110-
// Person {
111-
// name: "Bob".to_string(),
112-
// age: 35,
113-
// },
114-
// Person {
115-
// name: "Carol".to_string(),
116-
// age: 32,
117-
// },
118-
// ];
119-
120-
// let names: Vec<String> = people.iter().map(|p| p.name.clone()).collect();
121-
122-
// println!("{:?}", names);
123-
// }

lints/par_iter/ui/main2.rs

-21
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,3 @@ fn main() {}
100100
// println!("{:?}", doubled_numbers);
101101
// }
102102
//
103-
// // should parallelize
104-
// fn collect_at_end() {
105-
// let people = vec![
106-
// Person {
107-
// name: "Alice".to_string(),
108-
// age: 25,
109-
// },
110-
// Person {
111-
// name: "Bob".to_string(),
112-
// age: 35,
113-
// },
114-
// Person {
115-
// name: "Carol".to_string(),
116-
// age: 32,
117-
// },
118-
// ];
119-
120-
// let names: Vec<String> = people.iter().map(|p| p.name.clone()).collect();
121-
122-
// println!("{:?}", names);
123-
// }

0 commit comments

Comments
 (0)