From 1835ebd3a0e5ff1460ad7fc63c228905ac5ff1f7 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 13 Jan 2019 16:06:36 -0800 Subject: [PATCH] SC2245: Warn that Ksh [ -f * ] only applies to first (Fixes #1452) --- CHANGELOG.md | 1 + src/ShellCheck/Analytics.hs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11e19945c..71f1a887d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index c82fe3148..2631a580e 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -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" @@ -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