Skip to content

Commit 574c6d1

Browse files
committed
Suggest using test -e instead of -a (fixes #3174).
1 parent 72af76f commit 574c6d1

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- SC2327/SC2328: Warn about capturing the output of redirected commands.
44
- SC2329: Warn when (non-escaping) functions are never invoked.
55
- SC2330: Warn about unsupported glob matches with [[ .. ]] in BusyBox.
6+
- SC2331: Suggest using standard -e instead of unary -a in tests.
67
- Precompiled binaries for Linux riscv64 (linux.riscv64)
78
### Changed
89
- SC2002 about Useless Use Of Cat is now disabled by default. It can be

src/ShellCheck/Analytics.hs

+10
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ nodeChecks = [
204204
,checkUnnecessaryParens
205205
,checkPlusEqualsNumber
206206
,checkExpansionWithRedirection
207+
,checkUnaryTestA
207208
]
208209

209210
optionalChecks = map fst optionalTreeChecks
@@ -5098,6 +5099,15 @@ checkExpansionWithRedirection params t =
50985099
err redirectId 2328 $ "This redirection takes output away from the command substitution" ++ if suggestTee then " (use tee to duplicate)." else "."
50995100

51005101

5102+
prop_checkUnaryTestA1 = verify checkUnaryTestA "[ -a foo ]"
5103+
prop_checkUnaryTestA2 = verify checkUnaryTestA "[ ! -a foo ]"
5104+
prop_checkUnaryTestA3 = verifyNot checkUnaryTestA "[ foo -a bar ]"
5105+
checkUnaryTestA params t =
5106+
case t of
5107+
TC_Unary id _ "-a" _ ->
5108+
styleWithFix id 2331 "For file existence, prefer standard -e over legacy -a." $
5109+
fixWith [replaceStart id params 2 "-e"]
5110+
_ -> return ()
51015111

51025112
return []
51035113
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])

0 commit comments

Comments
 (0)