Replace const std::string& with std::string_view#3216
Replace const std::string& with std::string_view#3216Lestropie merged 3 commits intocheck_syntax_enhancedfrom
Conversation
f8f575a to
975db28
Compare
|
I think this is a good idea, but there is one specific case where |
|
Yep, mentioned in OP. Was trivial to find them in this process, because following a mass class rename |
bugprone-suspicious-stringview-data-usage may be helpful here. I think the problem may only appear when interacting with libraries like |
…k_syntax_noconststringref Conflicts: cpp/cmd/fixelconvert.cpp cpp/core/fixel/helpers.h cpp/core/fixel/legacy/image.h cpp/core/image_io/sparse.h cpp/gui/mrview/tool/fixel/legacy.h
|
|
||
| template <class HeaderType> | ||
| ZClean(const HeaderType &in, const std::string &message) | ||
| ZClean(const HeaderType &in, std::string_view message) |
There was a problem hiding this comment.
warning: constructor does not initialize these fields: upper, lower [cppcoreguidelines-pro-type-member-init]
cpp/core/filter/zclean.h:273:
- float upper, lower;
+ float upper{}, lower{};| ndim() = 3; | ||
| } | ||
|
|
||
| template <class HeaderType> ZClean(const HeaderType &in) : ZClean(in, "") {} |
There was a problem hiding this comment.
warning: constructor does not initialize these fields: zupper, zlower, fov_max, fov_min, bridge, dont_maskupper, keep_lower, keep_upper, upper, lower [cppcoreguidelines-pro-type-member-init]
cpp/core/filter/zclean.h:269:
- float zupper, zlower;
- float fov_max, fov_min;
- size_t bridge;
- bool dont_maskupper, keep_lower, keep_upper;
- float upper, lower;
+ float zupper{}, zlower{};
+ float fov_max{}, fov_min{};
+ size_t bridge{};
+ bool dont_maskupper{}, keep_lower{}, keep_upper{};
+ float upper{}, lower{};| } SliceData; | ||
|
|
||
| inline const SliceData parse_line(const std::string &line, const ParCols &cols) { | ||
| inline const SliceData parse_line(std::string_view line, const ParCols &cols) { |
There was a problem hiding this comment.
warning: return type 'const SliceData' is 'const'-qualified at the top level, which may reduce code readability without improving const correctness [readability-const-return-type]
| inline const SliceData parse_line(std::string_view line, const ParCols &cols) { | |
| inline SliceData parse_line(std::string_view line, const ParCols &cols) { |
|
|
||
| // For exchangeability blocks (either within or whole) | ||
| index_array_type load_blocks(const std::string &filename, const bool equal_sizes); | ||
| index_array_type load_blocks(std::string_view filename, const bool equal_sizes); |
There was a problem hiding this comment.
warning: parameter 'equal_sizes' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls]
| index_array_type load_blocks(std::string_view filename, const bool equal_sizes); | |
| index_array_type load_blocks(std::string_view filename, bool equal_sizes); |
|
|
||
| void clear_scheme(KeyValues &keyval) { | ||
| auto erase = [&](const std::string &s) { | ||
| auto erase = [&](const std::string s) { |
There was a problem hiding this comment.
warning: the const qualified parameter 's' is copied for each invocation; consider making it a reference [performance-unnecessary-value-param]
| auto erase = [&](const std::string s) { | |
| auto erase = [&](const std::string& s) { |
|
|
||
| static void (*previous_print_func)(const std::string &msg); | ||
| static void (*previous_report_to_user_func)(const std::string &msg, int type); | ||
| static void (*previous_print_func)(std::string_view msg); |
There was a problem hiding this comment.
warning: variable 'previous_print_func' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
static void (*previous_print_func)(std::string_view msg);
^| static void (*previous_print_func)(const std::string &msg); | ||
| static void (*previous_report_to_user_func)(const std::string &msg, int type); | ||
| static void (*previous_print_func)(std::string_view msg); | ||
| static void (*previous_report_to_user_func)(std::string_view msg, int type); |
There was a problem hiding this comment.
warning: variable 'previous_report_to_user_func' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
static void (*previous_report_to_user_func)(std::string_view msg, int type);
^| static void (*previous_print_func)(const std::string &msg); | ||
| static void (*previous_report_to_user_func)(const std::string &msg, int type); | ||
| static void (*previous_print_func)(std::string_view msg); | ||
| static void (*previous_report_to_user_func)(std::string_view msg, int type); |
There was a problem hiding this comment.
warning: variable 'previous_report_to_user_func' provides global access to a non-const object; consider making the pointed-to data 'const' [cppcoreguidelines-avoid-non-const-global-variables]
static void (*previous_report_to_user_func)(std::string_view msg, int type);
^| public: | ||
| template <class Functor> | ||
| __single_thread(Functor &&functor, const std::string &name = "unnamed") : __thread_base(name) { | ||
| template <class Functor> __single_thread(Functor &&functor, std::string_view name = "unnamed") : __thread_base(name) { |
There was a problem hiding this comment.
warning: constructor accepting a forwarding reference can hide the move constructor [bugprone-forwarding-reference-overload]
template <class Functor> __single_thread(Functor &&functor, std::string_view name = "unnamed") : __thread_base(name) {
^Additional context
cpp/core/thread.h:112: move constructor declared here
__single_thread(__single_thread &&) = default;
^| return std::abs(x); | ||
| } | ||
|
|
||
| template <class ValueType> struct is_string_type : std::false_type {}; |
There was a problem hiding this comment.
warning: redefinition of 'is_string_type' [clang-diagnostic-error]
template <class ValueType> struct is_string_type : std::false_type {};
^Additional context
cpp/core/types.h:193: previous definition is here
template <class ValueType> struct is_string_type : std::false_type {};
^
Proposed extension of #3212; listing separately for transparency / discussion.
At some point down the code quality rabbit-hole, particularly in seeking getting rid of all
char*/char**usage beyond just the C++ CLI code in #2911, I came across @daljit46's use ofstd::string_view. This is intended to simultaneously:chararrays orstd::string;const std::string&".The latter is appealing from a code quality perspective. One item that's often on my mind when navigating the code base is the regular use of pass-by-reference arguments to functions: sometimes it's as a read-only reference to avoid data duplication, others it's to enable functions to write back more than one piece of data (which I increasingly don't like; defining a struct to encapsulate all content returned by a function would be preferable). The presence or absence of
constisn't ideal for discriminating between these two very different purposes.As such, given I was already committed to #3212, I decided to have a go at forbidding the use of
const std::string &. There's only one remaining set of functions that persist with usingconst std::string &, and that's only because they're both private functions and the data immediately have to go through astd::istringstream.There are two main items to which I draw attention:
There are a handful of STL items---primarily file I/O I think---that still require a null-terminated character array as input. However the
.data()pointer ofstd::string_viewis not guaranteed to be null-terminated, and therefore should not be passed to such functions. Hopefully this shouldn't be a problem if all file manipulations are done through API calls, and those API calls utilisestd::string().c_str().Much of the remaining clunkiness of this transition revolves around string concatenation via
MR::operator+(). These would be more comprehensively addressed by extending Add {}-style string formatting for logging macros #3125 or a replacement to deal with string concatenation & formatting more generally.