From aabd6d3160bcfcc36b15347ac1156d7ad04a22db Mon Sep 17 00:00:00 2001 From: Binrui Dong Date: Thu, 30 Mar 2023 20:55:18 +0800 Subject: [PATCH 1/5] Clean up duplicate header includes [readability-duplicate-include] --- src/animation.cpp | 2 -- src/cata_utility.cpp | 1 - src/character.h | 1 - src/computer.cpp | 1 - src/main.cpp | 1 - src/monster.cpp | 1 - src/options.cpp | 1 - src/options.h | 1 - src/output.h | 3 +-- src/overmap_ui.cpp | 1 - src/rng.h | 1 - src/sdltiles.cpp | 2 -- 12 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/animation.cpp b/src/animation.cpp index de68b0e718b1a..c2f5ffad0078c 100644 --- a/src/animation.cpp +++ b/src/animation.cpp @@ -26,8 +26,6 @@ #include "weather.h" #if defined(TILES) -#include - #include "cata_tiles.h" // all animation functions will be pushed out to a cata_tiles function in some manner #include "sdltiles.h" #endif diff --git a/src/cata_utility.cpp b/src/cata_utility.cpp index a378062bb758b..30d1f89b6da00 100644 --- a/src/cata_utility.cpp +++ b/src/cata_utility.cpp @@ -14,7 +14,6 @@ #include "cached_options.h" #include "catacharset.h" -#include "cata_utility.h" #include "debug.h" #include "enum_conversions.h" #include "filesystem.h" diff --git a/src/character.h b/src/character.h index bfe020da85935..e1740428c2486 100644 --- a/src/character.h +++ b/src/character.h @@ -2,7 +2,6 @@ #ifndef CATA_SRC_CHARACTER_H #define CATA_SRC_CHARACTER_H -#include #include #include #include diff --git a/src/computer.cpp b/src/computer.cpp index 20600209f940b..88725d6112071 100644 --- a/src/computer.cpp +++ b/src/computer.cpp @@ -4,7 +4,6 @@ #include #include -#include "computer.h" #include "debug.h" #include "enum_conversions.h" #include "json.h" diff --git a/src/main.cpp b/src/main.cpp index 7925dae288357..bfad415bbc051 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,6 @@ */ // IWYU pragma: no_include -#include #include #include #include diff --git a/src/monster.cpp b/src/monster.cpp index db87a54cc2b8a..cd3e7c4a5a1b3 100644 --- a/src/monster.cpp +++ b/src/monster.cpp @@ -66,7 +66,6 @@ #include "viewer.h" #include "weakpoint.h" #include "weather.h" -#include "harvest.h" static const anatomy_id anatomy_default_anatomy( "default_anatomy" ); diff --git a/src/options.cpp b/src/options.cpp index ffbc5654e2b01..792bdb0baf0d5 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -1,6 +1,5 @@ #include "options.h" -#include #include #include #include diff --git a/src/options.h b/src/options.h index e1678ba89394f..24036891b89e6 100644 --- a/src/options.h +++ b/src/options.h @@ -4,7 +4,6 @@ #include #include -#include #include #include #include diff --git a/src/output.h b/src/output.h index 804ffbe2b7c77..54dcc92a2d0ec 100644 --- a/src/output.h +++ b/src/output.h @@ -2,9 +2,8 @@ #ifndef CATA_SRC_OUTPUT_H #define CATA_SRC_OUTPUT_H -#include -#include #include +#include #include #include #include diff --git a/src/overmap_ui.cpp b/src/overmap_ui.cpp index 9046168b1ced2..dc84e1ed4693c 100644 --- a/src/overmap_ui.cpp +++ b/src/overmap_ui.cpp @@ -1,6 +1,5 @@ #include "overmap_ui.h" -#include #include #include #include diff --git a/src/rng.h b/src/rng.h index 6b6e07443ae51..59e9123b4f677 100644 --- a/src/rng.h +++ b/src/rng.h @@ -2,7 +2,6 @@ #ifndef CATA_SRC_RNG_H #define CATA_SRC_RNG_H -#include #include #include #include diff --git a/src/sdltiles.cpp b/src/sdltiles.cpp index f74e3070b9349..8b057fbe1004e 100644 --- a/src/sdltiles.cpp +++ b/src/sdltiles.cpp @@ -42,7 +42,6 @@ #include "catacharset.h" #include "color.h" #include "color_loader.h" -#include "cuboid_rectangle.h" #include "cursesport.h" #include "debug.h" #include "filesystem.h" @@ -65,7 +64,6 @@ #include "overmap_ui.h" #include "overmapbuffer.h" #include "path_info.h" -#include "point.h" #include "sdl_geometry.h" #include "sdl_wrappers.h" #include "sdl_font.h" From d03954af2d7f27dbd2c40cf72be5ccd57f23e6e0 Mon Sep 17 00:00:00 2001 From: Binrui Dong Date: Thu, 30 Mar 2023 20:55:41 +0800 Subject: [PATCH 2/5] Remove unused decl [misc-unused-using-decls] --- src/input.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index fd667b654313d..88bb4c253becd 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -42,8 +42,6 @@ #include "translations.h" #include "ui_manager.h" -using std::min; // from - static const std::string default_context_id( "default" ); template From b6607cc71f2a935925363037972caa05a38e1d8d Mon Sep 17 00:00:00 2001 From: Binrui Dong Date: Thu, 30 Mar 2023 21:01:33 +0800 Subject: [PATCH 3/5] Migrate &vec[0] to vec.data() [readability-container-data-pointer] --- src/activity_handlers.cpp | 2 +- src/bionics_ui.cpp | 2 +- src/cata_utility.cpp | 2 +- src/catacharset.cpp | 8 ++++---- src/item.cpp | 10 +++++----- src/json.cpp | 4 ++-- src/mapgen_functions.cpp | 2 ++ src/memorial_logger.cpp | 2 +- src/output.cpp | 6 +++--- src/translation_plural_evaluator.cpp | 2 +- 10 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 6c99585ed7444..5ede2e760bdf8 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -2179,7 +2179,7 @@ void repair_item_finish( player_activity *act, Character *you, bool no_menu ) item_location *ploc = nullptr; if( !act->targets.empty() ) { - ploc = &act->targets[0]; + ploc = act->targets.data(); } item &main_tool = !w_hack.init( *act ) ? diff --git a/src/bionics_ui.cpp b/src/bionics_ui.cpp index b622c30cb86fb..2fee908f25567 100644 --- a/src/bionics_ui.cpp +++ b/src/bionics_ui.cpp @@ -882,7 +882,7 @@ void avatar::power_bionics() const bionic_data &bio_data = bio_id.obj(); if( menu_mode == ACTIVATING ) { if( bio_data.activated ) { - int b = tmp - &( *my_bionics )[0]; + int b = tmp - my_bionics->data(); bionic &bio = ( *my_bionics )[b]; hide = true; ui.mark_resize(); diff --git a/src/cata_utility.cpp b/src/cata_utility.cpp index 30d1f89b6da00..fead8bf4c591c 100644 --- a/src/cata_utility.cpp +++ b/src/cata_utility.cpp @@ -510,7 +510,7 @@ std::optional read_whole_file( const fs::path &path ) fin.seekg( 0 ); outstring.resize( size ); - fin.read( &outstring[0], size ); + fin.read( outstring.data(), size ); } if( fin.bad() ) { throw std::runtime_error( "reading file failed" ); diff --git a/src/catacharset.cpp b/src/catacharset.cpp index 12f79bca0e6e7..179d9b1ae92f8 100644 --- a/src/catacharset.cpp +++ b/src/catacharset.cpp @@ -347,13 +347,13 @@ std::wstring utf8_to_wstr( const std::string &str ) #if defined(_WIN32) int sz = MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, nullptr, 0 ) + 1; std::wstring wstr( sz, '\0' ); - MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, &wstr[0], sz ); + MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, wstr.data(), sz ); strip_trailing_nulls( wstr ); return wstr; #else std::size_t sz = std::mbstowcs( nullptr, str.c_str(), 0 ) + 1; std::wstring wstr( sz, '\0' ); - std::mbstowcs( &wstr[0], str.c_str(), sz ); + std::mbstowcs( wstr.data(), str.c_str(), sz ); strip_trailing_nulls( wstr ); return wstr; #endif @@ -364,13 +364,13 @@ std::string wstr_to_utf8( const std::wstring &wstr ) #if defined(_WIN32) int sz = WideCharToMultiByte( CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr ); std::string str( sz, '\0' ); - WideCharToMultiByte( CP_UTF8, 0, wstr.c_str(), -1, &str[0], sz, nullptr, nullptr ); + WideCharToMultiByte( CP_UTF8, 0, wstr.c_str(), -1, str.data(), sz, nullptr, nullptr ); strip_trailing_nulls( str ); return str; #else std::size_t sz = std::wcstombs( nullptr, wstr.c_str(), 0 ) + 1; std::string str( sz, '\0' ); - std::wcstombs( &str[0], wstr.c_str(), sz ); + std::wcstombs( str.data(), wstr.c_str(), sz ); strip_trailing_nulls( str ); return str; #endif diff --git a/src/item.cpp b/src/item.cpp index dc7a47ddd5b7e..d04c60fe0df4c 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -1598,25 +1598,25 @@ double item::get_var( const std::string &name, const double default_value ) cons const std::string &val = it->second; char *end; errno = 0; - double result = strtod( &val[0], &end ); + double result = strtod( val.data(), &end ); if( errno != 0 ) { debugmsg( "Error parsing floating point value from %s in item::get_var: %s", val, strerror( errno ) ); return default_value; } - if( end != &val[0] + val.size() ) { + if( end != val.data() + val.size() ) { if( *end == ',' ) { // likely legacy format with localized ',' for fraction separator instead of '.' std::string converted_val = val; - converted_val[end - &val[0]] = '.'; + converted_val[end - val.data()] = '.'; errno = 0; - double result = strtod( &converted_val[0], &end ); + double result = strtod( converted_val.data(), &end ); if( errno != 0 ) { debugmsg( "Error parsing floating point value from %s in item::get_var: %s", val, strerror( errno ) ); return default_value; } - if( end != &converted_val[0] + converted_val.size() ) { + if( end != converted_val.data() + converted_val.size() ) { debugmsg( "Stray characters at end of floating point value %s in item::get_var", val ); } return result; diff --git a/src/json.cpp b/src/json.cpp index 3ab2267ebbea8..5b5a580eee094 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -1924,7 +1924,7 @@ void TextJsonIn::error( int offset, const std::string &message ) rewind( 3, 240 ); size_t startpos = tell(); std::string buffer( pos - startpos, '\0' ); - stream->read( &buffer[0], pos - startpos ); + stream->read( buffer.data(), pos - startpos ); auto it = buffer.begin(); for( ; it < buffer.end() && ( *it == '\r' || *it == '\n' ); ++it ) { // skip starting newlines @@ -2073,7 +2073,7 @@ std::string TextJsonIn::substr( size_t pos, size_t len ) } ret.resize( len ); stream->seekg( pos ); - stream->read( &ret[0], len ); + stream->read( ret.data(), len ); return ret; } diff --git a/src/mapgen_functions.cpp b/src/mapgen_functions.cpp index 7d39a6c6ebc97..96925081f09a1 100644 --- a/src/mapgen_functions.cpp +++ b/src/mapgen_functions.cpp @@ -2185,8 +2185,10 @@ void mapgen_forest( mapgendata &dat ) if( p.x < SEEX ) { if( p.y < SEEY ) { unify_continuous_border( adjacent_biomes[3], adjacent_biomes[7], adjacent_biomes[0], + // NOLINTNEXTLINE(readability-container-data-pointer) &cardinal_four_weights[3], &cardinal_four_weights[0], self_weight ); } else { + // NOLINTNEXTLINE(readability-container-data-pointer) unify_continuous_border( adjacent_biomes[0], adjacent_biomes[4], adjacent_biomes[1], &cardinal_four_weights[0], &cardinal_four_weights[1], self_weight ); } diff --git a/src/memorial_logger.cpp b/src/memorial_logger.cpp index e9fbee8182273..6296704891136 100644 --- a/src/memorial_logger.cpp +++ b/src/memorial_logger.cpp @@ -160,7 +160,7 @@ void memorial_logger::load( std::istream &fin ) size_t size = fin.tellg(); fin.seekg( 0, std::ios_base::beg ); memorial_data.resize( size ); - fin.read( &memorial_data[0], size ); + fin.read( memorial_data.data(), size ); JsonValue jsin = json_loader::from_string( memorial_data ); if( !jsin.read( log ) ) { debugmsg( "Error reading JSON memorial log" ); diff --git a/src/output.cpp b/src/output.cpp index 1f7673ec0ecc6..068b5409be808 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -1346,7 +1346,7 @@ std::string to_upper_case( const std::string &s ) { const auto &f = std::use_facet>( std::locale() ); std::wstring wstr = utf8_to_wstr( s ); - f.toupper( &wstr[0], &wstr[0] + wstr.size() ); + f.toupper( wstr.data(), wstr.data() + wstr.size() ); return wstr_to_utf8( wstr ); } @@ -2415,7 +2415,7 @@ std::string cata::string_formatter::raw_string_format( const char *format, ... ) va_list args_copy; va_copy( args_copy, args ); - const int result = vsnprintf( &buffer[0], buffer_size, format, args_copy ); + const int result = vsnprintf( buffer.data(), buffer_size, format, args_copy ); va_end( args_copy ); // No error, and the buffer is big enough; we're done. @@ -2435,7 +2435,7 @@ std::string cata::string_formatter::raw_string_format( const char *format, ... ) } va_end( args ); - return std::string( &buffer[0] ); + return std::string( buffer.data() ); #endif } #endif diff --git a/src/translation_plural_evaluator.cpp b/src/translation_plural_evaluator.cpp index 3c5aebbba58fd..d3fe690ee8014 100644 --- a/src/translation_plural_evaluator.cpp +++ b/src/translation_plural_evaluator.cpp @@ -213,7 +213,7 @@ Token TranslationPluralRulesEvaluator::GetNextToken( const char *&p ) std::vector TranslationPluralRulesEvaluator::Lexer( const std::string &expr ) { std::vector tokens; - const char *p = &expr[0]; + const char *p = expr.data(); ExprToken token; while( ( token = GetNextToken( p ) ).type != ExprTokenType::Termination ) { tokens.emplace_back( token ); From 97f353e38f1c6bb0d2b58e32a696ed79f0a44ab6 Mon Sep 17 00:00:00 2001 From: Binrui Dong Date: Thu, 30 Mar 2023 21:03:13 +0800 Subject: [PATCH 4/5] Access static member through type name [readability-static-accessed-through-instance] --- src/savegame_json.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/savegame_json.cpp b/src/savegame_json.cpp index 920d438658207..f00aae8c4816b 100644 --- a/src/savegame_json.cpp +++ b/src/savegame_json.cpp @@ -2961,8 +2961,8 @@ void item::io( Archive &archive ) archive.io( "item_vars", item_vars, io::empty_default_tag() ); // TODO: change default to empty string archive.io( "name", corpse_name, std::string() ); - archive.io( "owner", owner, owner.NULL_ID() ); - archive.io( "old_owner", old_owner, old_owner.NULL_ID() ); + archive.io( "owner", owner, faction_id::NULL_ID() ); + archive.io( "old_owner", old_owner, faction_id::NULL_ID() ); archive.io( "invlet", invlet, '\0' ); archive.io( "damaged", damage_, 0 ); archive.io( "degradation", degradation_, 0 ); From ab3f43799d331e5df034bab4901a1b7a08c4c55e Mon Sep 17 00:00:00 2001 From: Binrui Dong Date: Thu, 30 Mar 2023 21:06:56 +0800 Subject: [PATCH 5/5] Avoid unnecessary copy construction [performance-unnecessary-copy-initialization] --- src/advanced_inv_pane.cpp | 2 +- src/construction.cpp | 2 +- src/consumption.cpp | 2 +- src/output.cpp | 2 +- src/sdltiles.cpp | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/advanced_inv_pane.cpp b/src/advanced_inv_pane.cpp index 4d8ab52ff9495..ed49465f8603f 100644 --- a/src/advanced_inv_pane.cpp +++ b/src/advanced_inv_pane.cpp @@ -60,7 +60,7 @@ void advanced_inventory_pane::load_settings( int saved_area_idx, !is_re_enter ) ? saved_area_idx : save_state->area_idx; const aim_location location = static_cast( i_location ); - const advanced_inv_area square = squares[location]; + const advanced_inv_area &square = squares[location]; // determine the square's vehicle/map item presence bool has_veh_items = square.can_store_in_vehicle() ? !square.veh->get_items( square.vstor ).empty() : false; diff --git a/src/construction.cpp b/src/construction.cpp index 8c9b4f7f5e186..8858987484241 100644 --- a/src/construction.cpp +++ b/src/construction.cpp @@ -1492,7 +1492,7 @@ void construct::done_appliance( const tripoint_bub_ms &p, Character & ) return; } - const item base = components.front(); + const item &base = components.front(); const vpart_id &vpart = vpart_appliance_from_item( base.typeId() ); // TODO: fix point types diff --git a/src/consumption.cpp b/src/consumption.cpp index 538a166cfa74c..6d8ef66694101 100644 --- a/src/consumption.cpp +++ b/src/consumption.cpp @@ -320,7 +320,7 @@ std::pair Character::compute_nutrient_range( our_extra_flags.insert( flag_COOKED ); } - const requirement_data requirements = rec.simple_requirements(); + const requirement_data &requirements = rec.simple_requirements(); const requirement_data::alter_item_comp_vector &component_requirements = requirements.get_components(); diff --git a/src/output.cpp b/src/output.cpp index 068b5409be808..6de513ccc75ea 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -295,7 +295,7 @@ std::string trim_by_length( const std::string &text, int width ) const std::vector color_segments = split_by_color( text ); for( size_t i = 0; i < color_segments.size() ; ++i ) { - std::string seg = color_segments[i]; + const std::string &seg = color_segments[i]; if( seg.empty() ) { // TODO: Check is required right now because, for a fully-color-tagged string, split_by_color // returns an empty string first diff --git a/src/sdltiles.cpp b/src/sdltiles.cpp index 8b057fbe1004e..63da9a08cde3f 100644 --- a/src/sdltiles.cpp +++ b/src/sdltiles.cpp @@ -769,7 +769,7 @@ std::string cata_tiles::get_omt_id_rotation_and_subtile( oter_id ot_id = oter_at( omp ); const oter_t &ot = *ot_id; oter_type_id ot_type_id = ot.get_type_id(); - oter_type_t ot_type = *ot_type_id; + const oter_type_t &ot_type = *ot_type_id; if( ot_type.has_connections() ) { // This would be for connected terrain