slevomat / coding-standard

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs
MIT License
1.37k stars 170 forks source link

False positive SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch.NonCapturingCatchRequired #1647

Closed Chi-teck closed 4 months ago

Chi-teck commented 7 months ago

Version: slevomat/coding-standard 8.14.1

Code to reproduce:

try {
  // Do something.
}
catch (\Exception $exception) {
  // Intentionally empty.
}
finally {
  \var_dump(isset($exception));
}

Error: Non-capturing catch is required.

The code example looks weird but it is valid. Apparently the corresponding sniff does not check usage of $exception variable outside of catch block.

rmikalkenas commented 6 months ago

A bit different, but related with the same sniff false positive:

function () {
    do {
        try {

        } catch (\Throwable $e) {

        }
    } while (true);

    throw $e;
};
kukulich commented 4 months ago

Fixed in https://github.com/slevomat/coding-standard/commit/d82a6713c0063ba31e1d2bbc8a6df22798a2e370