Skip to main content

Lint clippy::map_unwrap_or

warning clippy::map_unwrap_or §

called `map(<f>).unwrap_or_else(<g>)` on an `Option` value

src/render/search.rs:100:24

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • cargo-clippy-html (bin)

Primary: src/render/search.rs:100:24

            let name = sources
                .get(&span.path)
                .map(|s| s.display_path.clone())
                .unwrap_or_else(|| span.path.to_string_lossy().replace('\\', "/"));

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#map_unwrap_or

note: `-W clippy::map-unwrap-or` implied by `-W clippy::pedantic`

help: to override `-W clippy::pedantic` add `#[allow(clippy::map_unwrap_or)]`

help: try

Suggestion: try

MachineApplicable

--- "a/src/render/search.rs"
+++ "b/src/render/search.rs"
@@ -98,9 +98,7 @@
             .or_else(|| finding.spans.first())
         {
             let name = sources
-                .get(&span.path)
-                .map(|s| s.display_path.clone())
-                .unwrap_or_else(|| span.path.to_string_lossy().replace('\\', "/"));
+                .get(&span.path).map_or_else(|| span.path.to_string_lossy().replace('\\', "/"), |s| s.display_path.clone());
             context.push(format!("{name}:{}:{}", span.line_start, span.column_start));
         }
         let mut description = context.join(" · ");
Rendered diagnostic (text)
warning: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value
   --> src/render/search.rs:100:24
    |
100 |               let name = sources
    |  ________________________^
101 | |                 .get(&span.path)
102 | |                 .map(|s| s.display_path.clone())
103 | |                 .unwrap_or_else(|| span.path.to_string_lossy().replace('\\', "/"));
    | |__________________________________________________________________________________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#map_unwrap_or
    = note: `-W clippy::map-unwrap-or` implied by `-W clippy::pedantic`
    = help: to override `-W clippy::pedantic` add `#[allow(clippy::map_unwrap_or)]`
help: try
    |
100 ~             let name = sources
101 ~                 .get(&span.path).map_or_else(|| span.path.to_string_lossy().replace('\\', "/"), |s| s.display_path.clone());
    |