Skip to content

Commit df7d313

Browse files
authored
Fix issues reported by shellcheck (#5910)
- exit on cd failure, SC2164 - needless double negative, SC2236 - fix useless use of cat, SC2002 - replace legacy backticks, SC2006 - clean unused variables, SC2034 - avoid globing/splitting on variable expansion, SC2086 - expand variables at trap execution, SC2064 - remove redundant loop over single value, SC2043 - export variable separately from value assignment, SC2155 - ${name} is not needed in arithmetic expressions, SC2004 - do not assign array to string, SC2124 - disable shellcheck for zsh block - add shebang to `dev_tools/pypath` to identify it as a bash script No change in code logic. The updated code passes the following test ``` $ git ls-files | xargs file | grep -i shell.script | cut -d: -f1 | xargs shellcheck ```
1 parent 0196738 commit df7d313

32 files changed

+154
-130
lines changed

check/all

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
################################################################################
3232

3333
# Get the working directory to the repo root.
34-
cd "$(dirname "${BASH_SOURCE[0]}")"
35-
cd "$(git rev-parse --show-toplevel)"
34+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
35+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
36+
cd "${topdir}" || exit $?
3637

3738
# Parse arguments.
3839
apply_arg=""
@@ -44,7 +45,7 @@ for arg in "$@"; do
4445
elif [[ "${arg}" == "--apply-format-changes" ]]; then
4546
apply_arg="--apply"
4647
elif [ -z "${rev}" ]; then
47-
if [ "$(git cat-file -t ${arg} 2> /dev/null)" != "commit" ]; then
48+
if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then
4849
echo -e "\033[31mNo revision '${arg}'.\033[0m" >&2
4950
exit 1
5051
fi

check/asv_run

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
################################################################################
99

1010
# Get the working directory to the repo root.
11-
cd "$(dirname "${BASH_SOURCE[0]}")"
12-
cd "$(git rev-parse --show-toplevel)"
11+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
12+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
13+
cd "${topdir}" || exit $?
1314

1415
asv run "$@"

check/build-changed-protos

+6-5
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
################################################################################
2727

2828
# Get the working directory to the repo root.
29-
cd "$(dirname "${BASH_SOURCE[0]}")"
30-
cd "$(git rev-parse --show-toplevel)"
29+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
30+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
31+
cd "${topdir}" || exit $?
3132

3233
# Figure out which revision to compare against.
33-
if [ ! -z "$1" ] && [[ $1 != -* ]]; then
34-
if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then
34+
if [ -n "$1" ] && [[ $1 != -* ]]; then
35+
if [ "$(git cat-file -t "$1" 2> /dev/null)" != "commit" ]; then
3536
echo -e "\033[31mNo revision '$1'.\033[0m" >&2
3637
exit 1
3738
fi
@@ -63,7 +64,7 @@ dev_tools/build-protos.sh
6364
# but the error logic will still work.
6465
uncommitted=$(git status --porcelain 2>/dev/null | grep -E "^?? cirq-google" | cut -d " " -f 3)
6566

66-
if [[ ! -z "$uncommitted" ]]; then
67+
if [[ -n "$uncommitted" ]]; then
6768
echo -e "\033[31mERROR: Uncommitted generated files found! Please generate and commit these files using dev_tools/build-protos.sh:\033[0m"
6869
for generated in $uncommitted
6970
do

check/doctest

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
################################################################################
1515

1616
# Get the working directory to the repo root.
17-
cd "$(dirname "${BASH_SOURCE[0]}")"
18-
cd "$(git rev-parse --show-toplevel)"
17+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
18+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
19+
cd "${topdir}" || exit $?
1920

2021
source dev_tools/pypath
2122

check/format-incremental

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
################################################################################
3333

3434
# Get the working directory to the repo root.
35-
cd "$(dirname "${BASH_SOURCE[0]}")"
36-
cd "$(git rev-parse --show-toplevel)"
35+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
36+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
37+
cd "${topdir}" || exit $?
3738

3839

3940
# Parse arguments.
@@ -46,7 +47,7 @@ for arg in "$@"; do
4647
elif [[ "${arg}" == "--all" ]]; then
4748
only_changed=0
4849
elif [ -z "${rev}" ]; then
49-
if [ "$(git cat-file -t ${arg} 2> /dev/null)" != "commit" ]; then
50+
if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then
5051
echo -e "\033[31mNo revision '${arg}'.\033[0m" >&2
5152
exit 1
5253
fi
@@ -82,7 +83,7 @@ if (( only_changed == 1 )); then
8283

8384
# Get the modified, added and moved python files.
8485
IFS=$'\n' read -r -d '' -a format_files < \
85-
<(git diff --name-only --diff-filter=MAR ${rev} -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py')
86+
<(git diff --name-only --diff-filter=MAR "${rev}" -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py')
8687
else
8788
echo -e "Formatting all python files." >&2
8889
IFS=$'\n' read -r -d '' -a format_files < \

check/misc

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
################################################################################
99

1010
# Get the working directory to the repo root.
11-
cd "$( dirname "${BASH_SOURCE[0]}" )"
12-
cd "$(git rev-parse --show-toplevel)"
11+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
12+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
13+
cd "${topdir}" || exit $?
1314

1415
# Check for non-contrib references to contrib.
1516
results=$(grep -Rl "\bcirq\.contrib\b" cirq-core | grep -v "cirq/contrib" | grep -v "__")

check/mypy

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
################################################################################
99

1010
# Get the working directory to the repo root.
11-
cd "$(dirname "${BASH_SOURCE[0]}")"
12-
cd "$(git rev-parse --show-toplevel)"
11+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
12+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
13+
cd "${topdir}" || exit $?
1314

1415
CONFIG_FILE='mypy.ini'
1516

16-
CIRQ_PACKAGES=$(env PYTHONPATH=. python dev_tools/modules.py list --mode package-path)
17+
read -r -a CIRQ_PACKAGES < \
18+
<(env PYTHONPATH=. python dev_tools/modules.py list --mode package-path)
1719

1820
echo -e -n "\033[31m"
19-
mypy --config-file=dev_tools/conf/$CONFIG_FILE "$@" $CIRQ_PACKAGES dev_tools examples
21+
mypy --config-file=dev_tools/conf/$CONFIG_FILE "$@" "${CIRQ_PACKAGES[@]}" dev_tools examples
2022
result=$?
2123
echo -e -n "\033[0m"
2224

check/nbformat

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ for arg in "$@"; do
2525
done
2626

2727
# Get the working directory to the repo root.
28-
cd "$(dirname "${BASH_SOURCE[0]}")"
29-
cd "$(git rev-parse --show-toplevel)"
28+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
29+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
30+
cd "${topdir}" || exit $?
3031

3132
pip show tensorflow-docs > /dev/null || exit 1
3233

@@ -35,7 +36,7 @@ FORMAT_CMD="python3 -m tensorflow_docs.tools.nbfmt --indent=1"
3536
# Test the notebooks
3637
unformatted=$($FORMAT_CMD --test docs 2>&1 | grep "\- docs" || true)
3738
needed_changes=0
38-
if [ ! -z "${unformatted}" ]; then
39+
if [ -n "${unformatted}" ]; then
3940
needed_changes=1
4041
if (( only_print == 0 )); then
4142
$FORMAT_CMD docs

check/npm

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
################################################################################
2424

2525
# Get the working directory to the repo root.
26-
cd "$(dirname "${BASH_SOURCE[0]}")"
27-
cd "$(git rev-parse --show-toplevel)"
26+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
27+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
28+
cd "${topdir}" || exit $?
2829

2930
npm --prefix 'cirq-web/cirq_ts' "$@"

check/npx

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
################################################################################
2323

2424
# Get the working directory to the repo root.
25-
cd "$(dirname "${BASH_SOURCE[0]}")"
26-
cd "$(git rev-parse --show-toplevel)"
25+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
26+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
2727

28-
cd 'cirq-web/cirq_ts'
28+
cd "${topdir}/cirq-web/cirq_ts" || exit $?
2929
npx "$@"

check/pylint

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
################################################################################
99

1010
# Get the working directory to the repo root.
11-
cd "$(dirname "${BASH_SOURCE[0]}")"
12-
cd "$(git rev-parse --show-toplevel)"
11+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
12+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
13+
cd "${topdir}" || exit $?
1314

14-
CIRQ_MODULES=$(env PYTHONPATH=. python dev_tools/modules.py list --mode package-path)
15+
read -r -a CIRQ_MODULES < \
16+
<(env PYTHONPATH=. python dev_tools/modules.py list --mode package-path)
1517

1618
# Add dev_tools to $PYTHONPATH so that pylint can find custom checkers
17-
env PYTHONPATH=dev_tools pylint --jobs=0 --rcfile=dev_tools/conf/.pylintrc "$@" $CIRQ_MODULES dev_tools examples
19+
env PYTHONPATH=dev_tools pylint --jobs=0 --rcfile=dev_tools/conf/.pylintrc "$@" "${CIRQ_MODULES[@]}" dev_tools examples

check/pylint-changed-files

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
################################################################################
2626

2727
# Get the working directory to the repo root.
28-
cd "$(dirname "${BASH_SOURCE[0]}")"
29-
cd "$(git rev-parse --show-toplevel)"
28+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
29+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
30+
cd "${topdir}" || exit $?
3031

3132
# Figure out which revision to compare against.
32-
if [ ! -z "$1" ] && [[ $1 != -* ]]; then
33-
if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then
33+
if [ -n "$1" ] && [[ $1 != -* ]]; then
34+
if [ "$(git cat-file -t "$1" 2> /dev/null)" != "commit" ]; then
3435
echo -e "\033[31mNo revision '$1'.\033[0m" >&2
3536
exit 1
3637
fi
@@ -55,7 +56,7 @@ fi
5556

5657
typeset -a changed
5758
IFS=$'\n' read -r -d '' -a changed < \
58-
<(git diff --name-only ${rev} -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py' \
59+
<(git diff --name-only "${rev}" -- '*.py' ':(exclude)cirq-google/cirq_google/cloud/*' ':(exclude)*_pb2.py' \
5960
| grep -E "^(cirq|dev_tools|examples).*.py$"
6061
)
6162

check/pytest

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
################################################################################
1313

1414
# Get the working directory to the repo root.
15-
cd "$(dirname "${BASH_SOURCE[0]}")"
16-
cd "$(git rev-parse --show-toplevel)"
15+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
16+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
17+
cd "${topdir}" || exit $?
1718

1819
# Run in parallel by default. Pass the `-n0` option for a single-process run.
1920
# (the last `-n` option wins)

check/pytest-and-incremental-coverage

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pytest_result=$?
7979

8080
# assume successful cover_result in case coverage is not run
8181
cover_result=0
82-
if (( $ANALYZE_COV )); then
82+
if (( ANALYZE_COV )); then
8383
# Convert to .py,cover files.
8484
coverage annotate
8585

@@ -92,7 +92,7 @@ if (( $ANALYZE_COV )); then
9292
fi
9393

9494
# Report result.
95-
if (( ${pytest_result} || ${cover_result} )); then
95+
if (( pytest_result || cover_result )); then
9696
exit 1
9797
fi
9898
exit 0

check/pytest-changed-files

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@
2828
################################################################################
2929

3030
# Get the working directory to the repo root.
31-
cd "$(dirname "${BASH_SOURCE[0]}")"
32-
cd "$(git rev-parse --show-toplevel)"
31+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
32+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
33+
cd "${topdir}" || exit $?
3334

3435
# Figure out which branch to compare against.
35-
rest=$@
36-
if [ ! -z "$1" ] && [[ $1 != -* ]]; then
37-
if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then
36+
rest=( "$@" )
37+
if [ -n "$1" ] && [[ $1 != -* ]]; then
38+
if [ "$(git cat-file -t "$1" 2> /dev/null)" != "commit" ]; then
3839
echo -e "\033[31mNo revision '$1'.\033[0m" >&2
3940
exit 1
4041
fi
4142
rev=$1
42-
rest=${@:2}
43+
rest=( "${@:2}" )
4344
elif [ "$(git cat-file -t upstream/master 2> /dev/null)" == "commit" ]; then
4445
rev=upstream/master
4546
elif [ "$(git cat-file -t origin/master 2> /dev/null)" == "commit" ]; then
@@ -77,4 +78,4 @@ fi
7778

7879
source dev_tools/pypath
7980

80-
pytest ${rest} "${changed[@]}"
81+
pytest "${rest[@]}" "${changed[@]}"

check/pytest-changed-files-and-incremental-coverage

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ cd "$( dirname "${BASH_SOURCE[0]}" )" || exit 1
3333
cd "$(git rev-parse --show-toplevel)" || exit 1
3434

3535
# Figure out which revision to compare against.
36-
if [ ! -z "$1" ] && [[ $1 != -* ]]; then
37-
if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then
36+
if [ -n "$1" ] && [[ $1 != -* ]]; then
37+
if [ "$(git cat-file -t "$1" 2> /dev/null)" != "commit" ]; then
3838
echo -e "\033[31mNo revision '$1'.\033[0m" >&2
3939
exit 1
4040
fi

check/ts-build

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
################################################################################
2323

2424
# Get the working directory to the repo root.
25-
cd "$(dirname "${BASH_SOURCE[0]}")"
26-
cd "$(git rev-parse --show-toplevel)"
25+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
26+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
27+
cd "${topdir}" || exit $?
2728

2829
check/npx webpack --mode production "$@"

check/ts-build-current

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ check/ts-build
2626
# Find the changed bundle.js files, if any
2727
untracked=$(git status --porcelain 2>/dev/null | grep "cirq-web/cirq_ts/dist/" | cut -d " " -f 3)
2828

29-
if [[ ! -z "$untracked" ]]; then
29+
if [[ -n "$untracked" ]]; then
3030
echo -e "\033[31mERROR: Uncommitted changes to bundle file(s) found! Please commit these files:\033[0m"
3131
for generated in $untracked
3232
do
@@ -35,4 +35,4 @@ if [[ ! -z "$untracked" ]]; then
3535
exit 1
3636
fi
3737

38-
exit 0
38+
exit 0

check/ts-coverage

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
################################################################################
2323

2424
# Get the working directory to the repo root.
25-
cd "$(dirname "${BASH_SOURCE[0]}")"
26-
cd "$(git rev-parse --show-toplevel)"
25+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
26+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
27+
cd "${topdir}" || exit $?
2728

2829
check/npm run coverage "$@"

check/ts-lint

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
################################################################################
2323

2424
# Get the working directory to the repo root.
25-
cd "$(dirname "${BASH_SOURCE[0]}")"
26-
cd "$(git rev-parse --show-toplevel)"
25+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
26+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
27+
cd "${topdir}" || exit $?
2728

2829
check/npm run lint "$@"

check/ts-lint-and-format

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
################################################################################
2323

2424
# Get the working directory to the repo root.
25-
cd "$(dirname "${BASH_SOURCE[0]}")"
26-
cd "$(git rev-parse --show-toplevel)"
25+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
26+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
27+
cd "${topdir}" || exit $?
2728

2829
check/npm run fix "$@"

check/ts-test

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
################################################################################
2525

2626
# Get the working directory to the repo root.
27-
cd "$(dirname "${BASH_SOURCE[0]}")"
28-
cd "$(git rev-parse --show-toplevel)"
27+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
28+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
29+
cd "${topdir}" || exit $?
2930

3031
check/npm run test "$@"

check/ts-test-e2e

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
################################################################################
2525

2626
# Get the working directory to the repo root.
27-
cd "$(dirname "${BASH_SOURCE[0]}")"
28-
cd "$(git rev-parse --show-toplevel)"
27+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
28+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
29+
cd "${topdir}" || exit $?
2930

3031
check/npm run test-e2e "$@"

dev_tools/conf/pip-install-minimal-for-pytest-changed-files.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ set -e
2020
cd "$( dirname "${BASH_SOURCE[0]}" )"
2121
cd "$(git rev-parse --show-toplevel)"
2222

23-
REQS="-r dev_tools/requirements/pytest-minimal.env.txt"
23+
reqs=( -r dev_tools/requirements/pytest-minimal.env.txt )
2424

2525
# Install contrib requirements only if needed.
2626
changed=$(git diff --name-only origin/master | grep "cirq/contrib" || true)
27-
[ "${changed}" = "" ] || REQS="$REQS -r cirq-core/cirq/contrib/requirements.txt"
27+
[ "${changed}" = "" ] || reqs+=( -r cirq-core/cirq/contrib/requirements.txt )
2828

29-
pip install $REQS
29+
pip install "${reqs[@]}"

0 commit comments

Comments
 (0)