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 (2018-11-24) #26832

Merged
merged 9 commits into from
Nov 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 51 additions & 58 deletions msvc-full-features/stdafx.h
Original file line number Diff line number Diff line change
@@ -1,84 +1,77 @@
#pragma once

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cinttypes>
#include <csignal>
#include <cstdio>
#include <cwchar>
#include <ccomplex>
//#include <ciso646>
//#include <cstdalign>
#include <cstdlib>
#include <cwctype>
#include <cctype>
#include <climits>
#include <cstdarg>
#include <cstring>
#include <cerrno>
#include <clocale>
#include <cstdbool>
#include <ctgmath>
#include <cfenv>
#include <cmath>
#include <cstddef>
#include <ctime>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <clocale>
#include <cmath>
#include <codecvt>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cuchar>
#include <algorithm>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <list>
#include <regex>
#include <tuple>
#include <array>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <scoped_allocator>
#include <type_traits>
//#include <atomic>
//#include <future>
#include <map>
#include <set>
#include <typeindex>
#include <bitset>
#include <initializer_list>
#include <memory>
#include <sstream>
#include <typeinfo>
#include <chrono>
#include <iomanip>
#include <mutex>
#include <stack>
#include <unordered_map>
#include <codecvt>
#include <ios>
#include <new>
#include <stdexcept>
#include <unordered_set>
#include <complex>
#include <iosfwd>
#include <numeric>
#include <streambuf>
#include <utility>
//#include <condition_variable>
#include <iostream>
#include <ostream>
#include <string>
//#include <valarray>
#include <deque>
#include <istream>
#include <queue>
#include <strstream>
#include <vector>
#include <exception>
#include <iterator>
#include <random>
#include <system_error>
#include <forward_list>
#include <limits>
#include <ratio>
//#include <thread>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <strstream>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include "../src/platform_win.h"

Expand Down
4 changes: 2 additions & 2 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,15 +1704,15 @@ void activity_handlers::make_zlave_finish( player_activity *act, player *p )
act->set_to_null();
auto items = g->m.i_at( p->pos() );
std::string corpse_name = act->str_values[0];
item *body = NULL;
item *body = nullptr;

for( auto it = items.begin(); it != items.end(); ++it ) {
if( it->display_name() == corpse_name ) {
body = &*it;
}
}

if( body == NULL ) {
if( body == nullptr ) {
add_msg( m_info, _( "There's no corpse to make into a zombie slave!" ) );
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/advanced_inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "vpart_reference.h"

#ifdef __ANDROID__
#include "SDL_keyboard.h"
#include <SDL_keyboard.h>
#endif

#include <algorithm>
Expand Down
2 changes: 1 addition & 1 deletion src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ bool player::activate_bionic( int b, bool eff_only )
item water = item( "water_clean", 0 );
water.reset_temp_check();
int humidity = weatherPoint.humidity;
int water_charges = ( humidity * 3.0 ) / 100.0 + 0.5;
int water_charges = lround( humidity * 3.0 / 100.0 );
// At 50% relative humidity or more, the player will draw 2 units of water
// At 16% relative humidity or less, the player will draw 0 units of water
water.charges = water_charges;
Expand Down
4 changes: 2 additions & 2 deletions src/bionics_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ void player::power_bionics()
{
std::vector <bionic *> passive = filtered_bionics( *my_bionics, TAB_PASSIVE );
std::vector <bionic *> active = filtered_bionics( *my_bionics, TAB_ACTIVE );
bionic *bio_last = NULL;
bionic *bio_last = nullptr;
bionic_tab_mode tab_mode = TAB_ACTIVE;

//added title_tab_height for the tabbed bionic display
Expand Down Expand Up @@ -469,7 +469,7 @@ void player::power_bionics()

const std::string action = ctxt.handle_input();
const long ch = ctxt.get_raw_input().get_first_input();
bionic *tmp = NULL;
bionic *tmp = nullptr;
bool confirmCheck = false;

if( action == "DOWN" ) {
Expand Down
6 changes: 3 additions & 3 deletions src/cata_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ double convert_volume( int volume, int *out_scale )
ret *= 0.00105669;
scale = 2;
}
if( out_scale != NULL ) {
if( out_scale != nullptr ) {
*out_scale = scale;
}
return ret;
Expand All @@ -269,7 +269,7 @@ double clamp_to_width( double value, int width, int &scale )

double clamp_to_width( double value, int width, int &scale, bool *out_truncated )
{
if( out_truncated != NULL ) {
if( out_truncated != nullptr ) {
*out_truncated = false;
}
if( value >= std::pow( 10.0, width ) ) {
Expand All @@ -278,7 +278,7 @@ double clamp_to_width( double value, int width, int &scale, bool *out_truncated
// flag as truncated
value = std::pow( 10.0, width ) - 1.0;
scale = 0;
if( out_truncated != NULL ) {
if( out_truncated != nullptr ) {
*out_truncated = true;
}
} else if( scale > 0 ) {
Expand Down
6 changes: 3 additions & 3 deletions src/catalua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
#include "ui.h"

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

#include <type_traits>
Expand Down
2 changes: 1 addition & 1 deletion src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ std::string Character::enumerate_unmet_requirements( const item &it, const item
check_req( _( "perception" ), get_per(), it.type->min_per );

for( const auto &elem : it.type->min_skills ) {
check_req( context.contextualize_skill( elem.first )->name().c_str(),
check_req( context.contextualize_skill( elem.first )->name(),
get_skill_level( elem.first, context ),
elem.second );
}
Expand Down
2 changes: 1 addition & 1 deletion src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ zone_type_id zone_manager::get_near_zone_type_for_item( const item &it,
}

if( cat.id() == "food" ) {
const bool preserves = it.is_food_container() && it.type->container->preserves ? 1 : 0;
const bool preserves = it.is_food_container() && it.type->container->preserves;
const auto &it_food = it.is_food_container() ? it.contents.front() : it;

if( it_food.is_food() ) { // skip food without comestible, like MREs
Expand Down
2 changes: 1 addition & 1 deletion src/computer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ of pureed bone & LSD." ) );
case COMPACT_DOWNLOAD_SOFTWARE:
if( item *const usb = pick_usb() ) {
mission *miss = mission::find( mission_id );
if( miss == NULL ) {
if( miss == nullptr ) {
debugmsg( _( "Computer couldn't find its mission!" ) );
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/crafting_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ const recipe *select_crafting_recipe( int &batch_size )
bool show_hidden = false;
int batch_line = 0;
int display_mode = 0;
const recipe *chosen = NULL;
const recipe *chosen = nullptr;
std::vector<iteminfo> thisItem;
std::vector<iteminfo> dummy;

Expand Down
2 changes: 1 addition & 1 deletion src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Creature::Creature()
{
moves = 0;
pain = 0;
killer = NULL;
killer = nullptr;
speed_base = 100;
underwater = false;

Expand Down
2 changes: 1 addition & 1 deletion src/dependency_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ dependency_node *dependency_tree::get_node( mod_id key )
if( iter != master_node_map.end() ) {
return &iter->second;
}
return NULL;
return nullptr;
}

// makes sure to set up Cycle errors properly!
Expand Down
14 changes: 7 additions & 7 deletions src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ void editmap_hilight::draw( editmap &hm, bool update )
if( t_field.fieldCount() > 0 ) {
field_id t_ftype = t_field.fieldSymbol();
const field_entry *t_fld = t_field.findField( t_ftype );
if( t_fld != NULL ) {
if( t_fld != nullptr ) {
t_col = t_fld->color();
t_sym = t_fld->symbol();
}
Expand Down Expand Up @@ -537,7 +537,7 @@ void editmap::update_view( bool update_info )
if( t_field.fieldCount() > 0 ) {
field_id t_ftype = t_field.fieldSymbol();
const field_entry *t_fld = t_field.findField( t_ftype );
if( t_fld != NULL ) {
if( t_fld != nullptr ) {
t_col = t_fld->color();
t_sym = t_fld->symbol();
}
Expand Down Expand Up @@ -1043,14 +1043,14 @@ void editmap::update_fmenu_entry( uilist &fmenu, field &field, const field_id id
int fdens = 1;
const field_t &ftype = fieldlist[idx];
field_entry *fld = field.findField( idx );
if( fld != NULL ) {
if( fld != nullptr ) {
fdens = fld->getFieldDensity();
}
fmenu.entries[idx].txt = ftype.name( fdens - 1 );
if( fld != NULL ) {
if( fld != nullptr ) {
fmenu.entries[idx].txt += " " + std::string( fdens, '*' );
}
fmenu.entries[idx].text_color = ( fld != NULL ? c_cyan : fmenu.text_color );
fmenu.entries[idx].text_color = ( fld != nullptr ? c_cyan : fmenu.text_color );
fmenu.entries[idx].extratxt.color = ftype.color[fdens - 1];
}

Expand Down Expand Up @@ -1096,7 +1096,7 @@ int editmap::edit_fld()
int fdens = 0;
const field_id idx = static_cast<field_id>( fmenu.selected );
field_entry *fld = cur_field->findField( idx );
if( fld != NULL ) {
if( fld != nullptr ) {
fdens = fld->getFieldDensity();
}
int fsel_dens = fdens;
Expand Down Expand Up @@ -1133,7 +1133,7 @@ int editmap::edit_fld()
field &t_field = g->m.get_field( elem );
field_entry *t_fld = t_field.findField( fid );
int t_dens = 0;
if( t_fld != NULL ) {
if( t_fld != nullptr ) {
t_dens = t_fld->getFieldDensity();
}
if( fsel_dens != 0 ) {
Expand Down
2 changes: 1 addition & 1 deletion src/effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ void effect_type::register_ma_buff_effect( const effect_type &eff )
void effect::serialize( JsonOut &json ) const
{
json.start_object();
json.member( "eff_type", eff_type != NULL ? eff_type->id.str() : "" );
json.member( "eff_type", eff_type != nullptr ? eff_type->id.str() : "" );
json.member( "duration", duration );
json.member( "bp", static_cast<int>( bp ) );
json.member( "permanent", permanent );
Expand Down
4 changes: 2 additions & 2 deletions src/faction_camp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,7 @@ void talk_function::camp_fortifications_return( npc &p )
bool build_dir_NS = ( comp->companion_mission_points[0].y != comp->companion_mission_points[1].y );
//Ensure all tiles are generated before putting fences/trenches down...
for( auto pt : comp->companion_mission_points ) {
if( MAPBUFFER.lookup_submap( om_to_sm_copy( pt ) ) == NULL ) {
if( MAPBUFFER.lookup_submap( om_to_sm_copy( pt ) ) == nullptr ) {
oter_id &omt_test = overmap_buffer.ter( pt );
std::string om_i = omt_test.id().c_str();
//The thick forests will gen harsh boundries since it won't recognize these tiles when they become fortifications
Expand Down Expand Up @@ -2211,7 +2211,7 @@ void talk_function::draw_camp_tabs( const catacurses::window &win, const camp_ta
const int tab_step = 3;
int tab_space = 1;
int tab_x = 0;
for( auto t : tabs ) {
for( auto &t : tabs ) {
bool tab_empty = entries[tab_x + 1].empty();
draw_subtab( win, tab_space, t, tab_x == cur_tab, false, tab_empty );
tab_space += tab_step + utf8_width( t );
Expand Down
6 changes: 3 additions & 3 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ class game
std::list<std::string> get_npc_kill();

/** Performs a random short-distance teleport on the given player, granting teleglow if needed. */
void teleport( player *p = NULL, bool add_teleglow = true );
void teleport( player *p = nullptr, bool add_teleglow = true );
/** Handles swimming by the player. Called by plmove(). */
void plswim( const tripoint &p );
/** Picks and spawns a random fish from the remaining fish list when a fish is caught. */
Expand Down Expand Up @@ -680,7 +680,7 @@ class game
// and sets it (view set_driving_view_offset), if
// the options for this feature is deactivated or if veh is NULL,
// the function set the driving offset to (0,0)
void calc_driving_offset( vehicle *veh = NULL );
void calc_driving_offset( vehicle *veh = nullptr );

/**
* @name Liquid handling
Expand Down Expand Up @@ -762,7 +762,7 @@ class game
* Basically `false` indicates the user does not *want* to handle the liquid, `true`
* indicates they want to handle it.
*/
bool handle_liquid( item &liquid, item *source = NULL, int radius = 0,
bool handle_liquid( item &liquid, item *source = nullptr, int radius = 0,
const tripoint *source_pos = nullptr,
const vehicle *source_veh = nullptr, const int part_num = -1,
const monster *source_mon = nullptr );
Expand Down
Loading