Skip to content

Commit c408dd8

Browse files
Witcher01ojeda
authored andcommitted
drm/panic: avoid reimplementing Iterator::find
Rust's standard library's `std::iter::Iterator` trait provides a function `find` that finds the first element that satisfies a predicate. The function `Version::from_segments` is doing the same thing but is implementing the same logic itself. Clippy complains about this in the `manual_find` lint: error: manual implementation of `Iterator::find` --> drivers/gpu/drm/drm_panic_qr.rs:212:9 | 212 | / for v in (1..=40).map(|k| Version(k)) { 213 | | if v.max_data() * 8 >= segments.iter().map(|s| s.total_size_bits(v)).sum() { 214 | | return Some(v); 215 | | } 216 | | } 217 | | None | |____________^ help: replace with an iterator: `(1..=40).map(|k| Version(k)).find(|&v| v.max_data() * 8 >= segments.iter().map(|s| s.total_size_bits(v)).sum())` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_find = note: `-D clippy::manual-find` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::manual_find)]` Use `Iterator::find` instead to make the intention clearer. At the same time, clean up the redundant closure that Clippy warns about too: error: redundant closure --> drivers/gpu/drm/drm_panic_qr.rs:212:31 | 212 | for v in (1..=40).map(|k| Version(k)) { | ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Version` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure = note: `-D clippy::redundant-closure` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::redundant_closure)]` Fixes: cb5164a ("drm/panic: Add a QR code panic screen") Reported-by: Miguel Ojeda <ojeda@kernel.org> Link: #1123 Signed-off-by: Thomas Böhler <witcher@wiredspace.de> Reviewed-by: Jocelyn Falempe <jfalempe@redhat.com> Link: https://lore.kernel.org/r/20241019084048.22336-2-witcher@wiredspace.de [ Reworded to mention the redundant closure cleanup too. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 6ce162a commit c408dd8

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

drivers/gpu/drm/drm_panic_qr.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,9 @@ const FORMAT_INFOS_QR_L: [u16; 8] = [
209209
impl Version {
210210
/// Returns the smallest QR version than can hold these segments.
211211
fn from_segments(segments: &[&Segment<'_>]) -> Option<Version> {
212-
for v in (1..=40).map(|k| Version(k)) {
213-
if v.max_data() * 8 >= segments.iter().map(|s| s.total_size_bits(v)).sum() {
214-
return Some(v);
215-
}
216-
}
217-
None
212+
(1..=40)
213+
.map(Version)
214+
.find(|&v| v.max_data() * 8 >= segments.iter().map(|s| s.total_size_bits(v)).sum())
218215
}
219216

220217
fn width(&self) -> u8 {

0 commit comments

Comments
 (0)