Skip to content

Commit

Permalink
allow collect returns
Browse files Browse the repository at this point in the history
  • Loading branch information
lucarlig committed Mar 4, 2024
1 parent 95fc04f commit 7712470
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lints/par_iter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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::{self as hir};
use rustc_hir::{self as hir, ExprKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::{self, ty_kind::TyKind, Ty};
use utils::{check_implements_par_iter, generate_suggestion, is_type_valid};
Expand Down Expand Up @@ -113,7 +113,13 @@ impl<'tcx> LateLintPass<'tcx> for ParIter {

// TODO: this needs to change and find a better solutions for returns
if let TyKind::Adt(_, _) = ty.kind() {
return;
if let ExprKind::MethodCall(path_segment, _, _, _) = top_expr.kind {
if path_segment.ident.as_str() != "collect" {
return;
}
} else {
return;
}
}

let mut validator = Validator { cx, is_valid: true };
Expand Down
22 changes: 22 additions & 0 deletions lints/par_iter/ui/main.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,25 @@ fn simple_fold() {
});
println!("Sum: {}", sum);
}

// 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);
}
22 changes: 22 additions & 0 deletions lints/par_iter/ui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,25 @@ fn simple_fold() {
});
println!("Sum: {}", sum);
}

// 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);
}
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:253: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

0 comments on commit 7712470

Please sign in to comment.