Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code optimizations reported by static code analysis (2019-07-11) #32315

Merged
merged 11 commits into from
Jul 11, 2019
4 changes: 2 additions & 2 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ action_id look_up_action( const std::string &ident )
}
// ^^ Temporarily for the interface with the input manager!
for( int i = 0; i < NUM_ACTIONS; i++ ) {
if( action_ident( action_id( i ) ) == ident ) {
return action_id( i );
if( action_ident( static_cast<action_id>( i ) ) == ident ) {
return static_cast<action_id>( i );
}
}
return ACTION_NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2668,7 +2668,6 @@ void activity_handlers::drive_do_turn( player_activity *act, player *p )

void activity_handlers::travel_do_turn( player_activity *act, player *p )
{
const activity_id act_travel = activity_id( "ACT_TRAVELLING" );
if( !p->omt_path.empty() ) {
p->omt_path.pop_back();
if( p->omt_path.empty() ) {
Expand All @@ -2691,6 +2690,7 @@ void activity_handlers::travel_do_turn( player_activity *act, player *p )
const auto route_to = g->m.route( p->pos(), centre_sub, p->get_pathfinding_settings(),
p->get_path_avoid() );
if( !route_to.empty() ) {
const activity_id act_travel = activity_id( "ACT_TRAVELLING" );
p->set_destination( route_to, player_activity( act_travel ) );
} else {
p->add_msg_if_player( _( "You cannot reach that destination" ) );
Expand Down
2 changes: 1 addition & 1 deletion src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ void activity_on_turn_blueprint_move( player_activity &, player &p )
// If we got here without restarting the activity, it means we're done.
if( p.is_npc() ) {
npc *guy = dynamic_cast<npc *>( &p );
guy->current_activity = "";
guy->current_activity.clear();
guy->revert_after_activity();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/armor_layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ std::string body_part_names( const std::vector<body_part> &parts )
names.reserve( parts.size() );
for( size_t i = 0; i < parts.size(); ++i ) {
const body_part part = parts[i];
if( i + 1 < parts.size() && parts[i + 1] == body_part( bp_aiOther[part] ) ) {
if( i + 1 < parts.size() && parts[i + 1] == static_cast<body_part>( bp_aiOther[part] ) ) {
// Can combine two body parts (e.g. arms)
names.push_back( body_part_name_accusative( part, 2 ) );
++i;
Expand Down
28 changes: 14 additions & 14 deletions src/artifact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,14 @@ std::string new_artifact()
def.tool->def_charges = def.tool->max_charges;
// If we have charges, pick a recharge mechanism
if( def.tool->max_charges > 0 ) {
def.artifact->charge_type = art_charge( rng( ARTC_NULL + 1, NUM_ARTCS - 1 ) );
def.artifact->charge_type = static_cast<art_charge>( rng( ARTC_NULL + 1, NUM_ARTCS - 1 ) );
}
if( one_in( 8 ) && num_bad + num_good >= 4 ) {
def.artifact->charge_type = ARTC_NULL; // 1 in 8 chance that it can't recharge!
}
// Maybe pick an extra recharge requirement
if( one_in( std::max( 1, 6 - num_good ) ) && def.artifact->charge_type != ARTC_NULL ) {
def.artifact->charge_req = art_charge_req( rng( ACR_NULL + 1, NUM_ACRS - 1 ) );
def.artifact->charge_req = static_cast<art_charge_req>( rng( ACR_NULL + 1, NUM_ACRS - 1 ) );
}
// Assign dream data (stored individually so they can be overridden in json)
def.artifact->dream_msg_unmet = artifact_dream_data[static_cast<int>
Expand Down Expand Up @@ -923,8 +923,8 @@ std::string new_natural_artifact( artifact_natural_property prop )
const artifact_shape_datum &shape_data = random_entry_ref( artifact_shape_data );
// Pick a property
const artifact_natural_property property = ( prop > ARTPROP_NULL ? prop :
artifact_natural_property( rng( ARTPROP_NULL + 1,
ARTPROP_MAX - 1 ) ) );
static_cast<artifact_natural_property>( rng( ARTPROP_NULL + 1,
ARTPROP_MAX - 1 ) ) );
const artifact_property_datum &property_data = artifact_property_data[property];

def.sym = ":";
Expand Down Expand Up @@ -968,25 +968,25 @@ std::string new_natural_artifact( artifact_natural_property prop )
if( good_passive ) {
aep_good = random_entry_ref( property_data.passive_good );
if( aep_good == AEP_NULL || one_in( 4 ) ) {
aep_good = art_effect_passive( rng( AEP_NULL + 1, AEP_SPLIT - 1 ) );
aep_good = static_cast<art_effect_passive>( rng( AEP_NULL + 1, AEP_SPLIT - 1 ) );
}
}
if( bad_passive ) {
aep_bad = random_entry_ref( property_data.passive_bad );
if( aep_bad == AEP_NULL || one_in( 4 ) ) {
aep_bad = art_effect_passive( rng( AEP_SPLIT + 1, NUM_AEAS - 1 ) );
aep_bad = static_cast<art_effect_passive>( rng( AEP_SPLIT + 1, NUM_AEAS - 1 ) );
}
}
if( good_active ) {
aea_good = random_entry_ref( property_data.active_good );
if( aea_good == AEA_NULL || one_in( 4 ) ) {
aea_good = art_effect_active( rng( AEA_NULL + 1, AEA_SPLIT - 1 ) );
aea_good = static_cast<art_effect_active>( rng( AEA_NULL + 1, AEA_SPLIT - 1 ) );
}
}
if( bad_active ) {
aea_bad = random_entry_ref( property_data.active_bad );
if( aea_bad == AEA_NULL || one_in( 4 ) ) {
aea_bad = art_effect_active( rng( AEA_SPLIT + 1, NUM_AEAS - 1 ) );
aea_bad = static_cast<art_effect_active>( rng( AEA_SPLIT + 1, NUM_AEAS - 1 ) );
}
}

Expand All @@ -1012,10 +1012,10 @@ std::string new_natural_artifact( artifact_natural_property prop )
// (When "implanting" them in a mundane item, this ability may be lost
if( !def.artifact->effects_activated.empty() ) {
def.tool->def_charges = def.tool->max_charges = rng( 1, 4 );
def.artifact->charge_type = art_charge( rng( ARTC_NULL + 1, NUM_ARTCS - 1 ) );
def.artifact->charge_type = static_cast<art_charge>( rng( ARTC_NULL + 1, NUM_ARTCS - 1 ) );
//Maybe pick an extra recharge requirement
if( one_in( 6 ) ) {
def.artifact->charge_req = art_charge_req( rng( ACR_NULL + 1, NUM_ACRS - 1 ) );
def.artifact->charge_req = static_cast<art_charge_req>( rng( ACR_NULL + 1, NUM_ACRS - 1 ) );
}
}
// Assign dream data (stored individually so they can be overridden in json)
Expand Down Expand Up @@ -1062,7 +1062,7 @@ std::vector<art_effect_passive> fill_good_passive()
{
std::vector<art_effect_passive> ret;
for( int i = AEP_NULL + 1; i < AEP_SPLIT; i++ ) {
ret.push_back( art_effect_passive( i ) );
ret.push_back( static_cast<art_effect_passive>( i ) );
}
return ret;
}
Expand All @@ -1071,7 +1071,7 @@ std::vector<art_effect_passive> fill_bad_passive()
{
std::vector<art_effect_passive> ret;
for( int i = AEP_SPLIT + 1; i < NUM_AEPS; i++ ) {
ret.push_back( art_effect_passive( i ) );
ret.push_back( static_cast<art_effect_passive>( i ) );
}
return ret;
}
Expand All @@ -1080,7 +1080,7 @@ std::vector<art_effect_active> fill_good_active()
{
std::vector<art_effect_active> ret;
for( int i = AEA_NULL + 1; i < AEA_SPLIT; i++ ) {
ret.push_back( art_effect_active( i ) );
ret.push_back( static_cast<art_effect_active>( i ) );
}
return ret;
}
Expand All @@ -1089,7 +1089,7 @@ std::vector<art_effect_active> fill_bad_active()
{
std::vector<art_effect_active> ret;
for( int i = AEA_SPLIT + 1; i < NUM_AEAS; i++ ) {
ret.push_back( art_effect_active( i ) );
ret.push_back( static_cast<art_effect_active>( i ) );
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion src/calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ void calendar::sync()
// mid-game, the result could be the wrong season!
season = initial_season;
} else {
season = season_type( turn_number / DAYS( sl ) % 4 );
season = static_cast<season_type>( turn_number / DAYS( sl ) % 4 );
}

day = turn_number / DAYS( 1 ) % sl;
Expand Down
5 changes: 3 additions & 2 deletions src/computer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ void computer::load_data( const std::string &data )

dump >> tmpname >> tmpaction >> tmpsec;

add_option( string_replace( tmpname, "_", " " ), computer_action( tmpaction ), tmpsec );
add_option( string_replace( tmpname, "_", " " ), static_cast<computer_action>( tmpaction ),
tmpsec );
}

// Pull in failures
Expand All @@ -341,7 +342,7 @@ void computer::load_data( const std::string &data )
for( int n = 0; n < failsize; n++ ) {
int tmpfail;
dump >> tmpfail;
add_failure( computer_failure_type( tmpfail ) );
add_failure( static_cast<computer_failure_type>( tmpfail ) );
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ void debug()

case DEBUG_SPAWN_ARTIFACT:
if( const cata::optional<tripoint> center = g->look_around() ) {
artifact_natural_property prop = artifact_natural_property( rng( ARTPROP_NULL + 1,
artifact_natural_property prop = static_cast<artifact_natural_property>( rng( ARTPROP_NULL + 1,
ARTPROP_MAX - 1 ) );
m.create_anomaly( *center, prop );
m.spawn_natural_artifact( *center, prop );
Expand Down
28 changes: 14 additions & 14 deletions src/defense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,35 +538,35 @@ void defense_game::setup()
switch( selection ) {
case 1: // Scenario selection
if( action == "RIGHT" ) {
if( style == defense_style( NUM_DEFENSE_STYLES - 1 ) ) {
style = defense_style( 1 );
if( style == static_cast<defense_style>( NUM_DEFENSE_STYLES - 1 ) ) {
style = static_cast<defense_style>( 1 );
} else {
style = defense_style( style + 1 );
style = static_cast<defense_style>( style + 1 );
}
}
if( action == "LEFT" ) {
if( style == defense_style( 1 ) ) {
style = defense_style( NUM_DEFENSE_STYLES - 1 );
if( style == static_cast<defense_style>( 1 ) ) {
style = static_cast<defense_style>( NUM_DEFENSE_STYLES - 1 );
} else {
style = defense_style( style - 1 );
style = static_cast<defense_style>( style - 1 );
}
}
init_to_style( style );
break;

case 2: // Location selection
if( action == "RIGHT" ) {
if( location == defense_location( NUM_DEFENSE_LOCATIONS - 1 ) ) {
location = defense_location( 1 );
if( location == static_cast<defense_location>( NUM_DEFENSE_LOCATIONS - 1 ) ) {
location = static_cast<defense_location>( 1 );
} else {
location = defense_location( location + 1 );
location = static_cast<defense_location>( location + 1 );
}
}
if( action == "LEFT" ) {
if( location == defense_location( 1 ) ) {
location = defense_location( NUM_DEFENSE_LOCATIONS - 1 );
if( location == static_cast<defense_location>( 1 ) ) {
location = static_cast<defense_location>( NUM_DEFENSE_LOCATIONS - 1 );
} else {
location = defense_location( location - 1 );
location = static_cast<defense_location>( location - 1 );
}
}
mvwprintz( w, 5, 2, c_black, "\
Expand Down Expand Up @@ -900,7 +900,7 @@ void defense_game::caravan()

// Init the items for each category
for( int i = 0; i < NUM_CARAVAN_CATEGORIES; i++ ) {
items[i] = caravan_items( caravan_category( i ) );
items[i] = caravan_items( static_cast<caravan_category>( i ) );
for( std::vector<itype_id>::iterator it = items[i].begin();
it != items[i].end(); ) {
if( current_wave == 0 || !one_in( 4 ) ) {
Expand Down Expand Up @@ -1286,7 +1286,7 @@ void draw_caravan_categories( const catacurses::window &w, int category_selected

for( int i = 0; i < NUM_CARAVAN_CATEGORIES; i++ ) {
mvwprintz( w, i + 3, 1, ( i == category_selected ? h_white : c_white ),
caravan_category_name( caravan_category( i ) ) );
caravan_category_name( static_cast<caravan_category>( i ) ) );
}
wrefresh( w );
}
Expand Down
4 changes: 2 additions & 2 deletions src/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void event::actualize()

g->u.add_memorial_log( pgettext( "memorial_male", "Became wanted by the police!" ),
pgettext( "memorial_female", "Became wanted by the police!" ) );
int robx = ( u_pos.x > map_point.x ? 0 - SEEX * 2 : SEEX * 4 );
int roby = ( u_pos.y > map_point.y ? 0 - SEEY * 2 : SEEY * 4 );
int robx = u_pos.x > map_point.x ? 0 - SEEX * 2 : SEEX * 4;
int roby = u_pos.y > map_point.y ? 0 - SEEY * 2 : SEEY * 4;
g->summon_mon( robot_type, tripoint( robx, roby, g->u.posz() ) );
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/grab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ bool game::grabbed_veh_move( const tripoint &dp )
next_grab.x != 0 && next_grab.y != 0 ) {
// Zig-zag (or semi-zig-zag) pull: player is diagonal to vehicle
// and moves away from it, but not directly away
dp_veh.x = ( dp.x == -dp_veh.x ) ? 0 : dp_veh.x;
dp_veh.y = ( dp.y == -dp_veh.y ) ? 0 : dp_veh.y;
dp_veh.x = dp.x == -dp_veh.x ? 0 : dp_veh.x;
dp_veh.y = dp.y == -dp_veh.y ? 0 : dp_veh.y;

next_grab = -dp_veh;
zigzag = true;
Expand Down
1 change: 0 additions & 1 deletion src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ static const bionic_id bio_remote( "bio_remote" );

static const trait_id trait_HIBERNATE( "HIBERNATE" );
static const trait_id trait_SHELL2( "SHELL2" );
static const trait_id trait_DEBUG_HS( "DEBUG_HS" );

const skill_id skill_driving( "driving" );
const skill_id skill_melee( "melee" );
Expand Down
2 changes: 1 addition & 1 deletion src/help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Press ESC to return to the game." ) ) + 1;
second_column = std::max( second_column, utf8_width( cat_name ) + 4 );
}

shortcut_print( win, y + i % half_size, ( i < half_size ? 1 : second_column ),
shortcut_print( win, y + i % half_size, i < half_size ? 1 : second_column,
c_white, c_light_blue, cat_name );
}

Expand Down
4 changes: 2 additions & 2 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ std::string item::info( std::vector<iteminfo> &info, const iteminfo_query *parts
return ( g->u.vitamin_rate( v.first ) > 0_turns &&
v.second != 0 ) // only display vitamins that we actually require
? string_format( "%s (%i%%)", v.first.obj().name(),
int( v.second * g->u.vitamin_rate( v.first ) / 1_days * 100 ) )
static_cast<int>( v.second * g->u.vitamin_rate( v.first ) / 1_days * 100 ) )
: std::string();
} );
if( !required_vits.empty() && parts->test( iteminfo_parts::FOOD_VITAMINS ) ) {
Expand Down Expand Up @@ -4446,7 +4446,7 @@ bool item::ready_to_revive( const tripoint &pos ) const
return false;
}
int age_in_hours = to_hours<int>( age() );
age_in_hours -= int( static_cast<float>( burnt ) / ( volume() / 250_ml ) );
age_in_hours -= static_cast<int>( static_cast<float>( burnt ) / ( volume() / 250_ml ) );
if( damage_level( 4 ) > 0 ) {
age_in_hours /= ( damage_level( 4 ) + 1 );
}
Expand Down
2 changes: 1 addition & 1 deletion src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6567,7 +6567,7 @@ static std::string colorized_trap_name_at( const tripoint &point )
std::string name;
if( !trap.is_null() && trap.get_visibility() <= 1 ) {
name = colorize( trap.name(), trap.color ) + _( " on " );
};
}
return name;
}

Expand Down
8 changes: 4 additions & 4 deletions src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,8 @@ void consume_drug_iuse::info( const item &, std::vector<iteminfo> &dump ) const
if( rate <= 0_turns ) {
return std::string();
}
const int lo = int( v.second.first * rate / 1_days * 100 );
const int hi = int( v.second.second * rate / 1_days * 100 );
const int lo = static_cast<int>( v.second.first * rate / 1_days * 100 );
const int hi = static_cast<int>( v.second.second * rate / 1_days * 100 );

return string_format( lo == hi ? "%s (%i%%)" : "%s (%i-%i%%)", v.first.obj().name(), lo,
hi );
Expand Down Expand Up @@ -3561,7 +3561,7 @@ hp_part heal_actor::use_healing_item( player &healer, player &patient, item &it,
int highest_damage = 0;
for( int i = 0; i < num_hp_parts; i++ ) {
int damage = 0;
const body_part i_bp = player::hp_to_bp( hp_part( i ) );
const body_part i_bp = player::hp_to_bp( static_cast<hp_part>( i ) );
if( !patient.has_effect( effect_bandaged, i_bp ) ) {
damage += patient.hp_max[i] - patient.hp_cur[i];
}
Expand All @@ -3570,7 +3570,7 @@ hp_part heal_actor::use_healing_item( player &healer, player &patient, item &it,
damage += infect * patient.get_effect_dur( effect_infected, i_bp ) / 10_minutes;
if( damage > highest_damage ) {
highest_damage = damage;
healed = hp_part( i );
healed = static_cast<hp_part>( i );
}
}
} else if( patient.is_player() ) {
Expand Down
4 changes: 2 additions & 2 deletions src/iuse_software_snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ int snake_game::start_game()
std::vector<std::pair<int, int> > vSnakeBody;
std::map<int, std::map<int, bool> > mSnakeBody;

int iOffsetX = ( TERMX > FULL_SCREEN_WIDTH ) ? ( TERMX - FULL_SCREEN_WIDTH ) / 2 : 0;
int iOffsetY = ( TERMY > FULL_SCREEN_HEIGHT ) ? ( TERMY - FULL_SCREEN_HEIGHT ) / 2 : 0;
int iOffsetX = TERMX > FULL_SCREEN_WIDTH ? ( TERMX - FULL_SCREEN_WIDTH ) / 2 : 0;
int iOffsetY = TERMY > FULL_SCREEN_HEIGHT ? ( TERMY - FULL_SCREEN_HEIGHT ) / 2 : 0;

catacurses::window w_snake = catacurses::newwin( FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH,
iOffsetY, iOffsetX );
Expand Down
4 changes: 2 additions & 2 deletions src/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ template <class element_type, class element_allocator_type = std::allocator<elem
struct ebco_pair2 : allocator_type { // empty-base-class optimisation
size_type capacity; // Total element capacity of all initialized groups
explicit ebco_pair2( const size_type number_of_elements ) noexcept: capacity(
number_of_elements ) {};
number_of_elements ) {}
} element_allocator_pair;

struct ebco_pair : group_allocator_type {
size_type capacity; // Total group capacity
explicit ebco_pair( const size_type number_of_groups ) noexcept: capacity( number_of_groups ) {};
explicit ebco_pair( const size_type number_of_groups ) noexcept: capacity( number_of_groups ) {}
} group_allocator_pair;

group_vector() noexcept:
Expand Down
2 changes: 1 addition & 1 deletion src/magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct fake_spell {
bool self;
fake_spell( const spell_id &sp_id, bool hit_self = false,
const cata::optional<int> &max_level = cata::nullopt ) : id( sp_id ),
max_level( max_level ), self( hit_self ) {};
max_level( max_level ), self( hit_self ) {}
};

class spell_type
Expand Down
Loading