-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor: Remove c_str from util/check #26960
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
The head ref may contain hidden characters: "2301-c-str-\u{1F50B}"
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
Concept ACK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK fab9582
Pretty straightforward change that makes the code more robust. I'm curious why you reverted from fab5500 to fab9582, I think inline_check_non_fatal
and inline_assertion_check
could benefit from string_view
too? Compilation and unit tests seem fine here - so I'm probably missing some nuance?
Yeah, it might be fine, but I'd prefer to wait for C++23 consteval. Otherwise there seems to be the risk that an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK fab9582
If I understand correctly: because if we start using these helpers to take If so, nit: worth adding a little |
Thanks, done in #23363 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK fab9582
fab9582 refactor: Remove c_str from util/check (MarcoFalke) Pull request description: Seems confusing and fragile to require calling code to call `c_str()` when passing a read-only view of a std::string. Fix that by using std::string_view, which can be constructed from string literals and std::string. Also, remove the now unused `c_str()` from `src/wallet/bdb.cpp`. ACKs for top commit: stickies-v: ACK fab9582 aureleoules: ACK fab9582 theStack: ACK fab9582 Tree-SHA512: ae39812c6bb8e2ef095e1b843774af2718f48404cb848c3e43b16d3c22240557d69d54a13a038a4a9c48b3ba0e4523e1f87abdd60f91486092f50fd43c0e8483
I tried this and it seems to compile, but I don't think it is worth it: diff --git a/src/util/check.h b/src/util/check.h
index a02a1de8dc..f15117daef 100644
--- a/src/util/check.h
+++ b/src/util/check.h
@@ -21,9 +21,11 @@ public:
NonFatalCheckError(std::string_view msg, std::string_view file, int line, std::string_view func);
};
+consteval std::string_view inline_String_View(const char* sv) { return std::string_view{sv}; }
+
/** Helper for CHECK_NONFATAL() */
template <typename T>
-T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
+T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, std::string_view file, int line, std::string_view func, std::string_view assertion)
{
if (!val) {
throw NonFatalCheckError{assertion, file, line, func};
@@ -40,7 +42,7 @@ void assertion_fail(std::string_view file, int line, std::string_view func, std:
/** Helper for Assert()/Assume() */
template <bool IS_ASSERT, typename T>
-T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const char* file, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* assertion)
+T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] std::string_view file, [[maybe_unused]] int line, [[maybe_unused]] std::string_view func, [[maybe_unused]] std::string_view assertion)
{
if constexpr (IS_ASSERT
#ifdef ABORT_ON_FAILED_ASSUME
@@ -74,7 +76,7 @@ T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const char* f
inline_check_non_fatal(condition, __FILE__, __LINE__, __func__, #condition)
/** Identity function. Abort if the value compares equal to zero */
-#define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val)
+#define Assert(val) inline_assertion_check<true>(val, inline_String_View(__FILE__), __LINE__, __func__, #val)
/**
* Assume is the identity function. Seems better to just remove the helper functions and macros and use a plain C++20 function, which would also make the compiler warning more useful, see #28999 (comment) |
Seems confusing and fragile to require calling code to call
c_str()
when passing a read-only view of a std::string.Fix that by using std::string_view, which can be constructed from string literals and std::string.
Also, remove the now unused
c_str()
fromsrc/wallet/bdb.cpp
.