From 7604e5eb58567c4cbfb94615b449893e3bccaea4 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 15 Apr 2017 19:53:09 -0700 Subject: [PATCH] Warn when using a glob as a command name. --- ShellCheck/Analytics.hs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index 13b522a..094e226 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -162,6 +162,7 @@ nodeChecks = [ ,checkSubshellAsTest ,checkSplittingInArrays ,checkRedirectionToNumber + ,checkGlobAsCommand ] @@ -1940,8 +1941,9 @@ checkUnassignedReferences params t = warnings prop_checkGlobsAsOptions1 = verify checkGlobsAsOptions "rm *.txt" prop_checkGlobsAsOptions2 = verify checkGlobsAsOptions "ls ??.*" prop_checkGlobsAsOptions3 = verifyNot checkGlobsAsOptions "rm -- *.txt" +prop_checkGlobsAsOptions4 = verifyNot checkGlobsAsOptions "*.txt" checkGlobsAsOptions _ (T_SimpleCommand _ _ args) = - mapM_ check $ takeWhile (not . isEndOfArgs) args + mapM_ check $ takeWhile (not . isEndOfArgs) (drop 1 args) where check v@(T_NormalWord _ (T_Glob id s:_)) | s == "*" || s == "?" = info id 2035 "Use ./*glob* or -- *glob* so names with dashes won't become options." @@ -2808,5 +2810,14 @@ checkRedirectionToNumber _ t = case t of return $ warn id 2210 "This is a file redirection. Was it supposed to be a comparison or fd operation?" _ -> return () +prop_checkGlobAsCommand1 = verify checkGlobAsCommand "foo*" +prop_checkGlobAsCommand2 = verify checkGlobAsCommand "$(var[i])" +prop_checkGlobAsCommand3 = verifyNot checkGlobAsCommand "echo foo*" +checkGlobAsCommand _ t = case t of + T_SimpleCommand _ _ (first:_) -> + when (isGlob first) $ + warn (getId first) 2211 "This is a glob used as a command name. Was it supposed to be in ${..}, array, or is it missing quoting?" + _ -> return () + return [] runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])