False-positive cpp:S5417 ("std::move" and "std::forward" should not be confused) in concepts

Then tell us:

  • What language is this for?
    • C++
  • Which rule?
    • cpp:S5417
  • Why do you believe it’s a false-positive/false-negative?
    • Because I need to use the actual code in the concept to make the concept match the code.
  • Are you using
    • SonarQube Server v2026.1.0.119033
    • SonarQube for IDE v10.0.0.16576 (VS 2026), also
      • in connected mode with SonarQube Server: yes
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
#include <utility>

template<class C>
struct Generator {
    template<typename... Args>
        requires requires(C& c, Args&&... args) { c.consume(std::forward<Args>(args)...); }
    static void generateAndConsume(Args&&... args) {
        C().consume(std::forward<Args>(args)...);
    }
};

This raises cpp:S5417 in the concept in line 6.

Why is this incorrect? Compare this example use (by the way, copy-and-pasting this after the code above makes the false-positive go away, so be sure to analyse the code above in isolation).

struct Consumer {
    void consume(int&) const {}
};

int main() {
    Generator<Consumer>().generateAndConsume(1);
}

This, intentionally, does not compile. But it gives the intended message, which is that the constraint is not satisfied in line 17 (Generator<Consumer>().generateAndConsume(1);). Remove std::forward<Args> from line 6 and instead you get a compiler error in line 7: the concept accepts passing an int literal but fails forwarding it. That is not the correct code. (Replacing std::forward<Args> by std::move is even more wrong in my eyes.)

TLDR: SONAR should not flag the use of std::forward<Args> in concepts.

Hi @bers,

You are right: The rule should not trigger on your case, it is a false positive. We already have some test cases with concept, but not enough obviously. I’ve created a ticket to solve it.

Thank you for reporting it!

1 Like