Skip to content

Commit d16e851

Browse files
committed
refactor: fix most compiler and clazy warnings
These changes also provide a slight performance boost (about 6%). Signed-off-by: ComixHe <heyuming@deepin.org>
1 parent 2a478ed commit d16e851

14 files changed

Lines changed: 151 additions & 146 deletions

apps/app-identifier/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int main(int argc, char *argv[])
6060
return;
6161
}
6262

63-
auto appID = reply.arguments().first().toString();
63+
auto appID = reply.arguments().constFirst().toString();
6464
if (!appID.isEmpty()) {
6565
qInfo() << "The capacity of process" << pid << "is:" << appID;
6666
return;

apps/app-launch-helper/src/main.cpp

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,26 @@
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

5-
#include <numeric>
6-
#include <unordered_map>
7-
#include <vector>
8-
#include <deque>
9-
#include <memory>
10-
#include <iostream>
11-
#include <algorithm>
12-
#include <cstdlib>
13-
#include <map>
14-
#include <list>
15-
#include <thread>
16-
#include <filesystem>
17-
#include <cctype>
185
#include "constant.h"
196
#include "types.h"
207
#include "variantValue.h"
8+
#include <algorithm>
9+
#include <cstdlib>
10+
#include <deque>
11+
#include <filesystem>
12+
#include <memory>
13+
#include <unordered_map>
14+
#include <vector>
2115

2216
namespace {
2317

24-
ExitCode fromString(const std::string &str)
18+
ExitCode fromString(std::string_view str)
2519
{
2620
if (str == "done") {
2721
return ExitCode::Done;
2822
}
2923

30-
if (str == "canceled" or str == "timeout" or str == "failed" or str == "dependency" or str == "skipped") {
24+
if (str == "canceled" || str == "timeout" || str == "failed" || str == "dependency" || str == "skipped") {
3125
return ExitCode::SystemdError;
3226
}
3327

@@ -44,10 +38,10 @@ ExitCode fromString(const std::string &str)
4438

4539
ExitCode fromString(const char *str)
4640
{
47-
if (!str) {
41+
if (str == nullptr) {
4842
return ExitCode::Waiting;
4943
}
50-
std::string tmp{str};
44+
const std::string tmp{str};
5145
return fromString(tmp);
5246
}
5347

@@ -62,7 +56,7 @@ ExitCode fromString(const char *str)
6256

6357
int processExecStart(msg_ptr &msg, const std::deque<std::string_view> &execArgs)
6458
{
65-
int ret;
59+
int ret{0};
6660

6761
if (ret = sd_bus_message_open_container(msg, SD_BUS_TYPE_STRUCT, "sv"); ret < 0) {
6862
sd_journal_perror("open struct of ExecStart failed.");
@@ -99,7 +93,7 @@ int processExecStart(msg_ptr &msg, const std::deque<std::string_view> &execArgs)
9993
return ret;
10094
}
10195

102-
for (auto execArg : execArgs) {
96+
for (const auto &execArg : execArgs) {
10397
if (ret = sd_bus_message_append(msg, "s", execArg.data()); ret < 0) {
10498
sd_journal_perror("append args of execStart failed.");
10599
return ret;
@@ -147,16 +141,16 @@ DBusValueType getPropType(std::string_view key)
147141
{"WorkingDirectory", DBusValueType::String},
148142
{"ExecSearchPath", DBusValueType::ArrayOfString}};
149143

150-
if (auto it = map.find(key); it != map.cend()) {
144+
if (const auto it = map.find(key); it != map.cend()) {
151145
return it->second;
152146
}
153147

154148
return DBusValueType::String; // fallback to string
155149
}
156150

157-
int appendPropValue(msg_ptr &msg, DBusValueType type, const std::list<std::string_view> &value)
151+
int appendPropValue(msg_ptr &msg, DBusValueType type, const std::vector<std::string> &value)
158152
{
159-
int ret;
153+
int ret{0};
160154

161155
auto handler = creatValueHandler(msg, type);
162156
if (handler == nullptr) {
@@ -184,47 +178,42 @@ int appendPropValue(msg_ptr &msg, DBusValueType type, const std::list<std::strin
184178
return 0;
185179
}
186180

187-
int processKVPair(msg_ptr &msg, const std::map<std::string_view, std::list<std::string_view>> &props)
181+
int processKVPair(msg_ptr &msg, std::unordered_map<std::string_view, std::vector<std::string>> &props)
188182
{
189-
int ret;
183+
int ret{0};
190184
if (!props.empty()) {
191-
for (auto [key, value] : props) {
192-
const std::list<std::string_view> *valuePtr = &value;
193-
std::list<std::string_view> normalizedValue;
194-
std::vector<std::string> normalizedStorage;
195-
185+
for (auto &[key, value] : props) {
196186
if (key == "ExecSearchPath") {
187+
std::vector<std::string> normalizedValue;
197188
for (const auto &v : value) {
198-
std::filesystem::path p{std::string{v}};
189+
const std::filesystem::path p{std::string{v}};
199190
if (!p.is_absolute()) {
200191
sd_journal_print(LOG_INFO, "ExecSearchPath ignoring relative path: %s", std::string{v}.c_str());
201192
continue;
202193
}
203-
normalizedStorage.emplace_back(p.lexically_normal().string());
194+
normalizedValue.emplace_back(p.lexically_normal().string());
204195
}
205196

206-
normalizedValue.assign(normalizedStorage.begin(), normalizedStorage.end());
207-
208197
if (normalizedValue.empty()) {
209198
sd_journal_print(LOG_WARNING, "ExecSearchPath normalized to empty, skipping property");
210199
continue;
211200
}
212201

213-
valuePtr = &normalizedValue;
202+
value = std::move(normalizedValue);
214203
}
215204

216-
std::string keyStr{key};
205+
const std::string keyStr{key};
217206
if (ret = sd_bus_message_open_container(msg, SD_BUS_TYPE_STRUCT, "sv"); ret < 0) {
218207
sd_journal_perror("open struct of properties failed.");
219208
return ret;
220209
}
221210

222-
if (ret = sd_bus_message_append(msg, "s", keyStr.data()); ret < 0) {
211+
if (ret = sd_bus_message_append(msg, "s", keyStr.c_str()); ret < 0) {
223212
sd_journal_perror("append key of property failed.");
224213
return ret;
225214
}
226215

227-
if (ret = appendPropValue(msg, getPropType(key), *valuePtr); ret < 0) {
216+
if (ret = appendPropValue(msg, getPropType(key), value); ret < 0) {
228217
sd_journal_perror("append value of property failed.");
229218
return ret;
230219
}
@@ -241,7 +230,7 @@ int processKVPair(msg_ptr &msg, const std::map<std::string_view, std::list<std::
241230
std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
242231
{
243232
std::string serviceName{"internalError"};
244-
std::map<std::string_view, std::list<std::string_view>> props;
233+
std::unordered_map<std::string_view, std::vector<std::string>> props;
245234

246235
while (!cmdLines.empty()) { // NOTE: avoid stl exception
247236
auto str = cmdLines.front();
@@ -280,7 +269,7 @@ std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
280269
continue;
281270
}
282271

283-
props[key].push_back(kvStr.substr(splitIndex + 1));
272+
props[key].emplace_back(kvStr.substr(splitIndex + 1));
284273
cmdLines.pop_front();
285274
continue;
286275
}
@@ -290,7 +279,7 @@ std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
290279
}
291280

292281
// Processing of the binary file and its parameters that am want to launch
293-
auto &execArgs = cmdLines;
282+
const auto &execArgs = cmdLines;
294283
if (execArgs.empty()) {
295284
sd_journal_print(LOG_ERR, "param exec is empty.");
296285
serviceName = "invalidInput";
@@ -302,8 +291,8 @@ std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
302291
return serviceName;
303292
}
304293

305-
int ret;
306-
if (ret = sd_bus_message_append(msg, "s", props["unitName"].front().data()); ret < 0) { // unitName
294+
int ret{0};
295+
if (ret = sd_bus_message_append(msg, "s", props["unitName"].front().c_str()); ret < 0) { // unitName
307296
sd_journal_perror("append unitName failed.");
308297
return serviceName;
309298
}
@@ -368,7 +357,7 @@ std::string cmdParse(msg_ptr &msg, std::deque<std::string_view> cmdLines)
368357

369358
int jobRemovedReceiver(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
370359
{
371-
int ret;
360+
int ret{0};
372361
if (ret = sd_bus_error_is_set(ret_error); ret != 0) {
373362
sd_journal_print(LOG_ERR, "JobRemoved error: [%s,%s]", ret_error->name, ret_error->message);
374363
} else {
@@ -385,7 +374,7 @@ int jobRemovedReceiver(sd_bus_message *m, void *userdata, sd_bus_error *ret_erro
385374
}
386375
}
387376

388-
if (ret_error and ret_error->_need_free) {
377+
if (ret_error != nullptr && ret_error->_need_free != 0) {
389378
sd_bus_error_free(ret_error);
390379
}
391380

@@ -394,7 +383,7 @@ int jobRemovedReceiver(sd_bus_message *m, void *userdata, sd_bus_error *ret_erro
394383

395384
int process_dbus_message(sd_bus *bus)
396385
{
397-
int ret;
386+
int ret{0};
398387
ret = sd_bus_process(bus, nullptr);
399388
if (ret < 0) {
400389
sd_journal_print(LOG_ERR, "event loop error.");
@@ -422,7 +411,7 @@ int main(int argc, const char *argv[])
422411
sd_bus_message *msg{nullptr};
423412
sd_bus *bus{nullptr};
424413
std::string serviceId;
425-
int ret;
414+
int ret{0};
426415

427416
if (ret = sd_bus_open_user(&bus); ret < 0) {
428417
sd_journal_perror("Failed to connect to user bus.");
@@ -476,12 +465,12 @@ int main(int argc, const char *argv[])
476465
sd_bus_message_unref(reply);
477466

478467
while (true) { // wait for jobRemoved
479-
int ret = process_dbus_message(bus);
468+
const auto ret = process_dbus_message(bus);
480469
if (ret < 0) {
481470
releaseRes(error, msg, bus, ExitCode::InternalError);
482471
}
483472

484-
if (resultData.removedFlag) {
473+
if (resultData.removedFlag != 0) {
485474
break;
486475
}
487476
}

apps/dde-autostart/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int main(int argc, char *argv[])
110110

111111
auto reply = QDBusConnection::sessionBus().call(msg);
112112
if (reply.type() == QDBusMessage::ReplyMessage) {
113-
if (reply.arguments().first().toBool()) {
113+
if (reply.arguments().constFirst().toBool()) {
114114
launchSlot();
115115
return 0;
116116
}
@@ -119,6 +119,6 @@ int main(int argc, char *argv[])
119119
}
120120

121121
// Wait for signal
122-
return app.exec();
122+
return QCoreApplication::exec();
123123
}
124124
}

src/compatibilitymanager.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,19 @@ ParserError CompatibilityManager::parse(QFile &file) noexcept{
124124
}
125125

126126
auto obj = json.object();
127-
for (const QString &desktopId : obj.keys()) {
128-
auto existDesktopId = m_compatibilityConfig.find(desktopId);
129-
if(existDesktopId != m_compatibilityConfig.end()){
130-
qWarning() << "the desktop id : "<<desktopId<<"exist skip";
127+
for (auto it = obj.constBegin(); it != obj.constEnd(); ++it) {
128+
const auto &desktopId = it.key();
129+
auto existDesktopId = m_compatibilityConfig.constFind(desktopId);
130+
if(existDesktopId != m_compatibilityConfig.cend()){
131+
qWarning() << "the desktop id : "<< it.key() <<"exist skip";
131132
continue;
132133
}
133134
auto desktopEntry = obj[desktopId].toObject();
134135

135136
// 处理 Desktop Entry
136137
CompatibilityDesktopEntry compatibilityEntry;
137-
for(auto entryId : desktopEntry.keys()){
138+
const auto& keys = desktopEntry.keys();
139+
for(const auto &entryId : std::as_const(keys)){
138140
if(!entryId.startsWith(DesktopFileEntryKey) && !entryId.startsWith(DesktopFileActionKey)){
139141
qWarning() << "parse entry : "<<entryId<<" fail";
140142
return ParserError::MissingInfo;
@@ -145,13 +147,13 @@ ParserError CompatibilityManager::parse(QFile &file) noexcept{
145147
auto envArray = entry[DesktopEntryEnv].toArray();
146148

147149
QStringList env;
148-
for (const QJsonValue &envJson : envArray) {
150+
for (const auto &envJson : std::as_const(envArray)) {
149151
env.push_back(envJson.toString());
150152
}
151153

152154
//auto tub = std::make_tuple(exec,std::move(env.join("; ") + ";"));
153155
auto tub = std::make_tuple(exec,env);
154-
compatibilityEntry.insert(entryId,std::move(tub));
156+
compatibilityEntry.insert(entryId,tub);
155157
qInfo()<<"insert desktop id : " << entryId <<", with Exec : "<< exec << "and Env : "<<env;
156158
}
157159

0 commit comments

Comments
 (0)