Skip to content

Add explicit lifetimes to fix mismatched_lifetime_syntaxes warnings #558

@gsokoll

Description

@gsokoll

Description

Recent versions of Rust have enabled the mismatched_lifetime_syntaxes lint by default. This produces warnings when &self implies a lifetime but the return type uses elision to hide it. The current codebase generates 16 such warnings during compilation.

Example warning

warning: hiding a lifetime that's elided elsewhere is confusing
   --> rclrs/src/client.rs:423:15
    |
423 |     fn handle(&self) -> RclPrimitiveHandle {
    |               ^^^^^     ^^^^^^^^^^^^^^^^^^ the same lifetime is hidden here
    |               |
    |               the lifetime is elided here
    |
    = help: the same lifetime is referred to in inconsistent ways, making the signature confusing
help: use `'_` for type paths
    |
423 |     fn handle(&self) -> RclPrimitiveHandle<'_> {
    |                                           ++++

Affected locations

File Function Current Should Be
action/action_client.rs:850 handle() RclPrimitiveHandle RclPrimitiveHandle<'_>
action/action_client.rs:866 lock() MutexGuard<rcl_action_client_t> MutexGuard<'_, rcl_action_client_t>
action/action_server.rs:665 handle() RclPrimitiveHandle RclPrimitiveHandle<'_>
action/action_server.rs:690 lock() MutexGuard<rcl_action_server_t> MutexGuard<'_, rcl_action_server_t>
action/action_server/action_server_goal_handle.rs:34 lock() MutexGuard<rcl_action_goal_handle_t> MutexGuard<'_, rcl_action_goal_handle_t>
client.rs:423 handle() RclPrimitiveHandle RclPrimitiveHandle<'_>
client.rs:546 lock() MutexGuard<rcl_client_t> MutexGuard<'_, rcl_client_t>
logging/log_params.rs:39 get_logger_name() &LoggerName &LoggerName<'_>
node.rs:1244 use_undeclared_parameters() Parameters Parameters<'_>
service.rs:268 handle() RclPrimitiveHandle RclPrimitiveHandle<'_>
service.rs:288 lock() MutexGuard<rcl_service_t> MutexGuard<'_, rcl_service_t>
subscription.rs:280 handle() RclPrimitiveHandle RclPrimitiveHandle<'_>
subscription.rs:300 lock() MutexGuard<rcl_subscription_t> MutexGuard<'_, rcl_subscription_t>
timer.rs:509 handle() RclPrimitiveHandle RclPrimitiveHandle<'_>
wait_set/guard_condition.rs:221 handle() RclPrimitiveHandle RclPrimitiveHandle<'_>
wait_set/rcl_primitive.rs:30 handle() (trait def) RclPrimitiveHandle RclPrimitiveHandle<'_>

Why this matters

  • Explicit lifetimes make borrow relationships clearer to readers
  • Aligns with modern Rust idioms and style guidelines
  • Helps catch bugs during future refactoring
  • As a reference implementation for ROS2 Rust development, rclrs should model good practice

Proposed fix

Add explicit '_ lifetime annotations to all affected return types. The fix for the trait definition in wait_set/rcl_primitive.rs will need to be applied consistently across all trait implementations.

Testing

  • Verify clean build with no mismatched_lifetime_syntaxes warnings on Humble, Jazzy, and Rolling
  • Run existing test suite to confirm no regressions

I'm happy to contribute a PR for this fix. Thanks for your efforts in bringing Rust to ROS2 !

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