From d52670c749ff10ae4b959fadb3fb5c4d19583e16 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Fri, 25 Apr 2025 15:22:00 -0500 Subject: [PATCH] refactor: use `std::map::try_emplace()` over `std::map::insert()` (#46794) refactor: prefer std::map::try_emplace() over std::map::insert() Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- shell/browser/electron_autofill_driver_factory.cc | 3 +-- .../file_system_access_permission_context.cc | 2 +- shell/browser/hid/electron_hid_delegate.cc | 3 +-- shell/browser/hid/hid_chooser_context.cc | 2 +- shell/browser/net/proxying_url_loader_factory.cc | 4 ++-- shell/browser/notifications/mac/cocoa_notification.mm | 4 ++-- shell/browser/serial/electron_serial_delegate.cc | 3 +-- shell/browser/serial/serial_chooser_context.cc | 4 ++-- shell/browser/usb/electron_usb_delegate.cc | 3 +-- shell/browser/usb/usb_chooser_context.cc | 8 +++----- 10 files changed, 15 insertions(+), 21 deletions(-) diff --git a/shell/browser/electron_autofill_driver_factory.cc b/shell/browser/electron_autofill_driver_factory.cc index b2a68d4ec036..84631924d09f 100644 --- a/shell/browser/electron_autofill_driver_factory.cc +++ b/shell/browser/electron_autofill_driver_factory.cc @@ -94,8 +94,7 @@ AutofillDriver* AutofillDriverFactory::DriverForFrame( void AutofillDriverFactory::AddDriverForFrame( content::RenderFrameHost* render_frame_host, CreationCallback factory_method) { - auto insertion_result = - driver_map_.insert(std::make_pair(render_frame_host, nullptr)); + auto insertion_result = driver_map_.try_emplace(render_frame_host, nullptr); // This can be called twice for the key representing the main frame. if (insertion_result.second) { insertion_result.first->second = std::move(factory_method).Run(); diff --git a/shell/browser/file_system_access/file_system_access_permission_context.cc b/shell/browser/file_system_access/file_system_access_permission_context.cc index 865fa06f19ba..833f3992f581 100644 --- a/shell/browser/file_system_access/file_system_access_permission_context.cc +++ b/shell/browser/file_system_access/file_system_access_permission_context.cc @@ -745,7 +745,7 @@ void FileSystemAccessPermissionContext::SetLastPickedDirectory( base::Value::Dict dict; dict.Set(GenerateLastPickedDirectoryKey(id), std::move(entry)); MaybeEvictEntries(dict); - id_pathinfo_map_.insert(std::make_pair(origin, std::move(dict))); + id_pathinfo_map_.try_emplace(origin, std::move(dict)); } } diff --git a/shell/browser/hid/electron_hid_delegate.cc b/shell/browser/hid/electron_hid_delegate.cc index 1915d4e48939..3f0f6fd091f7 100644 --- a/shell/browser/hid/electron_hid_delegate.cc +++ b/shell/browser/hid/electron_hid_delegate.cc @@ -243,8 +243,7 @@ HidChooserController* ElectronHidDelegate::AddControllerForFrame( auto controller = std::make_unique( render_frame_host, std::move(filters), std::move(exclusion_filters), std::move(callback), web_contents, weak_factory_.GetWeakPtr()); - controller_map_.insert( - std::make_pair(render_frame_host, std::move(controller))); + controller_map_.try_emplace(render_frame_host, std::move(controller)); return ControllerForFrame(render_frame_host); } diff --git a/shell/browser/hid/hid_chooser_context.cc b/shell/browser/hid/hid_chooser_context.cc index 4869f56eb8de..8c27a729ff58 100644 --- a/shell/browser/hid/hid_chooser_context.cc +++ b/shell/browser/hid/hid_chooser_context.cc @@ -310,7 +310,7 @@ void HidChooserContext::SetUpHidManagerConnection( void HidChooserContext::InitDeviceList( std::vector devices) { for (auto& device : devices) - devices_.insert({device->guid, std::move(device)}); + devices_.try_emplace(device->guid, std::move(device)); is_initialized_ = true; diff --git a/shell/browser/net/proxying_url_loader_factory.cc b/shell/browser/net/proxying_url_loader_factory.cc index cee7c0f756e4..572e5e4ae6de 100644 --- a/shell/browser/net/proxying_url_loader_factory.cc +++ b/shell/browser/net/proxying_url_loader_factory.cc @@ -883,9 +883,9 @@ void ProxyingURLLoaderFactory::OnLoaderForCorsPreflightCreated( // sending request headers is very difficult. const uint64_t web_request_id = ++(*request_id_generator_); - auto result = requests_.insert(std::make_pair( + auto result = requests_.try_emplace( web_request_id, std::make_unique( - this, web_request_id, frame_routing_id_, request))); + this, web_request_id, frame_routing_id_, request)); result.first->second->OnLoaderCreated(std::move(receiver)); result.first->second->Restart(); diff --git a/shell/browser/notifications/mac/cocoa_notification.mm b/shell/browser/notifications/mac/cocoa_notification.mm index a90842f09616..2408fbd98cab 100644 --- a/shell/browser/notifications/mac/cocoa_notification.mm +++ b/shell/browser/notifications/mac/cocoa_notification.mm @@ -93,8 +93,8 @@ void CocoaNotification::Show(const NotificationOptions& options) { actionWithIdentifier:actionIdentifier title:base::SysUTF16ToNSString(action.text)]; [additionalActions addObject:notificationAction]; - additional_action_indices_.insert( - std::make_pair(base::SysNSStringToUTF8(actionIdentifier), i)); + additional_action_indices_.try_emplace( + base::SysNSStringToUTF8(actionIdentifier), i); } } i++; diff --git a/shell/browser/serial/electron_serial_delegate.cc b/shell/browser/serial/electron_serial_delegate.cc index e7b9e551fe1d..bd8f5c5a85c6 100644 --- a/shell/browser/serial/electron_serial_delegate.cc +++ b/shell/browser/serial/electron_serial_delegate.cc @@ -116,8 +116,7 @@ SerialChooserController* ElectronSerialDelegate::AddControllerForFrame( render_frame_host, std::move(filters), std::move(allowed_bluetooth_service_class_ids), std::move(callback), web_contents, weak_factory_.GetWeakPtr()); - controller_map_.insert( - std::make_pair(render_frame_host, std::move(controller))); + controller_map_.try_emplace(render_frame_host, std::move(controller)); return ControllerForFrame(render_frame_host); } diff --git a/shell/browser/serial/serial_chooser_context.cc b/shell/browser/serial/serial_chooser_context.cc index be387c644e9a..a857f6464bf7 100644 --- a/shell/browser/serial/serial_chooser_context.cc +++ b/shell/browser/serial/serial_chooser_context.cc @@ -85,7 +85,7 @@ void SerialChooserContext::GrantPortPermission( const url::Origin& origin, const device::mojom::SerialPortInfo& port, content::RenderFrameHost* render_frame_host) { - port_info_.insert({port.token, port.Clone()}); + port_info_.try_emplace(port.token, port.Clone()); if (CanStorePersistentEntry(port)) { auto* permission_manager = static_cast( @@ -270,7 +270,7 @@ void SerialChooserContext::SetUpPortManagerConnection( void SerialChooserContext::OnGetDevices( std::vector ports) { for (auto& port : ports) - port_info_.insert({port->token, std::move(port)}); + port_info_.try_emplace(port->token, std::move(port)); is_initialized_ = true; } diff --git a/shell/browser/usb/electron_usb_delegate.cc b/shell/browser/usb/electron_usb_delegate.cc index 4e3827d04f74..535e30e60ba7 100644 --- a/shell/browser/usb/electron_usb_delegate.cc +++ b/shell/browser/usb/electron_usb_delegate.cc @@ -301,8 +301,7 @@ UsbChooserController* ElectronUsbDelegate::AddControllerForFrame( auto controller = std::make_unique( render_frame_host, std::move(options), std::move(callback), web_contents, weak_factory_.GetWeakPtr()); - controller_map_.insert( - std::make_pair(render_frame_host, std::move(controller))); + controller_map_.try_emplace(render_frame_host, std::move(controller)); return ControllerForFrame(render_frame_host); } diff --git a/shell/browser/usb/usb_chooser_context.cc b/shell/browser/usb/usb_chooser_context.cc index 1273b7dbd41e..42fd6b2e4284 100644 --- a/shell/browser/usb/usb_chooser_context.cc +++ b/shell/browser/usb/usb_chooser_context.cc @@ -103,10 +103,8 @@ void UsbChooserContext::InitDeviceList( std::vector devices) { for (auto& device_info : devices) { DCHECK(device_info); - if (ShouldExposeDevice(*device_info)) { - devices_.insert( - std::make_pair(device_info->guid, std::move(device_info))); - } + if (ShouldExposeDevice(*device_info)) + devices_.try_emplace(device_info->guid, std::move(device_info)); } is_initialized_ = true; @@ -285,7 +283,7 @@ void UsbChooserContext::OnDeviceAdded( DCHECK(!devices_.contains(device_info->guid)); if (!ShouldExposeDevice(*device_info)) return; - devices_.insert(std::make_pair(device_info->guid, device_info->Clone())); + devices_.try_emplace(device_info->guid, device_info->Clone()); // Notify all observers. for (auto& observer : device_observer_list_)