Qt-Generic-NoOverloadedOperators

Do not overload certain operators

Required inputs: IR

Programmers expect operator && and operator || to have short-circuiting behavior. As it is impossible for user-defined overloads of these operators to have the expected behavior, these operators should never be overloaded.

The built-in operator , introduces a "sequence before" relation between the left and right operands. However, user-defined operator overloads act like functions, and thus prior to C++17 do not have a defined sequence relation between their arguments. While in C++17 and later the arguments are sequenced to match the built-in operator, overloaded operator , may not match programmer expectations and should therefore be avoided.

Programmers expect the address-of operator operator & to return the address of its operand. User-defined overloads can lead to surprising behavior and should be avoided.

Bad code:
class S {
public:
    bool operator&&(const S& other) const; // ERROR: Do not overload operator&&.
    bool operator||(const S& other) const; // ERROR: Do not overload operator||.
    bool operator,(const S& other) const; // ERROR: Do not overload operator,.
    S *operator&() const; // ERROR: Do not overload operator&.
};

Possible Messages

Key

Text

Severity

Disabled

forbidden_operator_overload

Do not overload operator.

None

False

Options

invalid

invalid : set[str] = {'operator&&', 'operator,', 'operator||'}

Selection of disallowed operator overloads by name.
 

invalid_kinds

invalid_kinds

Type: set[Operator_Type]

Default: {'ADDRESS_OF'}

Selection of disallowed operator overloads by operator kind.
 

Option Types

These types are used by options listed above:

Operator_Type

An enumeration.
 
  • NEW

  • ARRAY_NEW

  • ADD

  • UNARY_PLUS

  • MULTIPLY

  • DEREFERENCE_STAR

  • MODULO

  • ADDRESS_OF

  • BIT_AND

  • BIT_NOT

  • ASSIGN

  • GREATER_THAN

  • SUBTRACT_ASSIGN

  • DIVIDE_ASSIGN

  • BIT_XOR_ASSIGN

  • BIT_OR_ASSIGN

  • SHIFT_RIGHT

  • SHIFT_LEFT_ASSIGN

  • UNEQUAL

  • GREATER_OR_EQUAL

  • OR_ELSE

  • PREFIX_DECREMENT

  • POSTFIX_DECREMENT

  • ARROW_STAR

  • CALL

  • CONDITIONAL

  • GNU_MIN

  • DELETE

  • ARRAY_DELETE

  • SUBTRACT

  • UNARY_MINUS

  • DIVIDE

  • BIT_XOR

  • BIT_OR

  • LOGICAL_NOT

  • LESS_THAN

  • ADD_ASSIGN

  • MULTIPLY_ASSIGN

  • MODULO_ASSIGN

  • BIT_AND_ASSIGN

  • SHIFT_LEFT

  • SHIFT_RIGHT_ASSIGN

  • EQUAL

  • LESS_OR_EQUAL

  • AND_THEN

  • PREFIX_INCREMENT

  • POSTFIX_INCREMENT

  • COMMA

  • DEREFERENCE_ARROW

  • ARRAY_ACCESS

  • GNU_MAX

  • THREE_WAY_COMPARE

  • USER_DEFINED_LITERAL