Skip to content

UselessParentheses false positive with bitwise not operator (~) #1752

@spaze

Description

@spaze

I have a method that contains only this body (for testing):

	private function foo(): bool
	{
		~(1 - 1);
	}

PHPCS reports the following on ~(1 - 1);:

----------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------
 11 | ERROR | [x] Useless parentheses. (SlevomatCodingStandard.PHP.UselessParentheses.UselessParentheses)
----------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------------

Running phpcbf then removes the outer parentheses so that the method body reads

~1 - 1;

But those two lines don't give the same result:

> ~(1 - 1);
= -1

> ~1 - 1;
= -3

Using slevomat/coding-standard 8.18.0, phpcs 3.13.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions