The Not operator is applied twice to the same operand

ID

vbnet.correctness.no_double_operators

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Control Flow

Language

VB.NET

Description

Reports an expression in which Not is applied twice in a row, as in Not Not enabled or Not (Not enabled). The two negations cancel out, so the expression is equivalent to its operand.

Rationale

A doubled Not never changes the result, so at best it is noise the reader has to unwind to see that the expression means its operand. More often it is the visible residue of an edit that went wrong: a negation was added to a condition that was already negated, or a Not was meant to be removed and a second one was typed instead. Either way the surrounding logic no longer says what its author intended, and the redundant operator hides that. Applied to an integer, where Not is the bitwise complement, the same cancellation holds and the expression is a pure waste of two operations.

The following code illustrates the pattern detected by this rule:

Public Function IsActive(ByVal account As Account) As Boolean
    ' FLAGGED: The Not operator is applied twice to the same operand
    Return Not Not account.Enabled
End Function

Remediation

Delete both Not operators and use the operand directly. If a single negation was intended, keep exactly one. When the value is already Boolean, no conversion is needed - Not Not is not a Boolean cast.

' Before
Return Not Not account.Enabled

' After
Return account.Enabled

Configuration

This detector does not need any configuration.