Skip to contentSkip to conversation

rust: cpufreq: clean new `clippy::map_or_identity` lint for Rust 1.98.0

PATCH Applied Miguel Ojeda 6 participants 8 messages May 30 – Jun 1

Subsystems (3)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/1 rust: cpufreq: clean new `clippy::map_or_identity` lint for Rust 1.98.0

Starting with Rust 1.98.0 (expected 2026-08-20), Clippy is likely introducing a new lint clippy::map_or_identity [1][2], which currently triggers in a single case:

    warning: expression can be simplified using `Result::unwrap_or()`
        --> rust/kernel/cpufreq.rs:1326:60
         |
    1326 |         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
         |                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_or_identity
         = note: `-W clippy::map-or-identity` implied by `-W clippy::all`
         = help: to override `-W clippy::all` add `#[allow(clippy::map_or_identity)]`
    help: consider using `unwrap_or`
         |
    1326 -         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
    1326 +         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
         |

The suggestion is valid, thus clean it up.

---
 rust/kernel/cpufreq.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/cpufreq.rs +1 -1
index d8d26870bea2..a20bd5006f38 100644
@@ -1323,7 +1323,7 @@ impl<T: Driver> Registration<T> {
         // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number.
         let cpu_id = unsafe { CpuId::from_u32_unchecked(cpu) };
 
-        PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
+        PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
     }
 
     /// Driver's `update_limit` callback.

base-commit: 420dd187e1572bb7e232781bc4377a80c8eb64fb

Signature · 1 line
2.54.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Miguel Ojeda

Starting with Rust 1.98.0 (expected 2026-08-20), Clippy is likely introducing a new lint clippy::map_or_identity [1][2], which currently triggers in a single case:

    warning: expression can be simplified using `Result::unwrap_or()`
        --> rust/kernel/cpufreq.rs:1326:60
         |
    1326 |         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
         |                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_or_identity
         = note: `-W clippy::map-or-identity` implied by `-W clippy::all`
         = help: to override `-W clippy::all` add `#[allow(clippy::map_or_identity)]`
    help: consider using `unwrap_or`
         |
    1326 -         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
    1326 +         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
         |
Quoted 6 lines

The suggestion is valid, thus clean it up.

reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Miguel Ojeda
Quoted 19 lines · Miguel Ojeda

Starting with Rust 1.98.0 (expected 2026-08-20), Clippy is likely introducing a new lint clippy::map_or_identity [1][2], which currently triggers in a single case:

    warning: expression can be simplified using `Result::unwrap_or()`
        --> rust/kernel/cpufreq.rs:1326:60
         |
    1326 |         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
         |                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_or_identity
         = note: `-W clippy::map-or-identity` implied by `-W clippy::all`
         = help: to override `-W clippy::all` add `#[allow(clippy::map_or_identity)]`
    help: consider using `unwrap_or`
         |
    1326 -         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
    1326 +         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
         |
Quoted 6 lines

The suggestion is valid, thus clean it up.

Quoted 3 lines
---
 rust/kernel/cpufreq.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw
In reply to Miguel Ojeda
Quoted 20 lines · Miguel Ojeda

Starting with Rust 1.98.0 (expected 2026-08-20), Clippy is likely introducing a new lint clippy::map_or_identity [1][2], which currently triggers in a single case:

    warning: expression can be simplified using `Result::unwrap_or()`
        --> rust/kernel/cpufreq.rs:1326:60
         |
    1326 |         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
         |                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_or_identity
         = note: `-W clippy::map-or-identity` implied by `-W clippy::all`
         = help: to override `-W clippy::all` add `#[allow(clippy::map_or_identity)]`
    help: consider using `unwrap_or`
         |
    1326 -         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
    1326 +         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
         |
Quoted 6 lines

The suggestion is valid, thus clean it up.

Viresh, Rafael: I can put this into rust-next if you prefer (I considered rust-fixes, but it is not important enough at this stage).

Cheers,
Miguel

maintainer Acked-by
#4 lore raw
Quoted 23 lines · Miguel Ojeda
Quoted 22 lines · Miguel Ojeda

Starting with Rust 1.98.0 (expected 2026-08-20), Clippy is likely introducing a new lint clippy::map_or_identity [1][2], which currently triggers in a single case:

    warning: expression can be simplified using `Result::unwrap_or()`
        --> rust/kernel/cpufreq.rs:1326:60
         |
    1326 |         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
         |                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_or_identity
         = note: `-W clippy::map-or-identity` implied by `-W clippy::all`
         = help: to override `-W clippy::all` add `#[allow(clippy::map_or_identity)]`
    help: consider using `unwrap_or`
         |
    1326 -         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
    1326 +         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
         |

The suggestion is valid, thus clean it up.

Quoted 8 lines
Quoted 4 lines

Viresh, Rafael: I can put this into rust-next if you prefer (I considered rust-fixes, but it is not important enough at this stage).

Signature · 1 line
viresh
Reviewed-by on v1 1/1
#5 lore raw
In reply to Miguel Ojeda
Quoted 19 lines · Miguel Ojeda

Starting with Rust 1.98.0 (expected 2026-08-20), Clippy is likely introducing a new lint clippy::map_or_identity [1][2], which currently triggers in a single case:

     warning: expression can be simplified using `Result::unwrap_or()`
         --> rust/kernel/cpufreq.rs:1326:60
          |
     1326 |         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
          |                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          |
          = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_or_identity
          = note: `-W clippy::map-or-identity` implied by `-W clippy::all`
          = help: to override `-W clippy::all` add `#[allow(clippy::map_or_identity)]`
     help: consider using `unwrap_or`
          |
     1326 -         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
     1326 +         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
          |
Quoted 6 lines

The suggestion is valid, thus clean it up.

Quoted 19 lines
---
  rust/kernel/cpufreq.rs | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/cpufreq.rs +1 -1
index d8d26870bea2..a20bd5006f38 100644
@@ -1323,7 +1323,7 @@ impl<T: Driver> Registration<T> {
          // SAFETY: The C API guarantees that `cpu` refers to a valid CPU number.
          let cpu_id = unsafe { CpuId::from_u32_unchecked(cpu) };
 
-        PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
+        PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
      }
 
      /// Driver's `update_limit` callback.

base-commit: 420dd187e1572bb7e232781bc4377a80c8eb64fb

Signature · 2 lines
Thx and BRs,
Zhongqiu Han
Applied
#6 lore raw
In reply to Miguel Ojeda
Quoted 20 lines · Miguel Ojeda

Starting with Rust 1.98.0 (expected 2026-08-20), Clippy is likely introducing a new lint clippy::map_or_identity [1][2], which currently triggers in a single case:

    warning: expression can be simplified using `Result::unwrap_or()`
        --> rust/kernel/cpufreq.rs:1326:60
         |
    1326 |         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
         |                                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         |
         = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_or_identity
         = note: `-W clippy::map-or-identity` implied by `-W clippy::all`
         = help: to override `-W clippy::all` add `#[allow(clippy::map_or_identity)]`
    help: consider using `unwrap_or`
         |
    1326 -         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).map_or(0, |f| f))
    1326 +         PolicyCpu::from_cpu(cpu_id).map_or(0, |mut policy| T::get(&mut policy).unwrap_or(0))
         |
Quoted 6 lines

The suggestion is valid, thus clean it up.

Applied to rust-next -- thanks everyone!

Cheers,
Miguel

#7 lore raw
In reply to Miguel Ojeda
Quoted 2 lines · Miguel Ojeda
Cc [email protected] # Needed in 6.12.y and later (Rust is pinned in older LTSs).

Adjusted to "6.18.y and later", since cpufreq was not there in 6.12.y anyway.

(I didn't use Fixes: because it isn't really a fix, although as usual it depends on how one thinks about those...)

Cheers,
Miguel