CXX.POSSIBLE_COPY_PASTE.TERNARY_OP
Possible copy-paste in the 'then' and 'else' branches of a ternary operator.
This checker reports defects when the same expression is used in ‘then’ and ‘else’ branches of a ternary operator.
Vulnerable code example
Copy
void g() { cout << “g() is called”; }
void f() { cout << “f() is called”; }
void f1(bool b)
{
float value2 = b ? g() : g();
}
In the above example, line 5 is noncompliant as both then and else branches of the ternary operator are exactly the same. Klocwork reports a CXX.POSSIBLE_COPY_PASTE.TERNARY_OP defect at line 5, indicating, "Possible copy-paste in the 'then' and 'else' branches of a ternary operator."
Fixed code example
Copy
void g() { cout << “g() is called”; }
void f() { cout << “f() is called”; }
void f1(bool b)
{
float value2 = b ? f() : g();
}
In the fixed example, Klocwork no longer reports the defect CXX.POSSIBLE_COPY_PASTE.TERNARY_OP at line 5.