hhvm / hhast

Mutable AST library for Hack with linting and code migrations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

lexidor opened this issue · comments

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