chainlint.pl: complain about loops lacking explicit failure handling
commitfd4094c3cad7c62adb0b7080e0dca37f66bf0c6e
authorEric Sunshine <sunshine@sunshineco.com>
Thu, 1 Sep 2022 00:29:50 +0000 (1 00:29 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 1 Sep 2022 17:07:41 +0000 (1 10:07 -0700)
treeb83ea76c26940ea812ffc4b768f02e6f9ea5d434
parent832c68b3c210267c93e1dcb2f2763372339ca36c
chainlint.pl: complain about loops lacking explicit failure handling

Shell `for` and `while` loops do not terminate automatically just
because a command fails within the loop body. Instead, the loop
continues to iterate and eventually returns the exit status of the final
command of the final iteration, which may not be the command which
failed, thus it is possible for failures to go undetected. Consequently,
it is important for test authors to explicitly handle failure within the
loop body by terminating the loop manually upon failure. This can be
done by returning a non-zero exit code from within the loop body
(i.e. `|| return 1`) or exiting (i.e. `|| exit 1`) if the loop is within
a subshell, or by manually checking `$?` and taking some appropriate
action. Therefore, add logic to detect and complain about loops which
lack explicit `return` or `exit`, or `$?` check.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
12 files changed:
t/chainlint.pl
t/chainlint/complex-if-in-cuddled-loop.expect
t/chainlint/for-loop.expect
t/chainlint/loop-detect-failure.expect [new file with mode: 0644]
t/chainlint/loop-detect-failure.test [new file with mode: 0644]
t/chainlint/loop-detect-status.expect [new file with mode: 0644]
t/chainlint/loop-detect-status.test [new file with mode: 0644]
t/chainlint/loop-in-if.expect
t/chainlint/nested-loop-detect-failure.expect [new file with mode: 0644]
t/chainlint/nested-loop-detect-failure.test [new file with mode: 0644]
t/chainlint/semicolon.expect
t/chainlint/while-loop.expect