Skip to content

Commit

Permalink
Merge pull request #366 from cuviper/must_use
Browse files Browse the repository at this point in the history
Add `#[must_use]` attributes to iterator adaptors
  • Loading branch information
nikomatsakis committed Jun 14, 2017
2 parents a4f105d + e77fe82 commit f115dd8
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/iter/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rayon_core::join;
///
/// [`chain()`]: trait.ParallelIterator.html#method.chain
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Chain<A, B>
where A: ParallelIterator,
B: ParallelIterator<Item = A::Item>
Expand Down Expand Up @@ -209,7 +210,7 @@ impl<A, B> Producer for ChainProducer<A, B>
/// ////////////////////////////////////////////////////////////////////////
/// Wrapper for Chain to implement ExactSizeIterator

pub struct ChainSeq<A, B> {
struct ChainSeq<A, B> {
chain: iter::Chain<A, B>,
}

Expand Down
1 change: 1 addition & 0 deletions src/iter/cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::iter;
///
/// [`cloned()`]: trait.ParallelIterator.html#method.cloned
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Cloned<I: ParallelIterator> {
base: I,
}
Expand Down
1 change: 1 addition & 0 deletions src/iter/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::usize;
///
/// [`enumerate()`]: trait.ParallelIterator.html#method.enumerate
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Enumerate<I: IndexedParallelIterator> {
base: I,
}
Expand Down
1 change: 1 addition & 0 deletions src/iter/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::*;
///
/// [`filter()`]: trait.ParallelIterator.html#method.filter
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Filter<I: ParallelIterator, P> {
base: I,
filter_op: P,
Expand Down
1 change: 1 addition & 0 deletions src/iter/filter_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::*;
///
/// [`filter_map()`]: trait.ParallelIterator.html#method.filter_map
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct FilterMap<I: ParallelIterator, P> {
base: I,
filter_op: P,
Expand Down
1 change: 1 addition & 0 deletions src/iter/flat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::*;
///
/// [`flap_map()`]: trait.ParallelIterator.html#method.flat_map
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct FlatMap<I: ParallelIterator, F> {
base: I,
map_op: F,
Expand Down
4 changes: 3 additions & 1 deletion src/iter/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn fold<U, I, ID, F>(base: I, identity: ID, fold_op: F) -> Fold<I, ID, F>
///
/// [`fold()`]: trait.ParallelIterator.html#method.fold
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Fold<I, ID, F> {
base: I,
identity: ID,
Expand Down Expand Up @@ -94,7 +95,7 @@ impl<'r, U, T, C, ID, F> UnindexedConsumer<T> for FoldConsumer<'r, C, ID, F>
}
}

pub struct FoldFolder<'r, C, ID, F: 'r> {
struct FoldFolder<'r, C, ID, F: 'r> {
base: C,
fold_op: &'r F,
item: ID,
Expand Down Expand Up @@ -143,6 +144,7 @@ pub fn fold_with<U, I, F>(base: I, item: U, fold_op: F) -> FoldWith<I, U, F>
///
/// [`fold_with()`]: trait.ParallelIterator.html#method.fold_with
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct FoldWith<I, U, F> {
base: I,
item: U,
Expand Down
1 change: 1 addition & 0 deletions src/iter/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::iter;
///
/// [`inspect()`]: trait.ParallelIterator.html#method.inspect
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Inspect<I: ParallelIterator, F> {
base: I,
inspect_op: F,
Expand Down
2 changes: 2 additions & 0 deletions src/iter/len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::cmp;
///
/// [`min_len()`]: trait.IndexedParallelIterator.html#method.min_len
/// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct MinLen<I: IndexedParallelIterator> {
base: I,
min: usize,
Expand Down Expand Up @@ -126,6 +127,7 @@ impl<P> Producer for MinLenProducer<P>
///
/// [`max_len()`]: trait.IndexedParallelIterator.html#method.max_len
/// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct MaxLen<I: IndexedParallelIterator> {
base: I,
max: usize,
Expand Down
1 change: 1 addition & 0 deletions src/iter/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::iter;
///
/// [`map()`]: trait.ParallelIterator.html#method.map
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Map<I: ParallelIterator, F> {
base: I,
map_op: F,
Expand Down
1 change: 1 addition & 0 deletions src/iter/map_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::*;
///
/// [`map_with()`]: trait.ParallelIterator.html#method.map_with
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct MapWith<I: ParallelIterator, T, F> {
base: I,
item: T,
Expand Down
1 change: 1 addition & 0 deletions src/iter/rev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::internal::*;
use super::*;
use std::iter;

#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Rev<I: IndexedParallelIterator> {
base: I,
}
Expand Down
1 change: 1 addition & 0 deletions src/iter/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::cmp::min;
///
/// [`skip()`]: trait.ParallelIterator.html#method.skip
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Skip<I> {
base: I,
n: usize,
Expand Down
1 change: 1 addition & 0 deletions src/iter/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::cmp::min;
///
/// [`take()`]: trait.ParallelIterator.html#method.take
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Take<I> {
base: I,
n: usize,
Expand Down
1 change: 1 addition & 0 deletions src/iter/while_some.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::*;
///
/// [`while_some()`]: trait.ParallelIterator.html#method.while_some
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct WhileSome<I: ParallelIterator> {
base: I,
}
Expand Down
1 change: 1 addition & 0 deletions src/iter/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::*;
use std::cmp;
use std::iter;

#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Zip<A: IndexedParallelIterator, B: IndexedParallelIterator> {
a: A,
b: B,
Expand Down
30 changes: 30 additions & 0 deletions tests/compile-fail/must_use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![deny(unused_must_use)]

extern crate rayon;

// Check that we are flagged for ignoring `must_use` parallel adaptors.

use rayon::prelude::*;

fn main() {
let v: Vec<_> = (0..100).map(Some).collect();

v.par_iter().chain(&v); //~ ERROR unused result
v.par_iter().cloned(); //~ ERROR unused result
v.par_iter().enumerate(); //~ ERROR unused result
v.par_iter().filter(|_| true); //~ ERROR unused result
v.par_iter().filter_map(|x| *x); //~ ERROR unused result
v.par_iter().flat_map(|x| *x); //~ ERROR unused result
v.par_iter().fold(|| 0, |x, _| x); //~ ERROR unused result
v.par_iter().fold_with(0, |x, _| x); //~ ERROR unused result
v.par_iter().inspect(|_| {}); //~ ERROR unused result
v.par_iter().map(|x| x); //~ ERROR unused result
v.par_iter().map_with(0, |_, x| x); //~ ERROR unused result
v.par_iter().rev(); //~ ERROR unused result
v.par_iter().skip(1); //~ ERROR unused result
v.par_iter().take(1); //~ ERROR unused result
v.par_iter().cloned().while_some(); //~ ERROR unused result
v.par_iter().with_max_len(1); //~ ERROR unused result
v.par_iter().with_min_len(1); //~ ERROR unused result
v.par_iter().zip(&v); //~ ERROR unused result
}

0 comments on commit f115dd8

Please sign in to comment.