From 1b10fe3b7f3422f259135a66f207d76e9987ae26 Mon Sep 17 00:00:00 2001 From: Simon De Backer Date: Sun, 1 Nov 2020 18:44:14 +0100 Subject: [PATCH 1/2] Mod container-anti-pattern Finds when temporary containers are being created needlessly. These cases are usually easy to fix by using iterators, avoiding memory allocations. And mod to return on first true can we test if the Elements collection reload faster or not? --- sources/NameList/nameslist.cpp | 39 ++++++++++++++++------------------ 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/sources/NameList/nameslist.cpp b/sources/NameList/nameslist.cpp index bd2bac29d..73e5d8e4f 100644 --- a/sources/NameList/nameslist.cpp +++ b/sources/NameList/nameslist.cpp @@ -229,28 +229,25 @@ bool NamesList::operator==(const NamesList &nl) const } /** - Return the adequate name regarding the current system locale. - By order of preference, this function chooses: - - the name in the system language - - the English name - - the provided fallback name if non-empty - - the first language encountered in the list - - an empty string - @param fallback_name name to be returned when no adequate name has been found - @return The adequate name regarding the current system locale. -*/ + * @brief NamesList::name + * Return the adequate name regarding the current system locale. + * By order of preference, this function chooses: + * - the name in the system language + * - the English name + * - the provided fallback name if non-empty + * - the first language encountered in the list + * - an empty string + * @param fallback_name + * name to be returned when no adequate name has been found + * @return The adequate name regarding the current system locale. + */ QString NamesList::name(const QString &fallback_name) const { QString system_language = QETApp::langFromSetting(); - QString returned_name; - if (!hash_names[system_language].isEmpty()) { - returned_name = hash_names[system_language]; - } else if (!hash_names["en"].isEmpty()) { - returned_name = hash_names["en"]; - } else if (!fallback_name.isEmpty()) { - returned_name = fallback_name; - } else if (hash_names.count()) { - returned_name = hash_names.value(hash_names.keys().first()); - } - return(returned_name); + if (! hash_names[system_language].isEmpty()) + return (hash_names[system_language]); + if (! hash_names["en"].isEmpty()) return (hash_names["en"]); + if (! fallback_name.isEmpty()) return (fallback_name); + if (hash_names.count()) return (hash_names.begin().value()); + return (QString("")); } From 859bf7f645853ed37d8c51df23eef51863b47d22 Mon Sep 17 00:00:00 2001 From: Simon De Backer Date: Sun, 1 Nov 2020 19:10:30 +0100 Subject: [PATCH 2/2] Mod clazy checks range-loop you're using C++11 range-loops with non-const Qt containers (potential detach). adding missing & --- .../fileelementcollectionitem.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/sources/ElementsCollection/fileelementcollectionitem.cpp b/sources/ElementsCollection/fileelementcollectionitem.cpp index f3f7f63a0..7cf06651f 100644 --- a/sources/ElementsCollection/fileelementcollectionitem.cpp +++ b/sources/ElementsCollection/fileelementcollectionitem.cpp @@ -282,9 +282,8 @@ void FileElementCollectionItem::setUpData() ElementsLocation loc(collectionPath()); DiagramContext context = loc.elementInformations(); QStringList search_list; - for (QString key : context.keys()) { - search_list.append(context.value(key).toString()); - } + for (QString& key : context.keys()) + { search_list.append(context.value(key).toString()); } search_list.append(localName(loc)); setData(search_list.join(" ")); } @@ -352,8 +351,8 @@ void FileElementCollectionItem::populate(bool set_data, bool hide_element) QDir dir (fileSystemPath()); //Get all directory in this directory. - for(auto str : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, - QDir::Name)) + for (auto& str : + dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)) { FileElementCollectionItem *feci = new FileElementCollectionItem(); appendRow(feci); @@ -367,8 +366,8 @@ void FileElementCollectionItem::populate(bool set_data, bool hide_element) //Get all elmt file in this directory dir.setNameFilters(QStringList() << "*.elmt"); - for(auto str : dir.entryList(QDir::Files | QDir::NoDotAndDotDot, - QDir::Name)) + for (auto& str : + dir.entryList(QDir::Files | QDir::NoDotAndDotDot, QDir::Name)) { FileElementCollectionItem *feci = new FileElementCollectionItem(); appendRow(feci);