Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Linter to check if the result of the new operator is silently discarded #415

@lexidor

Description

@lexidor

If the return value of a constructor is not used, the constructor is only called for its side effect or the intent was to do something with the value, but the author forgot.

Constructors with side effects that the caller cares about (without caring about the newly constructed object) are very rare and possibly bad form. Let's require that the new object is used in some manner. This makes statements that only contain a NewExpression a lint error.

Example of error that this linter would prevent.

if ($permission->hasNotBeenGrantedToUser($user)){
  new PermissionDeniedException(...);
}

The intent was probably to throw the exception, but throw was left out.

See #412 for an example of this exact bug in hhast.
fe8a322

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions