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 <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-04-25 15:22:00 -05:00 committed by GitHub
parent 5dab95335b
commit d52670c749
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 15 additions and 21 deletions

View file

@ -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();

View file

@ -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));
}
}

View file

@ -243,8 +243,7 @@ HidChooserController* ElectronHidDelegate::AddControllerForFrame(
auto controller = std::make_unique<HidChooserController>(
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);
}

View file

@ -310,7 +310,7 @@ void HidChooserContext::SetUpHidManagerConnection(
void HidChooserContext::InitDeviceList(
std::vector<device::mojom::HidDeviceInfoPtr> devices) {
for (auto& device : devices)
devices_.insert({device->guid, std::move(device)});
devices_.try_emplace(device->guid, std::move(device));
is_initialized_ = true;

View file

@ -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<InProgressRequest>(
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();

View file

@ -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++;

View file

@ -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);
}

View file

@ -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<ElectronPermissionManager*>(
@ -270,7 +270,7 @@ void SerialChooserContext::SetUpPortManagerConnection(
void SerialChooserContext::OnGetDevices(
std::vector<device::mojom::SerialPortInfoPtr> 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;
}

View file

@ -301,8 +301,7 @@ UsbChooserController* ElectronUsbDelegate::AddControllerForFrame(
auto controller = std::make_unique<UsbChooserController>(
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);
}

View file

@ -103,10 +103,8 @@ void UsbChooserContext::InitDeviceList(
std::vector<device::mojom::UsbDeviceInfoPtr> 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_)