rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/7] drm/panic: avoid reimplementing Iterator::find
@ 2024-10-12  7:52 Thomas Böhler
  2024-10-12  7:52 ` [PATCH 2/7] drm/panic: remove unnecessary borrow in alignment_pattern Thomas Böhler
                   ` (7 more replies)
  0 siblings, 8 replies; 23+ messages in thread
From: Thomas Böhler @ 2024-10-12  7:52 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Jocelyn Falempe
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, rust-for-linux,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann, David Airlie,
	Simona Vetter, dri-devel, linux-kernel, Thomas Böhler

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.

Reported-by: Miguel Ojeda <ojeda@kernel.org>
Closes: https://github.com/Rust-for-Linux/linux/issues/1123
Signed-off-by: Thomas Böhler <witcher@wiredspace.de>
---
 drivers/gpu/drm/drm_panic_qr.rs | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/drm_panic_qr.rs b/drivers/gpu/drm/drm_panic_qr.rs
index 1ef56cb07dfb..76decf49e678 100644
--- a/drivers/gpu/drm/drm_panic_qr.rs
+++ b/drivers/gpu/drm/drm_panic_qr.rs
@@ -209,12 +209,9 @@
 impl Version {
     /// Returns the smallest QR version than can hold these segments.
     fn from_segments(segments: &[&Segment<'_>]) -> Option<Version> {
-        for v in (1..=40).map(|k| Version(k)) {
-            if v.max_data() * 8 >= segments.iter().map(|s| s.total_size_bits(v)).sum() {
-                return Some(v);
-            }
-        }
-        None
+        (1..=40)
+            .map(Version)
+            .find(|&v| v.max_data() * 8 >= segments.iter().map(|s| s.total_size_bits(v)).sum())
     }
 
     fn width(&self) -> u8 {
-- 
2.46.2


^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2024-10-19  7:46 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-12  7:52 [PATCH 1/7] drm/panic: avoid reimplementing Iterator::find Thomas Böhler
2024-10-12  7:52 ` [PATCH 2/7] drm/panic: remove unnecessary borrow in alignment_pattern Thomas Böhler
2024-10-14  8:45   ` Jocelyn Falempe
2024-10-12  7:52 ` [PATCH 3/7] drm/panic: prefer eliding lifetimes Thomas Böhler
2024-10-14  8:46   ` Jocelyn Falempe
2024-10-12  7:52 ` [PATCH 4/7] drm/panic: remove redundant field when assigning value Thomas Böhler
2024-10-14  8:46   ` Jocelyn Falempe
2024-10-12  7:52 ` [PATCH 5/7] drm/panic: correctly indent continuation of line in list item Thomas Böhler
2024-10-14  8:47   ` Jocelyn Falempe
2024-10-12  7:52 ` [PATCH 6/7] drm/panic: allow verbose boolean for clarity Thomas Böhler
2024-10-14  8:27   ` Alice Ryhl
2024-10-14  8:28   ` Alice Ryhl
2024-10-14  8:54   ` Jocelyn Falempe
2024-10-14 16:59     ` Miguel Ojeda
2024-10-14 19:23       ` Jocelyn Falempe
2024-10-12  7:52 ` [PATCH 7/7] drm/panic: allow verbose version check Thomas Böhler
2024-10-12 11:08   ` Miguel Ojeda
2024-10-14  8:55   ` Jocelyn Falempe
2024-10-12 11:04 ` [PATCH 1/7] drm/panic: avoid reimplementing Iterator::find Miguel Ojeda
2024-10-14  9:06   ` Jocelyn Falempe
2024-10-19  7:46     ` Thomas Böhler
2024-10-19  7:45   ` Thomas Böhler
2024-10-14  8:44 ` Jocelyn Falempe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).