Skip to content

Redundant .c_str() usage in rclcpp_components triggers ament_clang_tidy warning #2717

@LihanChen2004

Description

@LihanChen2004

Bug report

Required Info:

  • Operating System: Ubuntu 22.04
  • Installation type: Binary
  • Version or commit hash: Humble release branch (latest commit)
  • DDS implementation: Fast-RTPS
  • Client library: rclcpp

Steps to reproduce issue

  1. Clone the rclcpp repository from the ROS 2 Humble branch.
  2. Use the following code snippet in a file:
    for (const auto & clazz : classes) {
        std::string name = clazz.c_str();
        if (name.compare(class_name) == 0) {
            RCLCPP_DEBUG(logger, "Instantiate class %s", clazz.c_str());
            std::shared_ptr<rclcpp_components::NodeFactory> node_factory = nullptr;
            try {
                node_factory = loader->createInstance<rclcpp_components::NodeFactory>(clazz);
            } catch (const std::exception & ex) {
                RCLCPP_ERROR(logger, "Failed to load library %s", ex.what());
                return 1;
            } catch (...) {
                RCLCPP_ERROR(logger, "Failed to load library");
                return 1;
            }
            auto wrapper = node_factory->create_node_instance(options);
            auto node = wrapper.get_node_base_interface();
            node_wrappers.push_back(wrapper);
            exec.add_node(node);
        }
    }
  3. Run ament_clang_tidy as part of the build process.

Here is my .clang-tidy config:

---
Checks:          '-*,
                  performance-*,
                  -performance-unnecessary-value-param,
                  llvm-namespace-comment,
                  modernize-redundant-void-arg,
                  modernize-use-nullptr,
                  modernize-use-default,
                  modernize-use-override,
                  modernize-loop-convert,
                  modernize-make-shared,
                  modernize-make-unique,
                  misc-unused-parameters,
                  readability-named-parameter,
                  readability-redundant-smartptr-get,
                  readability-redundant-string-cstr,
                  readability-simplify-boolean-expr,
                  readability-container-size-empty,
                  readability-identifier-naming,
                  '
HeaderFilterRegex: ''
CheckOptions:
  - key:             llvm-namespace-comment.ShortNamespaceLines
    value:           '10'
  - key:             llvm-namespace-comment.SpacesBeforeComments
    value:           '2'
  - key:             misc-unused-parameters.StrictMode
    value:           '1'
  - key:             readability-braces-around-statements.ShortStatementLines
    value:           '2'
  # type names
  - key:             readability-identifier-naming.ClassCase
    value:           CamelCase
  - key:             readability-identifier-naming.EnumCase
    value:           CamelCase
  - key:             readability-identifier-naming.UnionCase
    value:           CamelCase
  # method names
  - key:             readability-identifier-naming.MethodCase
    value:           camelBack
  # variable names
  - key:             readability-identifier-naming.VariableCase
    value:           lower_case
  # class member names
  - key:             readability-identifier-naming.PrivateMemberCase
    value:           lower_case
  - key:             readability-identifier-naming.PrivateMemberSuffix
    value:           '_'
  - key:             readability-identifier-naming.ProtectedMemberCase
    value:           lower_case
  - key:             readability-identifier-naming.ProtectedMemberSuffix
    value:           '_'
  # const static or global variables are UPPER_CASE
  - key:             readability-identifier-naming.EnumConstantCase
    value:           UPPER_CASE
  - key:             readability-identifier-naming.StaticConstantCase
    value:           UPPER_CASE
  - key:             readability-identifier-naming.ClassConstantCase
    value:           UPPER_CASE
  - key:             readability-identifier-naming.GlobalVariableCase
    value:           UPPER_CASE
...

Expected behavior

No warnings or errors during the ament_clang_tidy checks.

Actual behavior

The following warning is raised during the ament_clang_tidy test:

/path/ros_ws/build/my_ros_package/rclcpp_components/node_main_sensor_scan_generation_node.cpp:43:24: warning:  redundant call to 'c_str' [readability-redundant-string-cstr]
std::string name = clazz.c_str();
                   ^~~~~~~~~~~~~
                   clazz

Additional information

The issue is caused by the redundant use of .c_str() when initializing a std::string from another std::string. This can be resolved by directly assigning clazz to name, as shown below:

std::string name = clazz;

This change avoids unnecessary conversion and adheres to modern C++ best practices. However, the original code in the repository triggers the clang-tidy warning, leading to a failure in the ament_clang_tidy test. This issue can disrupt CI pipelines and testing workflows, especially for users relying on strict linting configurations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions