rosidl icon indicating copy to clipboard operation
rosidl copied to clipboard

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

Open Ryanf55 opened this issue 1 year ago • 1 comments

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

Ryanf55 avatar Feb 26 '24 17:02 Ryanf55

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

wjwwood avatar Mar 07 '24 19:03 wjwwood