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

[no squash] Address some clang-tidy warnings #14153

Merged
merged 2 commits into from
Dec 25, 2023
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
7 changes: 5 additions & 2 deletions .github/workflows/cpp_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ on:
- 'util/ci/**'
- '.github/workflows/**.yml'

env:
CLANG_TIDY: clang-tidy-15

jobs:
clang_tidy:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- name: Install deps
run: |
source ./util/ci/common.sh
install_linux_deps clang-tidy-9
install_linux_deps $CLANG_TIDY

- name: Run clang-tidy
run: |
Expand Down
5 changes: 2 additions & 3 deletions src/clientiface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,9 @@ void RemoteClient::GetNextBlocks (
Get the border/face dot coordinates of a "d-radiused"
box
*/
std::vector<v3s16> list = FacePositionCache::getFacePositions(d);
const auto &list = FacePositionCache::getFacePositions(d);

std::vector<v3s16>::iterator li;
for (li = list.begin(); li != list.end(); ++li) {
for (auto li = list.begin(); li != list.end(); ++li) {
v3s16 p = *li + center;

/*
Expand Down
4 changes: 2 additions & 2 deletions src/database/database-sqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,8 @@ void ModStorageDatabaseSQLite3::listMods(std::vector<std::string> *res)
return 0;
}, (void *) res, &errmsg);
if (status != SQLITE_OK) {
DatabaseException e(std::string("Error trying to list mods with metadata: ") + errmsg);
auto msg = std::string("Error trying to list mods with metadata: ") + errmsg;
sqlite3_free(errmsg);
throw e;
throw DatabaseException(msg);
}
}
2 changes: 1 addition & 1 deletion src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ bool ItemStack::itemFits(ItemStack newitem,
return newitem.empty();
}

bool ItemStack::stacksWith(ItemStack other) const
bool ItemStack::stacksWith(const ItemStack &other) const
{
return (this->name == other.name &&
this->wear == other.wear &&
Expand Down
2 changes: 1 addition & 1 deletion src/inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct ItemStack

// Checks if another itemstack would stack with this one.
// Does not check if the item actually fits in the stack.
bool stacksWith(ItemStack other) const;
bool stacksWith(const ItemStack &other) const;

// Takes some items.
// If there are not enough, takes as many as it can.
Expand Down
2 changes: 1 addition & 1 deletion src/mapgen/mg_schematic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ void Schematic::condenseContentIds()
numids++;

m_nodenames.push_back(m_ndef->get(c).name);
nodeidmap.emplace(std::make_pair(c, id));
nodeidmap.emplace(c, id);
} else {
id = it->second;
}
Expand Down
4 changes: 2 additions & 2 deletions src/script/cpp_api/s_mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "cpp_api/s_internal.h"
#include "common/c_converter.h"

void ScriptApiMainMenu::setMainMenuData(MainMenuDataForScript *data)
void ScriptApiMainMenu::setMainMenuData(const MainMenuDataForScript *data)
{
SCRIPTAPI_PRECHECKHEADER

Expand All @@ -38,7 +38,7 @@ void ScriptApiMainMenu::setMainMenuData(MainMenuDataForScript *data)
lua_pop(L, 1);
}

void ScriptApiMainMenu::handleMainMenuEvent(std::string text)
void ScriptApiMainMenu::handleMainMenuEvent(const std::string &text)
{
SCRIPTAPI_PRECHECKHEADER

Expand Down
4 changes: 2 additions & 2 deletions src/script/cpp_api/s_mainmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class ScriptApiMainMenu : virtual public ScriptApiBase {
* Hand over MainMenuDataForScript to lua to inform lua of the content
* @param data the data
*/
void setMainMenuData(MainMenuDataForScript *data);
void setMainMenuData(const MainMenuDataForScript *data);

/**
* process events received from formspec
* @param text events in textual form
*/
void handleMainMenuEvent(std::string text);
void handleMainMenuEvent(const std::string &text);

/**
* process field data received from formspec
Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ int ModApiItem::l_get_content_id(lua_State *L)

// If this is called at mod load time, NodeDefManager isn't aware of
// aliases yet, so we need to handle them manually
std::string alias_name = idef->getAlias(name);
const auto &alias_name = idef->getAlias(name);

content_t content_id;
if (alias_name != name) {
Expand Down
14 changes: 7 additions & 7 deletions src/script/lua_api/l_mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_converter.h"

/******************************************************************************/
std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
std::string ModApiMainMenu::getTextData(lua_State *L, const std::string &name)
{
lua_getglobal(L, "gamedata");

Expand All @@ -56,7 +56,7 @@ std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
}

/******************************************************************************/
int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
int ModApiMainMenu::getIntegerData(lua_State *L, const std::string &name, bool& valid)
{
lua_getglobal(L, "gamedata");

Expand All @@ -65,14 +65,14 @@ int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
if(lua_isnil(L, -1)) {
valid = false;
return -1;
}
}

valid = true;
return luaL_checkinteger(L, -1);
}

/******************************************************************************/
int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
int ModApiMainMenu::getBoolData(lua_State *L, const std::string &name, bool& valid)
{
lua_getglobal(L, "gamedata");

Expand All @@ -81,7 +81,7 @@ int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
if(lua_isnil(L, -1)) {
valid = false;
return false;
}
}

valid = true;
return readParam<bool>(L, -1);
Expand Down Expand Up @@ -553,7 +553,7 @@ int ModApiMainMenu::l_create_world(lua_State *L)
// Set the settings for world creation
// this is a bad hack but the best we have right now..
StringMap backup;
for (auto it : use_settings) {
for (auto &it : use_settings) {
if (g_settings->existsLocal(it.first))
backup[it.first] = g_settings->get(it.first);
g_settings->set(it.first, it.second);
Expand All @@ -569,7 +569,7 @@ int ModApiMainMenu::l_create_world(lua_State *L)
}

// Restore previous settings
for (auto it : use_settings) {
for (auto &it : use_settings) {
auto it2 = backup.find(it.first);
if (it2 == backup.end())
g_settings->remove(it.first); // wasn't set before
Expand Down
6 changes: 3 additions & 3 deletions src/script/lua_api/l_mainmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ class ModApiMainMenu: public ModApiBase
* @param name name of variable to read
* @return string value of requested variable
*/
static std::string getTextData(lua_State *L, std::string name);
static std::string getTextData(lua_State *L, const std::string &name);

/**
* read an integer variable from gamedata table within lua stack
* @param L stack to read variable from
* @param name name of variable to read
* @return integer value of requested variable
*/
static int getIntegerData(lua_State *L, std::string name,bool& valid);
static int getIntegerData(lua_State *L, const std::string &name, bool& valid);

/**
* read a bool variable from gamedata table within lua stack
* @param L stack to read variable from
* @param name name of variable to read
* @return bool value of requested variable
*/
static int getBoolData(lua_State *L, std::string name,bool& valid);
static int getBoolData(lua_State *L, const std::string &name ,bool& valid);

/**
* Checks if a path may be modified. Paths in the temp directory or the user
Expand Down
5 changes: 2 additions & 3 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ bool Settings::updateConfigFile(const char *filename)


bool Settings::parseCommandLine(int argc, char *argv[],
std::map<std::string, ValueSpec> &allowed_options)
const std::map<std::string, ValueSpec> &allowed_options)
{
int nonopt_index = 0;
for (int i = 1; i < argc; i++) {
Expand All @@ -424,8 +424,7 @@ bool Settings::parseCommandLine(int argc, char *argv[],

std::string name = arg_name.substr(2);

std::map<std::string, ValueSpec>::iterator n;
n = allowed_options.find(name);
auto n = allowed_options.find(name);
if (n == allowed_options.end()) {
errorstream << "Unknown command-line parameter \""
<< arg_name << "\"" << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Settings {
bool updateConfigFile(const char *filename);
// NOTE: Types of allowed_options are ignored. Returns success.
bool parseCommandLine(int argc, char *argv[],
std::map<std::string, ValueSpec> &allowed_options);
const std::map<std::string, ValueSpec> &allowed_options);
bool parseConfigLines(std::istream &is);
void writeLines(std::ostream &os, u32 tab_depth=0) const;

Expand Down
6 changes: 3 additions & 3 deletions src/texture_override.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static const std::map<std::string, OverrideTarget> override_LUT = {
{ "*", OverrideTarget::ALL_FACES }
};

TextureOverrideSource::TextureOverrideSource(std::string filepath)
TextureOverrideSource::TextureOverrideSource(const std::string &filepath)
{
std::ifstream infile(filepath.c_str());
std::string line;
Expand Down Expand Up @@ -115,7 +115,7 @@ TextureOverrideSource::TextureOverrideSource(std::string filepath)
}

//! Get all overrides that apply to item definitions
std::vector<TextureOverride> TextureOverrideSource::getItemTextureOverrides()
std::vector<TextureOverride> TextureOverrideSource::getItemTextureOverrides() const
{
std::vector<TextureOverride> found_overrides;

Expand All @@ -128,7 +128,7 @@ std::vector<TextureOverride> TextureOverrideSource::getItemTextureOverrides()
}

//! Get all overrides that apply to node definitions
std::vector<TextureOverride> TextureOverrideSource::getNodeTileOverrides()
std::vector<TextureOverride> TextureOverrideSource::getNodeTileOverrides() const
{
std::vector<TextureOverride> found_overrides;

Expand Down
6 changes: 3 additions & 3 deletions src/texture_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ struct TextureOverride
class TextureOverrideSource
{
public:
TextureOverrideSource(std::string filepath);
TextureOverrideSource(const std::string &filepath);

//! Get all overrides that apply to item definitions
std::vector<TextureOverride> getItemTextureOverrides();
std::vector<TextureOverride> getItemTextureOverrides() const;

//! Get all overrides that apply to node definitions
std::vector<TextureOverride> getNodeTileOverrides();
std::vector<TextureOverride> getNodeTileOverrides() const;

private:
std::vector<TextureOverride> m_overrides;
Expand Down
3 changes: 2 additions & 1 deletion src/util/enriched_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ void EnrichedString::clear()
m_background = irr::video::SColor(0, 0, 0, 0);
}

void EnrichedString::operator=(const wchar_t *str)
EnrichedString &EnrichedString::operator=(const wchar_t *str)
{
clear();
addAtEnd(translate_string(std::wstring(str)), m_default_color);
return *this;
}

void EnrichedString::addAtEnd(const std::wstring &s, SColor initial_color)
Expand Down
2 changes: 1 addition & 1 deletion src/util/enriched_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EnrichedString {
const video::SColor &color = video::SColor(255, 255, 255, 255));
EnrichedString(const std::wstring &string,
const std::vector<video::SColor> &colors);
void operator=(const wchar_t *str);
EnrichedString &operator=(const wchar_t *str);

void clear();

Expand Down
2 changes: 1 addition & 1 deletion src/util/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ std::string sanitizeDirName(const std::string &str, const std::string &optional_
{
std::wstring safe_name = utf8_to_wide(str);

for (std::wstring disallowed_name : disallowed_dir_names) {
for (auto &disallowed_name : disallowed_dir_names) {
if (str_equal(safe_name, disallowed_name, true)) {
safe_name = utf8_to_wide(optional_prefix) + safe_name;
break;
Expand Down
4 changes: 2 additions & 2 deletions util/ci/clang-tidy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
cmake -B build -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
-DRUN_IN_PLACE=TRUE \
-DENABLE_{GETTEXT,SOUND}=FALSE \
-DENABLE_GETTEXT=FALSE \
-DBUILD_SERVER=TRUE
cmake --build build --target GenerateVersion

./util/ci/run-clang-tidy.py \
-clang-tidy-binary=clang-tidy-9 -p build \
-clang-tidy-binary=$CLANG_TIDY -p build \
-quiet -config="$(cat .clang-tidy)" \
'src/.*'
Loading