Best Practices - C Callbacks

Tell me you’ve heard this one before - You’re working on a RT system, and you have tasks/state machines/some other kind of structure that relies heavily on callbacks.

The callback usually has one void* parameter. This parameter is not const because it’s often some kind of context parameter. In some callbacks/handlers, you don’t do anything with this parameter. You can’t forgo this parameter altogether, because in some callbacks/handlers it IS used.

Adding new tasks/states will yield more callbacks/handlers. If any of them don’t use the context parameter, they’ll trigger the false-positive c:S1172 and c:S995.

Found on Developer Edition Version 10.4.1 (build 88267)

If this is not a false positive - what are best practices? :slight_smile:

Hi,

Could you provide a code snippet to reproduce the issues?

\thx

Hi @alexo,

I’ll jump ahead because I think I know what you mean, but please do not hesitate to share a snippet reproducing the issue.

We know of issues with the rule you mentioned and we have [CPP-2782] - Jira to track the improvement.

As for the best practice, since you mentioned C, I would say with C23 you can use [[maybe_unused]] to make it clearer that the callback does not use the parameter but is still required to exist.

I hope this helps

2 Likes