Skip to content

Commit 1d23284

Browse files
committed
add check that iterators are not returned
1 parent 454cc5e commit 1d23284

File tree

5 files changed

+63
-10
lines changed

5 files changed

+63
-10
lines changed

lints/par_iter/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use rustc_hir::intravisit::Visitor;
2323
use rustc_hir::{self as hir};
2424
use rustc_lint::{LateContext, LateLintPass, LintContext};
2525
use rustc_middle::ty::{self, ty_kind::TyKind, Ty};
26-
use utils::{check_implements_par_iter, generate_suggestion, is_type_valid};
26+
use rustc_span::sym;
27+
use utils::{check_implements_par_iter, check_trait_impl, generate_suggestion, is_type_valid};
2728
use variable_check::check_variables;
2829

2930
dylint_linting::declare_late_lint! {
@@ -111,6 +112,10 @@ impl<'tcx> LateLintPass<'tcx> for ParIter {
111112
}
112113

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

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

lints/par_iter/src/utils.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ pub(crate) fn check_implements_par_iter<'tcx>(
4141
implemented_traits
4242
}
4343

44-
fn check_trait_impl<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, trait_name: Symbol) -> bool {
44+
pub(crate) fn check_trait_impl<'tcx>(
45+
cx: &LateContext<'tcx>,
46+
ty: Ty<'tcx>,
47+
trait_name: Symbol,
48+
) -> bool {
4549
cx.tcx
4650
.get_diagnostic_item(trait_name)
4751
.map_or(false, |trait_id| implements_trait(cx, ty, trait_id, &[]))

lints/par_iter/ui/main.fixed

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ struct Case {
2727
priority: u32,
2828
}
2929

30+
#[derive(Clone)]
31+
struct QosCase {
32+
uid: u64, // Identifier for the Quality of Service case
33+
}
34+
35+
struct ApplicationState {
36+
foreground_high_qos_cases: Vec<QosCase>,
37+
background_high_qos_cases: Vec<QosCase>,
38+
}
39+
3040
fn main() {}
3141

3242
// should parallelize
@@ -318,3 +328,15 @@ fn request_request_filter() {
318328
println!("Downgrade case: {:?}", down_grade_case);
319329
println!("Swap case index: {:?}", swap_case_index_opt);
320330
}
331+
332+
// no
333+
impl ApplicationState {
334+
fn transition_to_background(&mut self, target_uid: u64) {
335+
let change_state_cases = self
336+
.background_high_qos_cases
337+
.iter()
338+
.cloned()
339+
.filter(|case| case.uid == target_uid);
340+
self.foreground_high_qos_cases.extend(change_state_cases);
341+
}
342+
}

lints/par_iter/ui/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ struct Case {
2727
priority: u32,
2828
}
2929

30+
#[derive(Clone)]
31+
struct QosCase {
32+
uid: u64, // Identifier for the Quality of Service case
33+
}
34+
35+
struct ApplicationState {
36+
foreground_high_qos_cases: Vec<QosCase>,
37+
background_high_qos_cases: Vec<QosCase>,
38+
}
39+
3040
fn main() {}
3141

3242
// should parallelize
@@ -318,3 +328,15 @@ fn request_request_filter() {
318328
println!("Downgrade case: {:?}", down_grade_case);
319329
println!("Swap case index: {:?}", swap_case_index_opt);
320330
}
331+
332+
// no
333+
impl ApplicationState {
334+
fn transition_to_background(&mut self, target_uid: u64) {
335+
let change_state_cases = self
336+
.background_high_qos_cases
337+
.iter()
338+
.cloned()
339+
.filter(|case| case.uid == target_uid);
340+
self.foreground_high_qos_cases.extend(change_state_cases);
341+
}
342+
}

lints/par_iter/ui/main.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: found iterator that can be parallelized
2-
--> $DIR/main.rs:34:5
2+
--> $DIR/main.rs:44:5
33
|
44
LL | (0..100).into_iter().for_each(|x| println!("{:?}", x));
55
| ^^^^^^^^^^^^^^^^^^^^ help: try using a parallel iterator: `(0..100).into_par_iter()`
@@ -8,7 +8,7 @@ LL | (0..100).into_iter().for_each(|x| println!("{:?}", x));
88
= help: to override `-D warnings` add `#[allow(par_iter)]`
99

1010
error: found iterator that can be parallelized
11-
--> $DIR/main.rs:58:5
11+
--> $DIR/main.rs:68:5
1212
|
1313
LL | / (0..100)
1414
LL | | .into_iter()
@@ -21,37 +21,37 @@ LL + .into_par_iter()
2121
|
2222

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

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

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

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

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

5353
error: found iterator that can be parallelized
54-
--> $DIR/main.rs:268:37
54+
--> $DIR/main.rs:278:37
5555
|
5656
LL | let doubled_numbers: Vec<i32> = numbers
5757
| _____________________________________^

0 commit comments

Comments
 (0)