Skip to content

Commit

Permalink
add check that iterators are not returned
Browse files Browse the repository at this point in the history
  • Loading branch information
lucarlig committed Mar 5, 2024
1 parent 454cc5e commit 1d23284
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lints/par_iter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use rustc_hir::intravisit::Visitor;
use rustc_hir::{self as hir};
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};
use rustc_span::sym;
use utils::{check_implements_par_iter, check_trait_impl, generate_suggestion, is_type_valid};
use variable_check::check_variables;

dylint_linting::declare_late_lint! {
Expand Down Expand Up @@ -111,6 +112,10 @@ impl<'tcx> LateLintPass<'tcx> for ParIter {
}

let ty: Ty<'_> = cx.typeck_results().expr_ty(top_expr);
// TODO: find a way to deal with iterators returns
if check_trait_impl(cx, ty, sym::Iterator) {
return;
}

// TODO: this needs to change and find a better solutions for returns
if let TyKind::Adt(_, _) = ty.kind() {
Expand Down
6 changes: 5 additions & 1 deletion lints/par_iter/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ pub(crate) fn check_implements_par_iter<'tcx>(
implemented_traits
}

fn check_trait_impl<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, trait_name: Symbol) -> bool {
pub(crate) fn check_trait_impl<'tcx>(
cx: &LateContext<'tcx>,
ty: Ty<'tcx>,
trait_name: Symbol,
) -> bool {
cx.tcx
.get_diagnostic_item(trait_name)
.map_or(false, |trait_id| implements_trait(cx, ty, trait_id, &[]))
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 @@ -27,6 +27,16 @@ struct Case {
priority: u32,
}

#[derive(Clone)]
struct QosCase {
uid: u64, // Identifier for the Quality of Service case
}

struct ApplicationState {
foreground_high_qos_cases: Vec<QosCase>,
background_high_qos_cases: Vec<QosCase>,
}

fn main() {}

// should parallelize
Expand Down Expand Up @@ -318,3 +328,15 @@ fn request_request_filter() {
println!("Downgrade case: {:?}", down_grade_case);
println!("Swap case index: {:?}", swap_case_index_opt);
}

// no
impl ApplicationState {
fn transition_to_background(&mut self, target_uid: u64) {
let change_state_cases = self
.background_high_qos_cases
.iter()
.cloned()
.filter(|case| case.uid == target_uid);
self.foreground_high_qos_cases.extend(change_state_cases);
}
}
22 changes: 22 additions & 0 deletions lints/par_iter/ui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ struct Case {
priority: u32,
}

#[derive(Clone)]
struct QosCase {
uid: u64, // Identifier for the Quality of Service case
}

struct ApplicationState {
foreground_high_qos_cases: Vec<QosCase>,
background_high_qos_cases: Vec<QosCase>,
}

fn main() {}

// should parallelize
Expand Down Expand Up @@ -318,3 +328,15 @@ fn request_request_filter() {
println!("Downgrade case: {:?}", down_grade_case);
println!("Swap case index: {:?}", swap_case_index_opt);
}

// no
impl ApplicationState {
fn transition_to_background(&mut self, target_uid: u64) {
let change_state_cases = self
.background_high_qos_cases
.iter()
.cloned()
.filter(|case| case.uid == target_uid);
self.foreground_high_qos_cases.extend(change_state_cases);
}
}
16 changes: 8 additions & 8 deletions lints/par_iter/ui/main.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: found iterator that can be parallelized
--> $DIR/main.rs:34:5
--> $DIR/main.rs:44:5
|
LL | (0..100).into_iter().for_each(|x| println!("{:?}", x));
| ^^^^^^^^^^^^^^^^^^^^ help: try using a parallel iterator: `(0..100).into_par_iter()`
Expand All @@ -8,7 +8,7 @@ LL | (0..100).into_iter().for_each(|x| println!("{:?}", x));
= help: to override `-D warnings` add `#[allow(par_iter)]`

error: found iterator that can be parallelized
--> $DIR/main.rs:58:5
--> $DIR/main.rs:68:5
|
LL | / (0..100)
LL | | .into_iter()
Expand All @@ -21,37 +21,37 @@ LL + .into_par_iter()
|

error: found iterator that can be parallelized
--> $DIR/main.rs:93:5
--> $DIR/main.rs:103:5
|
LL | list.into_iter().for_each(|x| println!("{:?}", x));
| ^^^^^^^^^^^^^^^^ help: try using a parallel iterator: `list.into_par_iter()`

error: found iterator that can be parallelized
--> $DIR/main.rs:109:5
--> $DIR/main.rs:119:5
|
LL | (0..10).into_iter().for_each(|x| {
| ^^^^^^^^^^^^^^^^^^^ help: try using a parallel iterator: `(0..10).into_par_iter()`

error: found iterator that can be parallelized
--> $DIR/main.rs:192:5
--> $DIR/main.rs:202:5
|
LL | data.iter()
| ^^^^^^^^^^^ help: try using a parallel iterator: `data.par_iter()`

error: found iterator that can be parallelized
--> $DIR/main.rs:219:5
--> $DIR/main.rs:229:5
|
LL | numbers.iter().enumerate().for_each(|t| {
| ^^^^^^^^^^^^^^ help: try using a parallel iterator: `numbers.par_iter()`

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

error: found iterator that can be parallelized
--> $DIR/main.rs:268:37
--> $DIR/main.rs:278:37
|
LL | let doubled_numbers: Vec<i32> = numbers
| _____________________________________^
Expand Down

0 comments on commit 1d23284

Please sign in to comment.