Why is this an issue?

The Delphi bitwise not operator binds stronger than the in binary operator, which can lead to subtly incorrect code. For example, in the code below the bitwise not has been confused for a logical not, which has introduced a bug:

var MyByte: Byte := 3;

if not MyByte in [252, 253, 254, 255] then
  raise Exception.Create('MyByte must not be above 251!'); // This is raised!

To avoid this pitfall, complex expressions involving not and in should be parenthesized appropriately, so the precedence is obvious at a glance.

How to fix it

If the bitwise not is intentional, parenthesize it:

var MyByte: Byte := 3;
if not MyByte in [252] then
  WriteLn('error: MyByte must not be 252!');
var MyByte: Byte := 3;
if (not MyByte) in [252] then
  WriteLn('error: MyByte must not be 3!');

Otherwise, parenthesize the binary expression that should be negated:

var MyByte: Byte := 3;
if not MyByte in [252] then
  WriteLn('error: MyByte must not be 252!');
var MyByte: Byte := 3;
if not (MyByte in [252]) then
  WriteLn('error: MyByte must be 252!');

Resources