Re: [PATCH 01/16] check-non-portable-shell: complain about "test" a/-o instead of &&/||
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Apr 12, 2021, 17:00 UTC
- Message-ID
- <CAPig+cQFFsLeE921WpzTxVnBMnNRiKs4N=hUQ2UQi1VznNEQwg@mail.gmail.com>
- In-Reply-To
- <patch-01.16-8e4b4a2a216-20210412T110456Z-avarab@gmail.com>
On Mon, Apr 12, 2021 at 7:09 AM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:
Show 8 quoted lines
> Expand the t/check-non-portable-shell.pl checks to complain about the > use of "-a" and "-o" to the "test" shell built-in to to mean "and" and > "or", as opposed to using two "test" invocations with "&&" or "||". > > There aren't any portability issues with using that construct that I > know of, but since Junio expressed a dislike of it in [1] and we've > currently got no such constructs let's add it to the lint checking. I > had various in-flight and WIP patches that used this construct.
It's not only Junio's dislike of `-a` and `-o` but also that they have long been considered obsolescent by POSIX[1]. GNU has likewise warned against it[2] for a few decades.
[1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_16 [2]: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.70/html_node/Limitations-of-Builtins.html
Show 7 quoted lines
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
> diff --git a/t/check-non-portable-shell.pl b/t/check-non-portable-shell.pl
> @@ -35,6 +35,7 @@ sub err {
> + # Portability issues
> @@ -48,6 +49,9 @@ sub err {
> + # Coding style preferencesGiven that these flags are considered obsolescent, thus may someday be dropped from some implementations, these seem a better fit for the "portability issues" section than the "coding style preferences" section. (I'd probably just drop those section titles, though.)
> + /\btest\s+-[a-z]\s+.*?\s+-a\s+/ and err '"test A && test B" preferred to "test A -a B"'; > + /\btest\s+-[a-z]\s+.*?\s+-o\s+/ and err '"test A || test B" preferred to "test A -o B"';
These will only match the simplistic forms of `test -X blah` (where "-X" is some single letter option), but will miss expressions such as `test "$foo" = bar`. Crafting a regex to match more generally would be non-trivial, so this simpler match is a reasonable start. Okay.