From 3c4a740759398da4512f3116364ef43d5c213d9d Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Mon, 11 Mar 2024 15:25:14 +0400 Subject: [PATCH] Improved constness of local variables. --- src/applib/app_gtkmm_tools.cpp | 2 +- src/applib/async_command_executor.cpp | 2 +- src/applib/async_command_executor.h | 2 +- src/applib/ata_storage_property.cpp | 2 +- src/applib/ata_storage_property_descr.cpp | 14 ++++---- src/applib/command_executor_3ware.h | 10 +++--- src/applib/command_executor_areca.h | 10 +++--- src/applib/command_executor_gui.cpp | 2 +- .../examples/example_smartctl_parser.cpp | 2 +- src/applib/examples/example_spawn.cpp | 4 +-- src/applib/selftest.cpp | 14 ++++---- src/applib/smartctl_executor.cpp | 4 +-- src/applib/smartctl_executor.h | 7 ++-- src/applib/storage_detector_linux.cpp | 2 +- src/applib/storage_device.cpp | 2 +- .../tests/test_smartctl_version_parser.cpp | 4 +-- src/gsc_executor_log_window.cpp | 36 +++++++++---------- src/hz/data_file.h | 2 +- src/hz/fs.h | 2 +- src/rconfig/examples/example_rconfig.cpp | 2 +- 20 files changed, 65 insertions(+), 60 deletions(-) diff --git a/src/applib/app_gtkmm_tools.cpp b/src/applib/app_gtkmm_tools.cpp index e9720c2..a3e3c6b 100644 --- a/src/applib/app_gtkmm_tools.cpp +++ b/src/applib/app_gtkmm_tools.cpp @@ -141,7 +141,7 @@ Glib::ustring app_make_valid_utf8(const Glib::ustring& str) Glib::ustring app_make_valid_utf8_from_command_output(const std::string& str) { - if constexpr(BuildEnv::is_kernel_family_windows()) { + if (BuildEnv::is_kernel_family_windows()) { try { return Glib::locale_to_utf8(str); // detects invalid utf-8 sequences } catch (Glib::ConvertError& e) { diff --git a/src/applib/async_command_executor.cpp b/src/applib/async_command_executor.cpp index b46c077..702d139 100644 --- a/src/applib/async_command_executor.cpp +++ b/src/applib/async_command_executor.cpp @@ -299,7 +299,7 @@ bool AsyncCommandExecutor::try_kill() void AsyncCommandExecutor::set_stop_timeouts(std::chrono::milliseconds term_timeout_msec, std::chrono::milliseconds kill_timeout_msec) { DBG_FUNCTION_ENTER_MSG; - DBG_ASSERT(term_timeout_msec.count() == 0 || kill_timeout_msec.count() == 0 || kill_timeout_msec > term_timeout_msec); + DBG_ASSERT(term_timeout_msec.count() == 0 || kill_timeout_msec.count() == 0 || (kill_timeout_msec > term_timeout_msec)); if (!this->running_) // process not running return; diff --git a/src/applib/async_command_executor.h b/src/applib/async_command_executor.h index fdefe65..13de49b 100644 --- a/src/applib/async_command_executor.h +++ b/src/applib/async_command_executor.h @@ -94,7 +94,7 @@ class AsyncCommandExecutor : public hz::ErrorHolder { /// Returns true if command has stopped. /// Call repeatedly in a waiting function, after execute(). /// When it returns true, call stopped_cleanup(). - bool stopped_cleanup_needed() const; + [[nodiscard]] bool stopped_cleanup_needed() const; /// Check if the process is running. Note that if this returns false, it doesn't mean that diff --git a/src/applib/ata_storage_property.cpp b/src/applib/ata_storage_property.cpp index 2b5001c..936c902 100644 --- a/src/applib/ata_storage_property.cpp +++ b/src/applib/ata_storage_property.cpp @@ -396,7 +396,7 @@ bool AtaStorageProperty::empty() const void AtaStorageProperty::dump(std::ostream& os, std::size_t internal_offset) const { - std::string offset(internal_offset, ' '); + const std::string offset(internal_offset, ' '); os << offset << "[" << get_section_name(section) << (section == Section::data ? (", " + get_subsection_name(subsection)) : "") << "]" diff --git a/src/applib/ata_storage_property_descr.cpp b/src/applib/ata_storage_property_descr.cpp index f2ee202..0feaffc 100644 --- a/src/applib/ata_storage_property_descr.cpp +++ b/src/applib/ata_storage_property_descr.cpp @@ -1434,11 +1434,11 @@ namespace { std::string humanized_reported_name; std::string ssd_hdd_str; - bool known_by_smartctl = !app_pcre_match("/Unknown_(HDD|SSD)_?Attr.*/i", p.reported_name, &ssd_hdd_str); + const bool known_by_smartctl = !app_pcre_match("/Unknown_(HDD|SSD)_?Attr.*/i", p.reported_name, &ssd_hdd_str); if (known_by_smartctl) { humanized_reported_name = " " + p.reported_name + " "; // spaces are for easy replacements - static std::unordered_map replacement_map = { + static const std::unordered_map replacement_map = { {"_", " "}, {"/", " / "}, {" Ct ", " Count "}, @@ -1492,7 +1492,7 @@ namespace { std::string match = " " + humanized_reported_name + " "; std::string against = " " + attr.displayable_name + " "; - static std::unordered_map replacement_map = { + static const std::unordered_map replacement_map = { {" Percent ", " % "}, {"-", ""}, {"(", ""}, @@ -1507,7 +1507,7 @@ namespace { std::string descr = std::string("") + Glib::Markup::escape_text(attr.displayable_name) + ""; if (!same_names) { - std::string reported_name_for_descr = Glib::Markup::escape_text(hz::string_replace_copy(p.reported_name, '_', ' ')); + const std::string reported_name_for_descr = Glib::Markup::escape_text(hz::string_replace_copy(p.reported_name, '_', ' ')); descr += "\nReported by smartctl as \"" + reported_name_for_descr + "\"\n"; } descr += "\n"; @@ -1529,9 +1529,9 @@ namespace { { StatisticDescription sd = get_devstat_db().find(p.reported_name); - std::string displayable_name = (sd.displayable_name.empty() ? sd.reported_name : sd.displayable_name); + const std::string displayable_name = (sd.displayable_name.empty() ? sd.reported_name : sd.displayable_name); - bool found = !sd.description.empty(); + const bool found = !sd.description.empty(); if (!found) { sd.description = "No description is available for this attribute."; @@ -1909,7 +1909,7 @@ WarningLevel ata_storage_property_autoset_warning(AtaStorageProperty& p) if (!eb.reported_types.empty()) { WarningLevel error_block_warning = WarningLevel::none; for (const auto& reported_type : eb.reported_types) { - WarningLevel individual_warning = AtaStorageErrorBlock::get_warning_level_for_error_type(reported_type); + const WarningLevel individual_warning = AtaStorageErrorBlock::get_warning_level_for_error_type(reported_type); if (individual_warning > error_block_warning) { error_block_warning = WarningLevel(individual_warning); } diff --git a/src/applib/command_executor_3ware.h b/src/applib/command_executor_3ware.h index ad54648..2087b6d 100644 --- a/src/applib/command_executor_3ware.h +++ b/src/applib/command_executor_3ware.h @@ -12,6 +12,8 @@ Copyright: #ifndef COMMAND_EXECUTOR_3WARE_H #define COMMAND_EXECUTOR_3WARE_H +#include + #include "local_glibmm.h" #include "async_command_executor.h" @@ -87,10 +89,10 @@ void TwCliExecutorGeneric::import_error() hz::ErrorBase* e = nullptr; // find the last relevant error. - for (auto iter = errors.crbegin(); iter != errors.crend(); ++iter) { + for (const auto& error : std::ranges::reverse_view(errors)) { // ignore iochannel errors, they may mask the real errors - if ((*iter)->get_type() != "giochannel" && (*iter)->get_type() != "custom") { - e = (*iter)->clone(); + if (error->get_type() != "giochannel" && error->get_type() != "custom") { + e = error->clone(); break; } } @@ -111,7 +113,7 @@ void TwCliExecutorGeneric::on_error_warn(hz::ErrorBase* e) return; // import the error only if it's relevant. - std::string error_type = e->get_type(); + const std::string error_type = e->get_type(); // ignore giochannel errors - higher level errors will be triggered, and they more user-friendly. if (error_type == "giochannel" || error_type == "custom") { diff --git a/src/applib/command_executor_areca.h b/src/applib/command_executor_areca.h index 814cbbd..a481625 100644 --- a/src/applib/command_executor_areca.h +++ b/src/applib/command_executor_areca.h @@ -12,6 +12,8 @@ Copyright: #ifndef COMMAND_EXECUTOR_ARECA_H #define COMMAND_EXECUTOR_ARECA_H +#include + #include "local_glibmm.h" #include "async_command_executor.h" @@ -87,10 +89,10 @@ void ArecaCliExecutorGeneric::import_error() hz::ErrorBase* e = nullptr; // find the last relevant error. - for (auto iter = errors.crbegin(); iter != errors.crend(); ++iter) { + for (const auto& error : std::ranges::reverse_view(errors)) { // ignore iochannel errors, they may mask the real errors - if ((*iter)->get_type() != "giochannel" && (*iter)->get_type() != "custom") { - e = (*iter)->clone(); + if (error->get_type() != "giochannel" && error->get_type() != "custom") { + e = error->clone(); break; } } @@ -111,7 +113,7 @@ void ArecaCliExecutorGeneric::on_error_warn(hz::ErrorBase* e) return; // import the error only if it's relevant. - std::string error_type = e->get_type(); + const std::string error_type = e->get_type(); // ignore giochannel errors - higher level errors will be triggered, and they more user-friendly. if (error_type == "giochannel" || error_type == "custom") { diff --git a/src/applib/command_executor_gui.cpp b/src/applib/command_executor_gui.cpp index 722a5ce..796787f 100644 --- a/src/applib/command_executor_gui.cpp +++ b/src/applib/command_executor_gui.cpp @@ -133,7 +133,7 @@ void CommandExecutorGui::set_running_dialog_abort_mode(bool aborting) } else if (!aborting) { - std::string msg = hz::string_replace_copy(get_running_msg(), "{command}", + const std::string msg = hz::string_replace_copy(get_running_msg(), "{command}", hz::fs_path_to_string(hz::fs_path_from_string(this->get_command_name()).filename())); running_dialog_->set_message("\n " + msg + " "); // running_dialog_->set_response_sensitive(Gtk::RESPONSE_CANCEL, true); diff --git a/src/applib/examples/example_smartctl_parser.cpp b/src/applib/examples/example_smartctl_parser.cpp index 9ad8eca..df56314 100644 --- a/src/applib/examples/example_smartctl_parser.cpp +++ b/src/applib/examples/example_smartctl_parser.cpp @@ -36,7 +36,7 @@ int main(int argc, char* argv[]) const hz::fs::path file(argv[1]); // native encoding std::string contents; - auto ec = hz::fs_file_get_contents(file, contents, 10*1024*1024); // 10M + auto ec = hz::fs_file_get_contents(file, contents, 10LLU*1024*1024); // 10M if (ec) { debug_out_error("app", ec.message() << "\n"); return EXIT_FAILURE; diff --git a/src/applib/examples/example_spawn.cpp b/src/applib/examples/example_spawn.cpp index aaf6781..bcc316a 100644 --- a/src/applib/examples/example_spawn.cpp +++ b/src/applib/examples/example_spawn.cpp @@ -24,7 +24,7 @@ int main() GPid pid = {}; int fd_stdout = 0, fd_stderr = 0; - std::string cmd = "iexplore"; + const std::string cmd = "iexplore"; // std::vector child_argv = Glib::shell_parse_argv(cmd); gchar* curr_dir = g_get_current_dir(); @@ -54,7 +54,7 @@ int main() #endif // blocking writes if the pipe is full helps for small-pipe systems (see man 7 pipe). - int channel_flags = ~G_IO_FLAG_NONBLOCK; + const int channel_flags = ~G_IO_FLAG_NONBLOCK; if (channel_stdout) { g_io_channel_set_encoding(channel_stdout, nullptr, nullptr); // binary IO diff --git a/src/applib/selftest.cpp b/src/applib/selftest.cpp index 60f2082..5aa4a01 100644 --- a/src/applib/selftest.cpp +++ b/src/applib/selftest.cpp @@ -44,14 +44,14 @@ std::chrono::seconds SelfTest::get_remaining_seconds() const { using namespace std::literals; - std::chrono::seconds total = get_min_duration_seconds(); + const std::chrono::seconds total = get_min_duration_seconds(); if (total <= 0s) return -1s; // unknown - double gran = (double(total.count()) / 9.); // seconds per 10% + const double gran = (double(total.count()) / 9.); // seconds per 10% // since remaining_percent_ may be manually set to 100, we limit from the above. - double rem_seconds_at_last_change = std::min(double(total.count()), gran * remaining_percent_ / 10.); - double rem = rem_seconds_at_last_change - timer_.elapsed(); + const double rem_seconds_at_last_change = std::min(double(total.count()), gran * remaining_percent_ / 10.); + const double rem = rem_seconds_at_last_change - timer_.elapsed(); return std::chrono::seconds(std::max(int64_t(0), (int64_t)std::round(rem))); // don't return negative values. } @@ -76,7 +76,7 @@ std::chrono::seconds SelfTest::get_min_duration_seconds() const case TestType::conveyance: prop_name = "ata_smart_data/self_test/polling_minutes/conveyance"; break; } - AtaStorageProperty p = drive_->lookup_property(prop_name, + const AtaStorageProperty p = drive_->lookup_property(prop_name, AtaStorageProperty::Section::data, AtaStorageProperty::SubSection::capabilities); // p stores it as uint64_t @@ -103,7 +103,7 @@ bool SelfTest::is_supported() const case TestType::conveyance: prop_name = "ata_smart_data/capabilities/conveyance_self_test_supported"; break; } - AtaStorageProperty p = drive_->lookup_property(prop_name, AtaStorageProperty::Section::internal); + const AtaStorageProperty p = drive_->lookup_property(prop_name, AtaStorageProperty::Section::internal); return (!p.empty() && p.get_value()); } @@ -185,7 +185,7 @@ std::string SelfTest::force_stop(const std::shared_ptr& smartct // any command (e.g. "--abort") will abort it. If it has "Suspend Offline...", // there's no way to abort such test. if (type_ == TestType::immediate_offline) { - AtaStorageProperty p = drive_->lookup_property( + const AtaStorageProperty p = drive_->lookup_property( "ata_smart_data/capabilities/offline_is_aborted_upon_new_cmd", AtaStorageProperty::Section::internal); if (!p.empty() && p.get_value()) { // if empty, give a chance to abort anyway. return _("Aborting this test is unsupported by the drive."); diff --git a/src/applib/smartctl_executor.cpp b/src/applib/smartctl_executor.cpp index dcf2c41..a278848 100644 --- a/src/applib/smartctl_executor.cpp +++ b/src/applib/smartctl_executor.cpp @@ -24,7 +24,7 @@ hz::fs::path get_smartctl_binary() { auto smartctl_binary = hz::fs_path_from_string(rconfig::get_data("system/smartctl_binary")); - if constexpr(BuildEnv::is_kernel_family_windows()) { + if (BuildEnv::is_kernel_family_windows()) { // Look in smartmontools installation directory. hz::fs::path system_binary; do { @@ -85,7 +85,7 @@ std::string execute_smartctl(const std::string& device, const std::string& devic std::shared_ptr smartctl_ex, std::string& smartctl_output) { // win32 doesn't have slashes in devices names. For others, check that slash is present. - if constexpr(!BuildEnv::is_kernel_family_windows()) { + if (!BuildEnv::is_kernel_family_windows()) { const std::string::size_type pos = device.rfind('/'); // find basename if (pos == std::string::npos) { debug_out_error("app", DBG_FUNC_MSG << "Invalid device name \"" << device << "\".\n"); diff --git a/src/applib/smartctl_executor.h b/src/applib/smartctl_executor.h index e8d173e..f199cd3 100644 --- a/src/applib/smartctl_executor.h +++ b/src/applib/smartctl_executor.h @@ -13,6 +13,7 @@ Copyright: #define SMARTCTL_EXECUTOR_H #include "local_glibmm.h" +#include #include #include "async_command_executor.h" @@ -102,10 +103,10 @@ class SmartctlExecutorGeneric : public ExecutorSync { hz::ErrorBase* e = nullptr; // find the last relevant error. - for (auto iter = errors.crbegin(); iter != errors.crend(); ++iter) { + for (const auto& error : std::ranges::reverse_view(errors)) { // ignore iochannel errors, they may mask the real errors - if ((*iter)->get_type() != "giochannel" && (*iter)->get_type() != "custom") { - e = (*iter)->clone(); + if (error->get_type() != "giochannel" && error->get_type() != "custom") { + e = error->clone(); break; } } diff --git a/src/applib/storage_detector_linux.cpp b/src/applib/storage_detector_linux.cpp index d685897..83f1f0a 100644 --- a/src/applib/storage_detector_linux.cpp +++ b/src/applib/storage_detector_linux.cpp @@ -248,7 +248,7 @@ inline std::string read_proc_scsi_scsi_file(std::vector< std::pair accel_group = this->get_accel_group(); + const Glib::RefPtr accel_group = this->get_accel_group(); if (window_close_button) { window_close_button->add_accelerator("clicked", accel_group, GDK_KEY_Escape, Gdk::ModifierType(0), Gtk::AccelFlags(0)); @@ -117,7 +117,7 @@ void GscExecutorLogWindow::show_last() if (treeview != nullptr && !list_store->children().empty()) { // Gtk::TreeRow row = *(list_store->children().rbegin()); // this causes invalid read error in valgrind - Gtk::TreeRow row = *(--(list_store->children().end())); + const Gtk::TreeRow row = *(--(list_store->children().end())); selection->select(row); // you would think that scroll_to_row would accept a TreeRow for a change (shock!) treeview->scroll_to_row(list_store->get_path(row)); @@ -136,7 +136,7 @@ void GscExecutorLogWindow::clear_view_widgets() auto* output_textview = this->lookup_widget("output_textview"); if (output_textview) { - Glib::RefPtr buffer = output_textview->get_buffer(); + const Glib::RefPtr buffer = output_textview->get_buffer(); buffer->set_text(""); } @@ -153,7 +153,7 @@ void GscExecutorLogWindow::on_command_output_received(const CommandExecutorResul entries.push_back(entry); // update tree model - Gtk::TreeRow row = *(list_store->append()); + const Gtk::TreeRow row = *(list_store->append()); row[col_num] = entries.size(); row[col_command] = info.command + " " + info.parameters; row[col_entry] = entry; @@ -187,8 +187,8 @@ void GscExecutorLogWindow::on_window_save_current_button_clicked() if (selection->count_selected_rows() == 0) return; - Gtk::TreeIter iter = selection->get_selected(); - std::shared_ptr entry = (*iter)[col_entry]; + const Gtk::TreeIter iter = selection->get_selected(); + const std::shared_ptr entry = (*iter)[col_entry]; static std::string last_dir; if (last_dir.empty()) { @@ -196,16 +196,16 @@ void GscExecutorLogWindow::on_window_save_current_button_clicked() } int result = 0; - Glib::RefPtr specific_filter = Gtk::FileFilter::create(); + const Glib::RefPtr specific_filter = Gtk::FileFilter::create(); specific_filter->set_name(_("Text Files")); specific_filter->add_pattern("*.txt"); - Glib::RefPtr all_filter = Gtk::FileFilter::create(); + const Glib::RefPtr all_filter = Gtk::FileFilter::create(); all_filter->set_name(_("All Files")); all_filter->add_pattern("*"); #if GTK_CHECK_VERSION(3, 20, 0) - std::unique_ptr dialog(gtk_file_chooser_native_new( + const std::unique_ptr dialog(gtk_file_chooser_native_new( _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), &g_object_unref); @@ -314,16 +314,16 @@ void GscExecutorLogWindow::on_window_save_all_button_clicked() } int result = 0; - Glib::RefPtr specific_filter = Gtk::FileFilter::create(); + const Glib::RefPtr specific_filter = Gtk::FileFilter::create(); specific_filter->set_name(_("Text Files")); specific_filter->add_pattern("*.txt"); - Glib::RefPtr all_filter = Gtk::FileFilter::create(); + const Glib::RefPtr all_filter = Gtk::FileFilter::create(); all_filter->set_name(_("All Files")); all_filter->add_pattern("*"); #if GTK_CHECK_VERSION(3, 20, 0) - std::unique_ptr dialog(gtk_file_chooser_native_new( + const std::unique_ptr dialog(gtk_file_chooser_native_new( _("Save Data As..."), this->gobj(), GTK_FILE_CHOOSER_ACTION_SAVE, nullptr, nullptr), &g_object_unref); @@ -412,18 +412,18 @@ void GscExecutorLogWindow::on_tree_selection_changed() this->clear_view_widgets(); if (selection->count_selected_rows() > 0) { - Gtk::TreeIter iter = selection->get_selected(); - Gtk::TreeRow row = *iter; + const Gtk::TreeIter iter = selection->get_selected(); + const Gtk::TreeRow& row = *iter; - std::shared_ptr entry = row[col_entry]; + const std::shared_ptr entry = row[col_entry]; if (auto* output_textview = this->lookup_widget("output_textview")) { - Glib::RefPtr buffer = output_textview->get_buffer(); + const Glib::RefPtr buffer = output_textview->get_buffer(); if (buffer) { buffer->set_text(app_make_valid_utf8_from_command_output(entry->std_output)); Glib::RefPtr tag; - Glib::RefPtr table = buffer->get_tag_table(); + const Glib::RefPtr table = buffer->get_tag_table(); if (table) tag = table->lookup("font"); if (!tag) @@ -435,7 +435,7 @@ void GscExecutorLogWindow::on_tree_selection_changed() } if (auto* command_entry = this->lookup_widget("command_entry")) { - std::string cmd_text = entry->command + " " + entry->parameters; + const std::string cmd_text = entry->command + " " + entry->parameters; command_entry->set_text(app_make_valid_utf8_from_command_output(cmd_text)); } diff --git a/src/hz/data_file.h b/src/hz/data_file.h index 3ce0ff7..196dea5 100644 --- a/src/hz/data_file.h +++ b/src/hz/data_file.h @@ -47,7 +47,7 @@ inline void data_file_add_search_directory(const std::string& domain, fs::path p /// Get currently registered search directories (a copy is returned) inline std::vector data_file_get_search_directories(const std::string& domain) { - if (DataFileStaticHolder::search_directories.count(domain) > 0) { + if (DataFileStaticHolder::search_directories.contains(domain)) { return DataFileStaticHolder::search_directories.at(domain); } return {}; diff --git a/src/hz/fs.h b/src/hz/fs.h index 99ee8e3..cc7566c 100644 --- a/src/hz/fs.h +++ b/src/hz/fs.h @@ -111,7 +111,7 @@ inline std::string fs_path_to_string(const fs::path& p) /// Same as std::filesystem::u8path(std::string), which is deprecated since C++20. inline fs::path fs_path_from_string(std::string_view u8str) { - return fs::path(u8string_from_string(u8str)); + return {u8string_from_string(u8str)}; } diff --git a/src/rconfig/examples/example_rconfig.cpp b/src/rconfig/examples/example_rconfig.cpp index 201189b..9b5ab2b 100644 --- a/src/rconfig/examples/example_rconfig.cpp +++ b/src/rconfig/examples/example_rconfig.cpp @@ -59,7 +59,7 @@ int main() #if defined ENABLE_GLIB && ENABLE_GLIB rconfig::autosave_set_config_file("test2.config"); rconfig::autosave_start(2s); // every 2 seconds - while(true) { + while(true) { // FIXME This is undefined behavior // without this the timeout function won't be called. g_main_context_iteration(nullptr, FALSE); }