Fix various copy instead of const ref reported by cppcheck (#5615)
authorLoïc Blot <nerzhul@users.noreply.github.com>
Wed, 19 Apr 2017 21:02:07 +0000 (23:02 +0200)
committerGitHub <noreply@github.com>
Wed, 19 Apr 2017 21:02:07 +0000 (23:02 +0200)
* Also remove InventoryList::peekItem unused function
* Fix some post increment to preincrement reported by cppcheck

13 files changed:
src/chat.cpp
src/chat.h
src/craftdef.h
src/environment.cpp
src/exceptions.h
src/game.cpp
src/httpfetch.cpp
src/inventory.cpp
src/inventory.h
src/script/common/c_content.cpp
src/script/common/c_content.h
src/script/lua_api/l_item.cpp
src/tool.h

index 46555b3dcc8f9da920efa2c44cade7e7ca971d6a..de7483e22360f45b9b236622f9a786f9f99c36ff 100644 (file)
@@ -386,7 +386,7 @@ s32 ChatBuffer::getBottomScrollPos() const
 
 
 
-ChatPrompt::ChatPrompt(std::wstring prompt, u32 history_limit):
+ChatPrompt::ChatPrompt(const std::wstring &prompt, u32 history_limit):
        m_prompt(prompt),
        m_line(L""),
        m_history(),
index 11061fd39e1c0e5619ec9d4b59c2961a7fe1260a..5de676a2e02e9debb0489126885d4fcbd4c652c5 100644 (file)
@@ -38,14 +38,14 @@ struct ChatLine
        // message text
        EnrichedString text;
 
-       ChatLine(std::wstring a_name, std::wstring a_text):
+       ChatLine(const std::wstring &a_name, const std::wstring &a_text):
                age(0.0),
                name(a_name),
                text(a_text)
        {
        }
 
-       ChatLine(EnrichedString a_name, EnrichedString a_text):
+       ChatLine(const EnrichedString &a_name, const EnrichedString &a_text):
                age(0.0),
                name(a_name),
                text(a_text)
@@ -148,7 +148,7 @@ private:
 class ChatPrompt
 {
 public:
-       ChatPrompt(std::wstring prompt, u32 history_limit);
+       ChatPrompt(const std::wstring &prompt, u32 history_limit);
        ~ChatPrompt();
 
        // Input character or string
index 695ee0c2c2fa1f7aebac0e516b1a4b32c3abacf3..bdd741f7c08293ed4014722d6ae194df351d249b 100644 (file)
@@ -97,7 +97,7 @@ struct CraftOutput
        CraftOutput():
                item(""), time(0)
        {}
-       CraftOutput(std::string item_, float time_):
+       CraftOutput(const std::string &item_, float time_):
                item(item_), time(time_)
        {}
        std::string dump() const;
@@ -124,7 +124,7 @@ struct CraftReplacements
        CraftReplacements():
                pairs()
        {}
-       CraftReplacements(std::vector<std::pair<std::string, std::string> > pairs_):
+       CraftReplacements(const std::vector<std::pair<std::string, std::string> > &pairs_):
                pairs(pairs_)
        {}
        std::string dump() const;
@@ -359,10 +359,13 @@ public:
        CraftDefinitionFuel():
                recipe(""), hash_inited(false), burntime()
        {}
-       CraftDefinitionFuel(std::string recipe_,
+       CraftDefinitionFuel(const std::string &recipe_,
                        float burntime_,
                        const CraftReplacements &replacements_):
-               recipe(recipe_), hash_inited(false), burntime(burntime_), replacements(replacements_)
+               recipe(recipe_),
+               hash_inited(false),
+               burntime(burntime_),
+               replacements(replacements_)
        {}
        virtual ~CraftDefinitionFuel(){}
 
index 9c2ea88963338771984d271dac6df37e928ec764..c1aeeec604c328c96e2e07aaf859fcff4dd570fa 100644 (file)
@@ -70,7 +70,7 @@ void Environment::setTimeOfDay(u32 time)
 {
        MutexAutoLock lock(this->m_time_lock);
        if (m_time_of_day > time)
-               m_day_count++;
+               ++m_day_count;
        m_time_of_day = time;
        m_time_of_day_f = (float)time / 24000.0;
 }
@@ -103,7 +103,7 @@ void Environment::stepTimeOfDay(float dtime)
                // Sync at overflow
                if (m_time_of_day + units >= 24000) {
                        sync_f = true;
-                       m_day_count++;
+                       ++m_day_count;
                }
                m_time_of_day = (m_time_of_day + units) % 24000;
                if (sync_f)
index 67a2d0df634079ef21b90a5d429e2654f7973796..1b39c6725a7921a86f2cba8ccfc5b5707f864037 100644 (file)
@@ -27,10 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 class BaseException : public std::exception
 {
 public:
-       BaseException(const std::string &s) throw()
-       {
-               m_s = s;
-       }
+       BaseException(const std::string &s) throw(): m_s(s) {}
        ~BaseException() throw() {}
        virtual const char * what() const throw()
        {
@@ -122,12 +119,12 @@ public:
 
 class ClientStateError : public BaseException {
 public:
-       ClientStateError(std::string s): BaseException(s) {}
+       ClientStateError(const std::string &s): BaseException(s) {}
 };
 
 class PrngException : public BaseException {
 public:
-       PrngException(std::string s): BaseException(s) {}
+       PrngException(const std::string &s): BaseException(s) {}
 };
 
 class ModError : public BaseException {
index bcf378e4efcacfecb3b3cb93850a8d90c642a87e..fbc0cf35f3869b0242ae4cdda798fa2f4443325c 100644 (file)
@@ -127,8 +127,8 @@ struct TextDestPlayerInventory : public TextDest
 
 struct LocalFormspecHandler : public TextDest
 {
-       LocalFormspecHandler(std::string formname):
-               m_client(0)
+       LocalFormspecHandler(const std::string &formname):
+               m_client(NULL)
        {
                m_formname = formname;
        }
@@ -139,11 +139,6 @@ struct LocalFormspecHandler : public TextDest
                m_formname = formname;
        }
 
-       void gotText(const std::wstring &message)
-       {
-               errorstream << "LocalFormspecHandler::gotText old style message received" << std::endl;
-       }
-
        void gotText(const StringMap &fields)
        {
                if (m_formname == "MT_PAUSE_MENU") {
index f64c9f717a62c36f9e976da22e98a363532684f0..21400355a7bfe8378076e1a078437cbe6608327a 100644 (file)
@@ -207,7 +207,7 @@ public:
 class HTTPFetchOngoing
 {
 public:
-       HTTPFetchOngoing(HTTPFetchRequest request, CurlHandlePool *pool);
+       HTTPFetchOngoing(const HTTPFetchRequest &request, CurlHandlePool *pool);
        ~HTTPFetchOngoing();
 
        CURLcode start(CURLM *multi);
@@ -228,7 +228,8 @@ private:
 };
 
 
-HTTPFetchOngoing::HTTPFetchOngoing(HTTPFetchRequest request_, CurlHandlePool *pool_):
+HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
+               CurlHandlePool *pool_):
        pool(pool_),
        curl(NULL),
        multi(NULL),
index 6d5b499168ac69c2f5d140659c3b37a1f8e7e1e3..8617f72634b6dc7eefc43bfe3d908d46bff4ab77 100644 (file)
@@ -46,12 +46,11 @@ static content_t content_translate_from_19_to_internal(content_t c_from)
 }
 
 ItemStack::ItemStack(const std::string &name_, u16 count_,
-               u16 wear_, IItemDefManager *itemdef)
+               u16 wear_, IItemDefManager *itemdef) :
+       name(itemdef->getAlias(name_)),
+       count(count_),
+       wear(wear_)
 {
-       name = itemdef->getAlias(name_);
-       count = count_;
-       wear = wear_;
-
        if (name.empty() || count == 0)
                clear();
        else if (itemdef->get(name).type == ITEM_TOOL)
@@ -370,14 +369,13 @@ ItemStack ItemStack::peekItem(u32 peekcount) const
        Inventory
 */
 
-InventoryList::InventoryList(std::string name, u32 size, IItemDefManager *itemdef)
+InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef):
+       m_name(name),
+       m_size(size),
+       m_width(0),
+       m_itemdef(itemdef)
 {
-       m_name = name;
-       m_size = size;
-       m_width = 0;
-       m_itemdef = itemdef;
        clearItems();
-       //m_dirty = false;
 }
 
 InventoryList::~InventoryList()
@@ -712,14 +710,6 @@ ItemStack InventoryList::takeItem(u32 i, u32 takecount)
        return taken;
 }
 
-ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const
-{
-       if(i >= m_items.size())
-               return ItemStack();
-
-       return m_items[i].peekItem(peekcount);
-}
-
 void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
 {
        // Take item from source list
index fe16397280cc8b86d653b2894bfa99d0219ae954..a9fef3b05ace1c216a91c42e1c7b6b7c37ddc304 100644 (file)
@@ -173,7 +173,7 @@ struct ItemStack
 class InventoryList
 {
 public:
-       InventoryList(std::string name, u32 size, IItemDefManager *itemdef);
+       InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
        ~InventoryList();
        void clearItems();
        void setSize(u32 newsize);
@@ -239,9 +239,6 @@ public:
        // Returns empty item if couldn't take any.
        ItemStack takeItem(u32 i, u32 takecount);
 
-       // Similar to takeItem, but keeps the slot intact.
-       ItemStack peekItem(u32 i, u32 peekcount) const;
-
        // Move an item to a different list (or a different stack in the same list)
        // count is the maximum number of items to move (0 for everything)
        // returns number of moved items
@@ -254,8 +251,8 @@ public:
 
 private:
        std::vector<ItemStack> m_items;
-       u32 m_size, m_width;
        std::string m_name;
+       u32 m_size, m_width;
        IItemDefManager *m_itemdef;
 };
 
index bcae874b99e09eb9ed6422ecb30368bfdb813873..8dfb851e6e7fe43adfd71f7e0b845e39e5303582 100644 (file)
@@ -43,15 +43,12 @@ struct EnumString es_TileAnimationType[] =
 };
 
 /******************************************************************************/
-ItemDefinition read_item_definition(lua_State* L,int index,
-               ItemDefinition default_def)
+void read_item_definition(lua_State* L, int index,
+               const ItemDefinition &default_def, ItemDefinition &def)
 {
-       if(index < 0)
+       if (index < 0)
                index = lua_gettop(L) + 1 + index;
 
-       // Read the item definition
-       ItemDefinition def = default_def;
-
        def.type = (ItemType)getenumfield(L, index, "type",
                        es_ItemType, ITEM_NONE);
        getstringfield(L, index, "name", def.name);
@@ -118,8 +115,6 @@ ItemDefinition read_item_definition(lua_State* L,int index,
        // "" = no prediction
        getstringfield(L, index, "node_placement_prediction",
                        def.node_placement_prediction);
-
-       return def;
 }
 
 /******************************************************************************/
@@ -873,7 +868,7 @@ void push_tool_capabilities(lua_State *L,
                lua_newtable(L);
                // For each groupcap
                for (ToolGCMap::const_iterator i = toolcap.groupcaps.begin();
-                       i != toolcap.groupcaps.end(); i++) {
+                       i != toolcap.groupcaps.end(); ++i) {
                        // Create groupcap table
                        lua_newtable(L);
                        const std::string &name = i->first;
@@ -881,7 +876,7 @@ void push_tool_capabilities(lua_State *L,
                        // Create subtable "times"
                        lua_newtable(L);
                        for (UNORDERED_MAP<int, float>::const_iterator
-                                       i = groupcap.times.begin(); i != groupcap.times.end(); i++) {
+                                       i = groupcap.times.begin(); i != groupcap.times.end(); ++i) {
                                lua_pushinteger(L, i->first);
                                lua_pushnumber(L, i->second);
                                lua_settable(L, -3);
@@ -900,7 +895,7 @@ void push_tool_capabilities(lua_State *L,
                lua_newtable(L);
                // For each damage group
                for (DamageGroup::const_iterator i = toolcap.damageGroups.begin();
-                       i != toolcap.damageGroups.end(); i++) {
+                       i != toolcap.damageGroups.end(); ++i) {
                        // Create damage group table
                        lua_pushinteger(L, i->second);
                        lua_setfield(L, -2, i->first.c_str());
@@ -939,7 +934,7 @@ void read_inventory_list(lua_State *L, int tableindex,
        InventoryList *invlist = inv->addList(name, listsize);
        int index = 0;
        for(std::vector<ItemStack>::const_iterator
-                       i = items.begin(); i != items.end(); i++){
+                       i = items.begin(); i != items.end(); ++i){
                if(forcesize != -1 && index == forcesize)
                        break;
                invlist->changeItem(index, *i);
index 949b136eb35ad3b7f6a441c14253ee9d98bde866..c701c0384248a1cdca9ea24026d41f41b0ee9aa6 100644 (file)
@@ -86,8 +86,8 @@ ToolCapabilities   read_tool_capabilities    (lua_State *L, int table);
 void               push_tool_capabilities    (lua_State *L,
                                               const ToolCapabilities &prop);
 
-ItemDefinition     read_item_definition      (lua_State *L, int index,
-                                              ItemDefinition default_def);
+void read_item_definition (lua_State *L, int index, const ItemDefinition &default_def,
+               ItemDefinition &def);
 void               read_object_properties    (lua_State *L, int index,
                                               ObjectProperties *prop,
                                               IItemDefManager *idef);
index 2d32093a73431a883ac7d5c825435ebab336bbc6..0e4fc4ef05509c38ee4fbf005c2847b41e5ba67f 100644 (file)
@@ -526,7 +526,7 @@ int ModApiItemMod::l_register_item_raw(lua_State *L)
        def.node_placement_prediction = "__default";
 
        // Read the item definition
-       def = read_item_definition(L, table, def);
+       read_item_definition(L, table, def, def);
 
        // Default to having client-side placement prediction for nodes
        // ("" in item definition sets it off)
index ebba5b7490fb78ee63cbda07810c3ade9d4a032f..f33152355c21e3cf5f9f3dc3bfa14c99195a1493 100644 (file)
@@ -63,8 +63,8 @@ struct ToolCapabilities
        ToolCapabilities(
                        float full_punch_interval_=1.4,
                        int max_drop_level_=1,
-                       ToolGCMap groupcaps_=ToolGCMap(),
-                       DamageGroup damageGroups_=DamageGroup()
+                       const ToolGCMap &groupcaps_ = ToolGCMap(),
+                       const DamageGroup &damageGroups_ = DamageGroup()
        ):
                full_punch_interval(full_punch_interval_),
                max_drop_level(max_drop_level_),
@@ -85,8 +85,8 @@ struct DigParams
        u16 wear;
        std::string main_group;
 
-       DigParams(bool a_diggable=false, float a_time=0, u16 a_wear=0,
-                       std::string a_main_group=""):
+       DigParams(bool a_diggable = false, float a_time = 0.0f, u16 a_wear = 0,
+                       const std::string &a_main_group = ""):
                diggable(a_diggable),
                time(a_time),
                wear(a_wear),