Skip to content

Commit

Permalink
SC2245: Warn that Ksh [ -f * ] only applies to first (Fixes #1452)
Browse files Browse the repository at this point in the history
  • Loading branch information
koalaman committed Jan 14, 2019
1 parent ec6f9e4 commit 1835ebd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Since previous release
### Added
- Preliminary support for fix suggestions
- SC2245: Warn that Ksh ignores all but the first glob result in `[`
- SC2243/SC2244: Suggest using explicit -n for `[ $foo ]`

## v0.6.0 - 2018-12-02
Expand Down
16 changes: 13 additions & 3 deletions src/ShellCheck/Analytics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2466,8 +2466,10 @@ prop_checkTestArgumentSplitting13 = verify checkTestArgumentSplitting "[ \"$@\"
prop_checkTestArgumentSplitting14 = verify checkTestArgumentSplitting "[[ \"$@\" == \"\" ]]"
prop_checkTestArgumentSplitting15 = verifyNot checkTestArgumentSplitting "[[ \"$*\" == \"\" ]]"
prop_checkTestArgumentSplitting16 = verifyNot checkTestArgumentSplitting "[[ -v foo[123] ]]"
prop_checkTestArgumentSplitting17 = verifyNot checkTestArgumentSplitting "#!/bin/ksh\n[ -e foo* ]"
prop_checkTestArgumentSplitting18 = verify checkTestArgumentSplitting "#!/bin/ksh\n[ -d foo* ]"
checkTestArgumentSplitting :: Parameters -> Token -> Writer [TokenComment] ()
checkTestArgumentSplitting _ t =
checkTestArgumentSplitting params t =
case t of
(TC_Unary _ typ op token) | isGlob token ->
if op == "-v"
Expand All @@ -2476,8 +2478,16 @@ checkTestArgumentSplitting _ t =
err (getId token) 2208 $
"Use [[ ]] or quote arguments to -v to avoid glob expansion."
else
err (getId token) 2144 $
op ++ " doesn't work with globs. Use a for loop."
if (typ == SingleBracket && shellType params == Ksh)
then
-- Ksh appears to stop processing after unrecognized tokens, so operators
-- will effectively work with globs, but only the first match.
when (op `elem` ['-':c:[] | c <- "bcdfgkprsuwxLhNOGRS" ]) $
warn (getId token) 2245 $
op ++ " only applies to the first expansion of this glob. Use a loop to check any/all."
else
err (getId token) 2144 $
op ++ " doesn't work with globs. Use a for loop."

(TC_Nullary _ typ token) -> do
checkBraces typ token
Expand Down

0 comments on commit 1835ebd

Please sign in to comment.