Skip to content

Commit

Permalink
clang-tidy modernize-use-emplace (#48661)
Browse files Browse the repository at this point in the history
* Port code to emplace_back

This is clang-tidy making automated changes to convert push_back into
emplace_back whenever the argument to push_back is either a constructor
call for the type in question of an expression of a type implicitly
convertible to the type in question.

This should eliminate one move-construction of the inserted object for
each call.

* Enable clang-tidy modernize-use-emplace
  • Loading branch information
jbytheway authored May 14, 2021
1 parent ece7730 commit 552d7fa
Show file tree
Hide file tree
Showing 89 changed files with 929 additions and 933 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ readability-*,\
-modernize-pass-by-value,\
-modernize-return-braced-init-list,\
-modernize-use-default-member-init,\
-modernize-use-emplace,\
-performance-unnecessary-value-param,\
-readability-else-after-return,\
-readability-implicit-bool-conversion,\
Expand Down
4 changes: 2 additions & 2 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ void activity_handlers::fill_liquid_do_turn( player_activity *act, player *p )
act_ref.set_to_null();
} else {
if( act_ref.str_values.empty() ) {
act_ref.str_values.push_back( std::string() );
act_ref.str_values.emplace_back( );
}
act_ref.str_values.at( 0 ) = serialize( liquid );
}
Expand Down Expand Up @@ -3951,7 +3951,7 @@ void activity_handlers::fertilize_plot_do_turn( player_activity *act, player *p
auto check_fertilizer = [&]( bool ask_user = true ) -> void {
if( act->str_values.empty() )
{
act->str_values.push_back( "" );
act->str_values.emplace_back( "" );
}
fertilizer = itype_id( act->str_values[0] );

Expand Down
36 changes: 18 additions & 18 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,13 @@ void activity_handlers::washing_finish( player_activity *act, player *p )
}

std::vector<item_comp> comps;
comps.push_back( item_comp( itype_water, required.water ) );
comps.push_back( item_comp( itype_water_clean, required.water ) );
comps.emplace_back( itype_water, required.water );
comps.emplace_back( itype_water_clean, required.water );
p->consume_items( comps, 1, is_liquid_crafting_component );

std::vector<item_comp> comps1;
comps1.push_back( item_comp( itype_soap, required.cleanser ) );
comps1.push_back( item_comp( itype_detergent, required.cleanser ) );
comps1.emplace_back( itype_soap, required.cleanser );
comps1.emplace_back( itype_detergent, required.cleanser );
p->consume_items( comps1 );

p->add_msg_if_player( m_good, _( "You washed your items." ) );
Expand Down Expand Up @@ -1469,7 +1469,7 @@ static std::vector<std::tuple<tripoint, itype_id, int>> requirements_map( player
continue;
}
}
requirement_map.push_back( std::make_tuple( point_elem, map_elem.first, map_elem.second ) );
requirement_map.emplace_back( point_elem, map_elem.first, map_elem.second );
}
}
// Ok we now have a list of all the items that match the requirements, their points, and a quantity for each one.
Expand All @@ -1493,8 +1493,8 @@ static std::vector<std::tuple<tripoint, itype_id, int>> requirements_map( player
}
if( item_quantity >= quantity_required ) {
// it's just this spot that can fulfil the requirement on its own
final_map.push_back( std::make_tuple( pos_here, item_here, std::min<int>( quantity_here,
quantity_required ) ) );
final_map.emplace_back( pos_here, item_here, std::min<int>( quantity_here,
quantity_required ) );
if( quantity_here >= quantity_required ) {
line_found = true;
break;
Expand All @@ -1520,10 +1520,10 @@ static std::vector<std::tuple<tripoint, itype_id, int>> requirements_map( player
int quantity_here2 = std::get<2>( *it );
if( comp_elem.type == item_here2 ) {
if( quantity_here2 >= remainder ) {
final_map.push_back( std::make_tuple( pos_here2, item_here2, remainder ) );
final_map.emplace_back( pos_here2, item_here2, remainder );
line_found = true;
} else {
final_map.push_back( std::make_tuple( pos_here2, item_here2, remainder ) );
final_map.emplace_back( pos_here2, item_here2, remainder );
remainder -= quantity_here2;
}
}
Expand Down Expand Up @@ -1551,8 +1551,8 @@ static std::vector<std::tuple<tripoint, itype_id, int>> requirements_map( player
}
if( item_quantity >= quantity_required ) {
// it's just this spot that can fulfil the requirement on its own
final_map.push_back( std::make_tuple( pos_here, item_here, std::min<int>( quantity_here,
quantity_required ) ) );
final_map.emplace_back( pos_here, item_here, std::min<int>( quantity_here,
quantity_required ) );
if( quantity_here >= quantity_required ) {
line_found = true;
break;
Expand All @@ -1578,10 +1578,10 @@ static std::vector<std::tuple<tripoint, itype_id, int>> requirements_map( player
int quantity_here2 = std::get<2>( *it );
if( comp_elem.type == item_here2 ) {
if( quantity_here2 >= remainder ) {
final_map.push_back( std::make_tuple( pos_here2, item_here2, remainder ) );
final_map.emplace_back( pos_here2, item_here2, remainder );
line_found = true;
} else {
final_map.push_back( std::make_tuple( pos_here2, item_here2, remainder ) );
final_map.emplace_back( pos_here2, item_here2, remainder );
remainder -= quantity_here2;
}
}
Expand All @@ -1604,7 +1604,7 @@ static std::vector<std::tuple<tripoint, itype_id, int>> requirements_map( player
item test_item = item( item_here, calendar::turn_zero );
if( test_item.has_quality( tool_qual, qual_level ) ) {
// it's just this spot that can fulfil the requirement on its own
final_map.push_back( std::make_tuple( pos_here, item_here, 1 ) );
final_map.emplace_back( pos_here, item_here, 1 );
line_found = true;
break;
}
Expand Down Expand Up @@ -2002,14 +2002,14 @@ void activity_on_turn_move_loot( player_activity &act, player &p )
src_veh = &vp->vehicle();
src_part = vp->part_index();
for( auto &it : src_veh->get_items( src_part ) ) {
items.push_back( std::make_pair( &it, true ) );
items.emplace_back( &it, true );
}
} else {
src_veh = nullptr;
src_part = -1;
}
for( item &it : here.i_at( src_loc ) ) {
items.push_back( std::make_pair( &it, false ) );
items.emplace_back( &it, false );
}

//Skip items that have already been processed
Expand Down Expand Up @@ -2158,7 +2158,7 @@ static bool mine_activity( player &p, const tripoint &src_loc )
moves /= 2;
}
p.assign_activity( powered ? ACT_JACKHAMMER : ACT_PICKAXE, moves );
p.activity.targets.push_back( item_location( p, chosen_item ) );
p.activity.targets.emplace_back( p, chosen_item );
p.activity.placement = here.getabs( src_loc );
return true;

Expand Down Expand Up @@ -2648,7 +2648,7 @@ static bool generic_multi_activity_do( player &p, const activity_id &act_id,
item *best_rod = p.best_quality_item( qual_FISHING );
p.assign_activity( ACT_FISH, to_moves<int>( 5_hours ), 0,
0, best_rod->tname() );
p.activity.targets.push_back( item_location( p, best_rod ) );
p.activity.targets.emplace_back( p, best_rod );
p.activity.coord_set = g->get_fishable_locations( ACTIVITY_SEARCH_DISTANCE, src_loc );
return false;
} else if( reason == do_activity_reason::NEEDS_MINING ) {
Expand Down
24 changes: 12 additions & 12 deletions src/armor_layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,40 +330,40 @@ std::vector<std::string> clothing_flags_description( const item &worn_item )
std::vector<std::string> description_stack;

if( worn_item.has_flag( flag_FIT ) ) {
description_stack.push_back( _( "It fits you well." ) );
description_stack.emplace_back( _( "It fits you well." ) );
} else if( worn_item.has_flag( flag_VARSIZE ) ) {
description_stack.push_back( _( "It could be refitted." ) );
description_stack.emplace_back( _( "It could be refitted." ) );
}

if( worn_item.has_flag( flag_HOOD ) ) {
description_stack.push_back( _( "It has a hood." ) );
description_stack.emplace_back( _( "It has a hood." ) );
}
if( worn_item.has_flag( flag_POCKETS ) ) {
description_stack.push_back( _( "It has pockets." ) );
description_stack.emplace_back( _( "It has pockets." ) );
}
if( worn_item.has_flag( flag_WATERPROOF ) ) {
description_stack.push_back( _( "It is waterproof." ) );
description_stack.emplace_back( _( "It is waterproof." ) );
}
if( worn_item.has_flag( flag_WATER_FRIENDLY ) ) {
description_stack.push_back( _( "It is water friendly." ) );
description_stack.emplace_back( _( "It is water friendly." ) );
}
if( worn_item.has_flag( flag_FANCY ) ) {
description_stack.push_back( _( "It looks fancy." ) );
description_stack.emplace_back( _( "It looks fancy." ) );
}
if( worn_item.has_flag( flag_SUPER_FANCY ) ) {
description_stack.push_back( _( "It looks really fancy." ) );
description_stack.emplace_back( _( "It looks really fancy." ) );
}
if( worn_item.has_flag( flag_FLOTATION ) ) {
description_stack.push_back( _( "You will not drown today." ) );
description_stack.emplace_back( _( "You will not drown today." ) );
}
if( worn_item.has_flag( flag_OVERSIZE ) ) {
description_stack.push_back( _( "It is very bulky." ) );
description_stack.emplace_back( _( "It is very bulky." ) );
}
if( worn_item.has_flag( flag_SWIM_GOGGLES ) ) {
description_stack.push_back( _( "It helps you to see clearly underwater." ) );
description_stack.emplace_back( _( "It helps you to see clearly underwater." ) );
}
if( worn_item.has_flag( flag_SEMITANGIBLE ) ) {
description_stack.push_back( _( "It can occupy the same space as other things." ) );
description_stack.emplace_back( _( "It can occupy the same space as other things." ) );
}

return description_stack;
Expand Down
4 changes: 2 additions & 2 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,11 +745,11 @@ void avatar::do_read( item &book )
player *n = g->find_npc( character_id( activity.values[i] ) );
if( n != nullptr ) {
const std::string &s = activity.get_str_value( i, "1" );
learners.push_back( { n, strtod( s.c_str(), nullptr ) } );
learners.emplace_back( n, strtod( s.c_str(), nullptr ) );
}
// Otherwise they must have died/teleported or something
}
learners.push_back( { this, 1.0 } );
learners.emplace_back( this, 1.0 );
//whether to continue reading or not
bool continuous = false;
// NPCs who learned a little about the skill
Expand Down
16 changes: 8 additions & 8 deletions src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2199,13 +2199,13 @@ bool Character::uninstall_bionic( const bionic_id &b_id, player &installer, bool
activity.values.push_back( success );
activity.values.push_back( units::to_kilojoule( b_id->capacity ) );
activity.values.push_back( pl_skill );
activity.str_values.push_back( "uninstall" );
activity.str_values.emplace_back( "uninstall" );
activity.str_values.push_back( b_id.str() );
activity.str_values.push_back( "" ); // installer_name is unused for uninstall
activity.str_values.emplace_back( "" ); // installer_name is unused for uninstall
if( autodoc ) {
activity.str_values.push_back( "true" );
activity.str_values.emplace_back( "true" );
} else {
activity.str_values.push_back( "false" );
activity.str_values.emplace_back( "false" );
}
for( const std::pair<const bodypart_str_id, size_t> &elem : b_id->occupied_bodyparts ) {
add_effect( effect_under_operation, difficulty * 20_minutes, elem.first.id(), true, difficulty );
Expand Down Expand Up @@ -2486,18 +2486,18 @@ bool Character::install_bionics( const itype &type, player &installer, bool auto
activity.values.push_back( success );
activity.values.push_back( units::to_millijoule( bioid->capacity ) );
activity.values.push_back( pl_skill );
activity.str_values.push_back( "install" );
activity.str_values.emplace_back( "install" );
activity.str_values.push_back( bioid.str() );

if( installer.has_trait( trait_PROF_MED ) || installer.has_trait( trait_PROF_AUTODOC ) ) {
activity.str_values.push_back( installer.disp_name( true ) );
} else {
activity.str_values.push_back( "NOT_MED" );
activity.str_values.emplace_back( "NOT_MED" );
}
if( autodoc ) {
activity.str_values.push_back( "true" );
activity.str_values.emplace_back( "true" );
} else {
activity.str_values.push_back( "false" );
activity.str_values.emplace_back( "false" );
}
for( const std::pair<const bodypart_str_id, size_t> &elem : bioid->occupied_bodyparts ) {
add_effect( effect_under_operation, difficulty * 20_minutes, elem.first.id(), true, difficulty );
Expand Down
4 changes: 2 additions & 2 deletions src/bionics_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ static std::string build_bionic_poweronly_string( const bionic &bio )
bio_data.charge_time ) );
}
if( bio_data.has_flag( STATIC( json_character_flag( "BIONIC_TOGGLED" ) ) ) ) {
properties.push_back( bio.powered ? _( "ON" ) : _( "OFF" ) );
properties.emplace_back( bio.powered ? _( "ON" ) : _( "OFF" ) );
}
if( bio.incapacitated_time > 0_turns ) {
properties.push_back( _( "(incapacitated)" ) );
properties.emplace_back( _( "(incapacitated)" ) );
}
if( bio.get_safe_fuel_thresh() > 0 && ( !bio.info().fuel_opts.empty() ||
bio.info().is_remote_fueled ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3493,7 +3493,7 @@ std::vector<item_location> Character::find_reloadables()
node->remaining_ammo_capacity() > 0;
}
if( reloadable ) {
reloadables.push_back( item_location( *this, node ) );
reloadables.emplace_back( *this, node );
}
return VisitResponse::NEXT;
} );
Expand Down
2 changes: 1 addition & 1 deletion src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ void zone_manager::zone_edited( zone_data &zone )
}
}
//Add it to the list of changed zones
changed_vzones.push_back( std::make_pair( zone_data( zone ), &zone ) );
changed_vzones.emplace_back( zone_data( zone ), &zone );
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/construction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ construction_id construction_menu( const bool blueprint )
}
current_buffer_location += construct_buffers[i].size();
if( i < construct_buffers.size() - 1 ) {
full_construct_buffer.push_back( std::string() );
full_construct_buffer.emplace_back( );
current_buffer_location++;
}
}
Expand Down Expand Up @@ -1781,7 +1781,7 @@ void finalize_constructions()
if( !vp.has_flag( flag_INITIAL_PART ) ) {
continue;
}
frame_items.push_back( item_comp( vp.base_item, 1 ) );
frame_items.emplace_back( vp.base_item, 1 );
}

if( frame_items.empty() ) {
Expand Down
2 changes: 1 addition & 1 deletion src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2314,7 +2314,7 @@ void Character::disassemble_all( bool one_pass )
bool found_any = false;
std::vector<item_location> to_disassemble;
for( item &it : get_map().i_at( pos() ) ) {
to_disassemble.push_back( item_location( map_cursor( pos() ), &it ) );
to_disassemble.emplace_back( map_cursor( pos() ), &it );
}
for( item_location &it_loc : to_disassemble ) {
// Prevent disassembling an in process disassembly because it could have been created by a previous iteration of this loop
Expand Down
6 changes: 3 additions & 3 deletions src/crafting_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ const recipe *select_crafting_recipe( int &batch_size_out )
current.clear();
for( int i = 1; i <= 50; i++ ) {
current.push_back( chosen );
available.push_back( availability( chosen, i ) );
available.emplace_back( chosen, i );
}
} else {
static_popup popup;
Expand Down Expand Up @@ -1091,7 +1091,7 @@ std::string peek_related_recipe( const recipe *current, const recipe_subset &ava
const requirement_data &req = current->simple_requirements();
for( const std::vector<item_comp> &comp_list : req.get_components() ) {
for( const item_comp &a : comp_list ) {
related_components.push_back( { a.type, item::nname( a.type, 1 ) } );
related_components.emplace_back( a.type, item::nname( a.type, 1 ) );
}
}
std::sort( related_components.begin(), related_components.end(), compare_second );
Expand All @@ -1104,7 +1104,7 @@ std::string peek_related_recipe( const recipe *current, const recipe_subset &ava
get_player_character().get_learned_recipes().of_component( tid );
for( const auto &b : known_recipes ) {
if( available.contains( b ) ) {
related_results.push_back( { b->result(), b->result_name() } );
related_results.emplace_back( b->result(), b->result_name() );
}
}
std::stable_sort( related_results.begin(), related_results.end(), compare_second );
Expand Down
8 changes: 4 additions & 4 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ void Creature::process_effects()
// Add any effects that others remove to the removal list
for( const auto &removed_effect : _it.second.get_removes_effects() ) {
rem_ids.push_back( removed_effect );
rem_bps.push_back( bodypart_str_id::NULL_ID() );
rem_bps.emplace_back( bodypart_str_id::NULL_ID() );
}
effect &e = _it.second;
const int prev_int = e.get_intensity();
Expand Down Expand Up @@ -2115,7 +2115,7 @@ std::vector<bodypart_id> Creature::get_all_body_parts( get_body_part_flags flags
if( only_main && elem.first->main_part != elem.first ) {
continue;
}
all_bps.push_back( elem.first );
all_bps.emplace_back( elem.first );
}

if( flags & get_body_part_flags::sorted ) {
Expand Down Expand Up @@ -2596,11 +2596,11 @@ void Creature::describe_infrared( std::vector<std::string> &buf ) const
size_str = "invalid";
break;
}
buf.push_back( _( "You see a figure radiating heat." ) );
buf.emplace_back( _( "You see a figure radiating heat." ) );
buf.push_back( string_format( _( "It is %s in size." ), size_str ) );
}

void Creature::describe_specials( std::vector<std::string> &buf ) const
{
buf.push_back( _( "You sense a creature here." ) );
buf.emplace_back( _( "You sense a creature here." ) );
}
3 changes: 2 additions & 1 deletion src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,8 @@ static void debug_menu_game_state()
std::vector<std::pair<m_flag, int>> sorted;
sorted.reserve( m_flag::MF_MAX );
for( int f = 0; f < m_flag::MF_MAX; f++ ) {
sorted.push_back( {static_cast<m_flag>( f ), MonsterGenerator::generator().m_flag_usage_stats[f]} );
sorted.emplace_back( static_cast<m_flag>( f ),
MonsterGenerator::generator().m_flag_usage_stats[f] );
}
std::sort( sorted.begin(), sorted.end(), []( std::pair<m_flag, int> a, std::pair<m_flag, int> b ) {
return a.second != b.second ? a.second > b.second : a.first < b.first;
Expand Down
2 changes: 1 addition & 1 deletion src/dialogue_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ size_t dialogue_window::add_to_history( const std::string &text )
// Empty line between lines of dialogue
void dialogue_window::add_history_separator()
{
history.push_back( "" );
history.emplace_back( "" );
}

void dialogue_window::print_history( const size_t hilight_lines )
Expand Down
Loading

0 comments on commit 552d7fa

Please sign in to comment.