Skip to content

Commit 403433f

Browse files
committed
Auto merge of #12526 - kpreid:patch-2, r=blyxyas
Mention `size_hint()` effect in `flat_map_option` lint documentation. The previous documentation for `flat_map_option` mentioned only readability benefits, but there is also at least one performance benefit: the `size_hint()` upper bound is preserved, whereas `flat_map().size_hint()` is always `(0, None)`. Program demonstrating this difference: ```rust fn main() { let evens = |i| if i % 2 == 0 { Some(i) } else { None }; dbg!( [1, 2, 3].iter().flat_map(evens).size_hint(), [1, 2, 3].iter().filter_map(evens).size_hint(), ); } ``` changelog: [`flat_map_option`]: Mention the benefit to `size_hint()`.
2 parents fc053c3 + 15da6e7 commit 403433f

File tree

1 file changed

+6
-2
lines changed
  • clippy_lints/src/methods

1 file changed

+6
-2
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,12 @@ declare_clippy_lint! {
231231
/// used instead.
232232
///
233233
/// ### Why is this bad?
234-
/// When applicable, `filter_map()` is more clear since it shows that
235-
/// `Option` is used to produce 0 or 1 items.
234+
/// `filter_map()` is known to always produce 0 or 1 output items per input item,
235+
/// rather than however many the inner iterator type produces.
236+
/// Therefore, it maintains the upper bound in `Iterator::size_hint()`,
237+
/// and communicates to the reader that the input items are not being expanded into
238+
/// multiple output items without their having to notice that the mapping function
239+
/// returns an `Option`.
236240
///
237241
/// ### Example
238242
/// ```no_run

0 commit comments

Comments
 (0)