ros2 / rosidl

Packages which provide the ROS IDL (.msg) definition and code generation.
Apache License 2.0
75 stars 125 forks source link

Add [[nodiscard]] attribute to C++ headers that return values, such as operators [Jazzy] #786

Open Ryanf55 opened 6 months ago

Ryanf55 commented 6 months ago

Feature request

Feature description

Getters and operators could have the [[nodiscard]] attribute to prevent bugs.

Implementation considerations

See /opt/ros/humble/include/geometry_msgs/geometry_msgs/msg/detail/point__struct.hpp. This is ABI breaking because it increases the strictness of the API.

Before:

  bool operator!=(const Point_ & other) const
  {
    return !this->operator==(other);
  }

Proposed:

  [[nodiscard]] bool operator!=(const Point_ & other) const
  {
    return !this->operator==(other);
  }

Effect:

Using the != operator without assigning the value would then issue a compile warning.

Related

Same as https://github.com/gazebosim/sdformat/issues/1358

wjwwood commented 6 months ago

Reviewing from our issue triaging meeting, sounds like a nice improvement, help is appreciated.