Should cpp:S1705 only trigger when the returned value is discarded?

In SonarQube version 8.9.1 it appears like the rule about preferring the pre-increment over the post increment operator described here Rules explorer, detects any and all usages of the operator.

It seems a little too strict, because in some situations where the returned value is used, avoiding it is noisy. Syntactically it would make sense if the rule would trigger in the same cases where a compilation warning would be issued if the post-increment operator was declared with [[nodiscard]].

And indeed, the wording of this rule says that one should prefer to replace the post-increment with pre-increment, not that the post-increment should be avoided at all cost.

Case in point is something like this:

const int someValue = iter != vec.end() ? *iter++ : 0;

When this rule was first designed, we wanted it to be very simple, because even if the use case you present is valid, it can be hard to understand, and therefore error-prone.

However, since the alternative ways to write such code are probably not really easier to read, I’ve created a ticket to reopen this discussion.

Thank you,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.