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

1   Void g() { cout << “g() is called”; }
2   void f() { cout << “f() is called”; }
3   void f1(bool b)
4   {
5     float value2 = b ? g() : g(); 
6   }

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

1   Void g() { cout << “g() is called”; }
2   void f() { cout << “f() is called”; }
3   void f1(bool b)
4   {
5     float value2 = b ? f() : g(); 
6   }

In the fixed example, Klocwork no longer reports the defect CXX.POSSIBLE_COPY_PASTE.TERNARY_OP at line 5.