Skip to content

SuspiciousReturn ​

error on by default

Group: Probable bugs · PhpStorm name: SuspiciousReturnInspection

A return inside a finally block discards whatever the try block was doing on its way out: its own return value is replaced and any exception in flight is swallowed. When the try block itself returns or throws, this is almost always a bug.

Example ​

php
<?php
function load($path) {
    try {
        if (!is_file($path)) {
            throw new InvalidArgumentException($path);
        }
        $data = file($path);
    } finally {
        return null;
    }
}

function fetch($id) {
    try {
        foreach ([$id] as $k) { return $k; }
    } catch (Exception $e) {
    } finally {
        $cleanup = function () { return true; };
        return;
    }
}

function quiet() {
    try {
        work();
    } catch (Exception $e) {
        return 1;
    } finally {
        return 0;
    }
}

Reported:

  • line 9: Returning from 'finally' discards the try block's return value or exception.
  • line 19: Returning from 'finally' discards the try block's return value or exception.

Configure ​

In custos.json:

json
{
  "rules": {
    "SuspiciousReturn": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore SuspiciousReturn

/**
 * @noinspection SuspiciousReturnInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.