Skip to content

UselessReturn ​

info on by default quick-fix

Group: Confusing constructs · PhpStorm name: UselessReturnInspection

Two pointless return forms: a bare return; as the very last statement of a function body (the function ends there anyway), and return $local = expr; where the assignment to a local variable can never be observed after the function has returned.

Example ​

php
<?php
function build(array $parts, &$out) {
    $memo = null;
    $join = function ($sep, &$acc) use (&$memo, $parts) {
        if ($memo !== null) {
            return $memo = implode($sep, $parts);
        }
        if ($acc === []) {
            return $acc = $parts;
        }
        return $sep = strtoupper($sep);
    };
    if ($out) {
        return $out = $join;
    }
    return $parts = $join;
}

function clear(array &$rows) {
    if (!$rows) { return; }
    $rows = [];
    return;
}

function counter() {
    static $n = 0;
    return $n = $n + 1;
}

function guarded() {
    try {
        return $res = compute();
    } finally {
        release($res);
    }
}

function wrapped() {
    return ($tmp = compute());
}
php
<?php
function build(array $parts, &$out) {
    $memo = null;
    $join = function ($sep, &$acc) use (&$memo, $parts) {
        if ($memo !== null) {
            return $memo = implode($sep, $parts);
        }
        if ($acc === []) {
            return $acc = $parts;
        }
        return strtoupper($sep);
    };
    if ($out) {
        return $out = $join;
    }
    return $join;
}

function clear(array &$rows) {
    if (!$rows) { return; }
    $rows = [];
    return;
}

function counter() {
    static $n = 0;
    return $n = $n + 1;
}

function guarded() {
    try {
        return $res = compute();
    } finally {
        release($res);
    }
}

function wrapped() {
    return ($tmp = compute());
}

Reported:

  • line 11: The assigned variable is never used after returning; return the value directly.
  • line 16: The assigned variable is never used after returning; return the value directly.
  • line 22: Redundant 'return;' at the end of the body; remove it.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore UselessReturn

/**
 * @noinspection UselessReturnInspection
 */

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