tools/format-cpp: make shellcheck happy again
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 26 Oct 2023 12:42:56 +0000 (08:42 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 30 Oct 2023 18:15:46 +0000 (14:15 -0400)
This patch:

* Checks the `version` variable assignment directly to avoid relying on
  `$?`, therefore removing the `shellcheck disable` comment.

* Makes `formatter` an array to satisfy SC2086, therefore removing the
  `shellcheck disable` comment.

* Quotes `$(nproc)` to satisfy SC2068.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ib7c5fe530faad12704ee8b91ae55a6bfcb1aee5c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11142
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-by: Michael Jeanson <mjeanson@efficios.com>
tools/format-cpp

index 7fcf75896e986e4198cbe961ce77d3fa6ceaef9f..a2c6a3aefee8c3ccbce0cb7d62f390136211a538 100755 (executable)
@@ -8,14 +8,11 @@ expected_formatter_major_version=15
 
 # Runs the formatter, making sure it's the expected version.
 format_cpp() {
-       local formatter=$1
+       local formatter=("$@")
        local version
 
-       version=$($formatter --version)
-
-        # shellcheck disable=SC2181
-       if (($? != 0)); then
-               echo "Cannot execute \`$formatter --version\`." >&2
+       if ! version=$("${formatter[@]}" --version); then
+               echo "Cannot execute \`${formatter[*]} --version\`." >&2
                return 1
        fi
 
@@ -34,25 +31,23 @@ format_cpp() {
 
        # Using xargs to fail as soon as the formatter fails (`-exec`
        # won't stop if its subprocess fails).
-       #
-       # shellcheck disable=SC2086
        find "$root_dir" \( -name '*.cpp' -o -name '*.hpp' \) \
                ! -wholename '*/cpp-common/optional.hpp' \
                ! -wholename '*/cpp-common/string_view.hpp' \
                ! -wholename '*/cpp-common/nlohmann/json.hpp' \
-               -print0 | xargs -P$(nproc) -n1 -0 $formatter
+               -print0 | xargs -P"$(nproc)" -n1 -0 "${formatter[@]}"
 }
 
 if [[ -n "$FORMATTER" ]]; then
        # Try using environment-provided formatter
-       formatter=$FORMATTER
+       read -ra formatter <<< "$FORMATTER"
 elif command -v clang-format-$expected_formatter_major_version &> /dev/null; then
        # Try using the expected version of clang-format
-       formatter="clang-format-$expected_formatter_major_version -i"
+       formatter=("clang-format-$expected_formatter_major_version" -i)
 else
        # Try using `clang-format` as is
-       formatter='clang-format -i'
+       formatter=(clang-format -i)
 fi
 
 # Try to format files
-format_cpp "$formatter"
+format_cpp "${formatter[@]}"
This page took 0.025786 seconds and 4 git commands to generate.