Redundant .c_str() usage in rclcpp_components triggers ament_clang_tidy warning#2718
Redundant .c_str() usage in rclcpp_components triggers ament_clang_tidy warning#2718fujitatomoya merged 2 commits intoros2:humblefrom LihanChen2004:fix-redundant-c_str
Conversation
Signed-off-by: LihanChen2004 <757003373@qq.com>
NOTE: `ament_cmake_clang_tidy` will be added when the PR will be merged. ros2/rclcpp#2718
NOTE: `ament_cmake_clang_tidy` will be added when the PR will be merged. ros2/rclcpp#2718
fujitatomoya
left a comment
There was a problem hiding this comment.
@LihanChen2004 thanks for creating PR.
Co-authored-by: Tomoya Fujita <Tomoya.Fujita@sony.com> Signed-off-by: LihanChen2004 <74599182+LihanChen2004@users.noreply.github.com>
|
Pulls: #2718 |
fujitatomoya
left a comment
There was a problem hiding this comment.
lgtm, CI failing because this targets to humble only. i will restart.
|
Pulls: #2718 |
The code snippet:
is redundant and unnecessary when initializing a
std::stringobject.The method
std::string::operator=(const char*), which takes a C-style string (const char*), introduces an extra conversion step when the input is already astd::string. Specifically, thec_str()method is used to extract aconst char*pointer fromclazz, and thenstd::stringreconstructs the string from the pointer.Instead, the simpler and more efficient approach is:
This directly utilizes the copy constructor or move constructor of
std::string, avoiding the unnecessary intermediate conversion toconst char*. It results in cleaner, more idiomatic C++ code with no performance penalty.Addresses: #2717