From eaf2fbb21b41424ac5b269ae4ef6ca80fa9672e2 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Sat, 2 Mar 2024 18:24:22 -0500 Subject: [PATCH] Enable `clippy::map_flatten` (#8733) This PR enables the [`clippy::map_flatten`](https://rust-lang.github.io/rust-clippy/master/index.html#/map_flatten) rule and fixes the outstanding violations. Release Notes: - N/A --- crates/auto_update/src/auto_update.rs | 3 +-- crates/collab/src/rpc/connection_pool.rs | 6 ++---- crates/collab_ui/src/chat_panel.rs | 6 ++---- crates/copilot_ui/src/copilot_button.rs | 2 +- crates/editor/src/element.rs | 3 +-- crates/editor/src/movement.rs | 3 +-- crates/project/src/project.rs | 2 +- crates/terminal_view/src/terminal_view.rs | 9 +++------ crates/zed/src/main.rs | 4 +--- tooling/xtask/src/main.rs | 1 - 10 files changed, 13 insertions(+), 26 deletions(-) diff --git a/crates/auto_update/src/auto_update.rs b/crates/auto_update/src/auto_update.rs index 23ca550516..f364304d59 100644 --- a/crates/auto_update/src/auto_update.rs +++ b/crates/auto_update/src/auto_update.rs @@ -343,8 +343,7 @@ impl AutoUpdater { )); cx.update(|cx| { if let Some(param) = ReleaseChannel::try_global(cx) - .map(|release_channel| release_channel.release_query_param()) - .flatten() + .and_then(|release_channel| release_channel.release_query_param()) { url_string += "&"; url_string += param; diff --git a/crates/collab/src/rpc/connection_pool.rs b/crates/collab/src/rpc/connection_pool.rs index e438fa2caf..2d28290373 100644 --- a/crates/collab/src/rpc/connection_pool.rs +++ b/crates/collab/src/rpc/connection_pool.rs @@ -94,21 +94,19 @@ impl ConnectionPool { self.connected_users .get(&user_id) .into_iter() - .map(|state| { + .flat_map(|state| { state .connection_ids .iter() .flat_map(|cid| self.connections.get(cid)) }) - .flatten() } pub fn user_connection_ids(&self, user_id: UserId) -> impl Iterator + '_ { self.connected_users .get(&user_id) .into_iter() - .map(|state| &state.connection_ids) - .flatten() + .flat_map(|state| &state.connection_ids) .copied() } diff --git a/crates/collab_ui/src/chat_panel.rs b/crates/collab_ui/src/chat_panel.rs index 1b6dc4887f..b2285c5142 100644 --- a/crates/collab_ui/src/chat_panel.rs +++ b/crates/collab_ui/src/chat_panel.rs @@ -444,8 +444,7 @@ impl ChatPanel { let reply_to_message = message .reply_to_message_id - .map(|id| active_chat.read(cx).find_loaded_message(id)) - .flatten() + .and_then(|id| active_chat.read(cx).find_loaded_message(id)) .cloned(); let replied_to_you = @@ -839,7 +838,7 @@ impl Render for ChatPanel { .when_some(reply_to_message_id, |el, reply_to_message_id| { let reply_message = self .active_chat() - .map(|active_chat| { + .and_then(|active_chat| { active_chat.read(cx).messages().iter().find_map(|m| { if m.id == ChannelMessageId::Saved(reply_to_message_id) { Some(m) @@ -848,7 +847,6 @@ impl Render for ChatPanel { } }) }) - .flatten() .cloned(); el.when_some(reply_message, |el, reply_message| { diff --git a/crates/copilot_ui/src/copilot_button.rs b/crates/copilot_ui/src/copilot_button.rs index 8ea106a3ff..ca9d2ae122 100644 --- a/crates/copilot_ui/src/copilot_button.rs +++ b/crates/copilot_ui/src/copilot_button.rs @@ -238,7 +238,7 @@ impl CopilotButton { impl StatusItemView for CopilotButton { fn set_active_pane_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext) { - if let Some(editor) = item.map(|item| item.act_as::(cx)).flatten() { + if let Some(editor) = item.and_then(|item| item.act_as::(cx)) { self.editor_subscription = Some(( cx.observe(&editor, Self::update_enabled), editor.entity_id().as_u64() as usize, diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 9dae0d0ee3..b1e372b91e 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -4083,8 +4083,7 @@ mod tests { .position_map .line_layouts .iter() - .map(|line_with_invisibles| &line_with_invisibles.invisibles) - .flatten() + .flat_map(|line_with_invisibles| &line_with_invisibles.invisibles) .cloned() .collect() } diff --git a/crates/editor/src/movement.rs b/crates/editor/src/movement.rs index d52255cd51..39c22dd5c4 100644 --- a/crates/editor/src/movement.rs +++ b/crates/editor/src/movement.rs @@ -688,7 +688,7 @@ mod tests { // add all kinds of inlays between two word boundaries: we should be able to cross them all, when looking for another boundary let mut id = 0; let inlays = (0..buffer_snapshot.len()) - .map(|offset| { + .flat_map(|offset| { [ Inlay { id: InlayId::Suggestion(post_inc(&mut id)), @@ -712,7 +712,6 @@ mod tests { }, ] }) - .flatten() .collect(); let snapshot = display_map.update(cx, |map, cx| { map.splice_inlays(Vec::new(), inlays, cx); diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 11691aeb71..5aefc19e32 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -2775,7 +2775,7 @@ impl Project { let project_settings = ProjectSettings::get(Some((worktree_id.to_proto() as usize, Path::new(""))), cx); let lsp = project_settings.lsp.get(&adapter.name.0); - let override_options = lsp.map(|s| s.initialization_options.clone()).flatten(); + let override_options = lsp.and_then(|s| s.initialization_options.clone()); let server_id = pending_server.server_id; let container_dir = pending_server.container_dir.clone(); diff --git a/crates/terminal_view/src/terminal_view.rs b/crates/terminal_view/src/terminal_view.rs index e756dd454b..584469d353 100644 --- a/crates/terminal_view/src/terminal_view.rs +++ b/crates/terminal_view/src/terminal_view.rs @@ -879,12 +879,9 @@ impl Item for TerminalView { .or_else(|| { cx.update(|cx| { let strategy = TerminalSettings::get_global(cx).working_directory.clone(); - workspace - .upgrade() - .map(|workspace| { - get_working_directory(workspace.read(cx), cx, strategy) - }) - .flatten() + workspace.upgrade().and_then(|workspace| { + get_working_directory(workspace.read(cx), cx, strategy) + }) }) .ok() .flatten() diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 00af090d33..dd5a0b5be8 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -108,9 +108,7 @@ fn main() { let open_listener = listener.clone(); app.on_open_urls(move |urls, _| open_listener.open_urls(&urls)); app.on_reopen(move |cx| { - if let Some(app_state) = AppState::try_global(cx) - .map(|app_state| app_state.upgrade()) - .flatten() + if let Some(app_state) = AppState::try_global(cx).and_then(|app_state| app_state.upgrade()) { workspace::open_new(&app_state, cx, |workspace, cx| { Editor::new_file(workspace, &Default::default(), cx) diff --git a/tooling/xtask/src/main.rs b/tooling/xtask/src/main.rs index 0d8a4e9457..92e8a9807a 100644 --- a/tooling/xtask/src/main.rs +++ b/tooling/xtask/src/main.rs @@ -104,7 +104,6 @@ fn run_clippy(args: ClippyArgs) -> Result<()> { "clippy::manual_find", "clippy::manual_flatten", "clippy::map_entry", - "clippy::map_flatten", "clippy::needless_arbitrary_self_type", "clippy::needless_borrowed_reference", "clippy::needless_lifetimes",