diff options
| author | Obei Sideg <obei.sideg@gmail.com> | 2023-02-16 22:18:36 +0300 |
|---|---|---|
| committer | Obei Sideg <obei.sideg@gmail.com> | 2023-02-23 13:57:13 +0300 |
| commit | b93d54556fd25a704dbba77d9d09f6726f5bce37 (patch) | |
| tree | 5bbd036dd292e48202c8f258d0487a0bd0c81f5e | |
| parent | a87443a85999eadc867d261667363137adcb2420 (diff) | |
| download | rust-b93d54556fd25a704dbba77d9d09f6726f5bce37.tar.gz rust-b93d54556fd25a704dbba77d9d09f6726f5bce37.zip | |
Add ui test for `map_unit_fn` lint in closure case
| -rw-r--r-- | tests/ui/lint/lint_map_unit_fn.rs | 9 | ||||
| -rw-r--r-- | tests/ui/lint/lint_map_unit_fn.stderr | 43 |
2 files changed, 51 insertions, 1 deletions
diff --git a/tests/ui/lint/lint_map_unit_fn.rs b/tests/ui/lint/lint_map_unit_fn.rs index 1dae47cd4c6..dc1ecbf8424 100644 --- a/tests/ui/lint/lint_map_unit_fn.rs +++ b/tests/ui/lint/lint_map_unit_fn.rs @@ -8,4 +8,13 @@ fn main() { let mut x: Vec<Vec<u8>> = vec![vec![0, 2, 1], vec![5, 4, 3]]; x.iter_mut().map(foo); //~^ ERROR `Iterator::map` call that discard the iterator's values + x.iter_mut().map(|items| { + //~^ ERROR `Iterator::map` call that discard the iterator's values + items.sort(); + }); + let f = |items: &mut Vec<u8>| { + items.sort(); + }; + x.iter_mut().map(f); + //~^ ERROR `Iterator::map` call that discard the iterator's values } diff --git a/tests/ui/lint/lint_map_unit_fn.stderr b/tests/ui/lint/lint_map_unit_fn.stderr index ee300e170b3..fbf689c5421 100644 --- a/tests/ui/lint/lint_map_unit_fn.stderr +++ b/tests/ui/lint/lint_map_unit_fn.stderr @@ -21,5 +21,46 @@ help: you might have meant to use `Iterator::for_each` LL | x.iter_mut().for_each(foo); | ~~~~~~~~ -error: aborting due to previous error +error: `Iterator::map` call that discard the iterator's values + --> $DIR/lint_map_unit_fn.rs:11:18 + | +LL | x.iter_mut().map(|items| { + | ^ ------- + | | | + | ____________________|___this function returns `()`, which is likely not what you wanted + | | __________________| + | | | +LL | | | +LL | | | items.sort(); +LL | | | }); + | | | -^ after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items + | | |_____|| + | |_______| + | called `Iterator::map` with callable that returns `()` + | + = note: `Iterator::map`, like many of the methods on `Iterator`, gets executed lazily, meaning that its effects won't be visible until it is iterated +help: you might have meant to use `Iterator::for_each` + | +LL | x.iter_mut().for_each(|items| { + | ~~~~~~~~ + +error: `Iterator::map` call that discard the iterator's values + --> $DIR/lint_map_unit_fn.rs:18:18 + | +LL | let f = |items: &mut Vec<u8>| { + | --------------------- this function returns `()`, which is likely not what you wanted +... +LL | x.iter_mut().map(f); + | ^^^^-^ + | | | + | | called `Iterator::map` with callable that returns `()` + | after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items + | + = note: `Iterator::map`, like many of the methods on `Iterator`, gets executed lazily, meaning that its effects won't be visible until it is iterated +help: you might have meant to use `Iterator::for_each` + | +LL | x.iter_mut().for_each(f); + | ~~~~~~~~ + +error: aborting due to 3 previous errors |
