From 50084c06c5706eb7018d213bdf8312e9f0e8725b Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Fri, 1 Jul 2016 21:26:46 -0700 Subject: [PATCH] Don't warn when $(seq) is used unquoted. --- ShellCheck/ASTLib.hs | 18 ++++++++++++++++-- ShellCheck/Analytics.hs | 7 ++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ShellCheck/ASTLib.hs b/ShellCheck/ASTLib.hs index ccf183e..c85390d 100644 --- a/ShellCheck/ASTLib.hs +++ b/ShellCheck/ASTLib.hs @@ -215,6 +215,19 @@ getCommandName t = T_Annotation _ _ t -> getCommandName t otherwise -> Nothing +-- If a command substitution is a single command, get its name. +-- $(date +%s) = Just "date" +getCommandNameFromExpansion :: Token -> Maybe String +getCommandNameFromExpansion t = + case t of + T_DollarExpansion _ [c] -> extract c + T_Backticked _ [c] -> extract c + T_DollarBraceCommandExpansion _ [c] -> extract c + otherwise -> Nothing + where + extract (T_Pipeline _ _ [cmd]) = getCommandName cmd + extract _ = Nothing + -- Get the basename of a token representing a command getCommandBasename = liftM basename . getCommandName where @@ -238,8 +251,8 @@ isOnlyRedirection t = isFunction t = case t of T_Function {} -> True; _ -> False --- Get the list of commands from tokens that contain them, such as --- the body of while loops and if statements. +-- Get the lists of commands from tokens that contain them, such as +-- the body of while loops or branches of if statements. getCommandSequences t = case t of T_Script _ _ cmds -> [cmds] @@ -252,6 +265,7 @@ getCommandSequences t = T_IfExpression _ thens elses -> map snd thens ++ [elses] otherwise -> [] +-- Get a list of names of associative arrays getAssociativeArrays t = nub . execWriter $ doAnalysis f t where diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index 9dfe2cb..d36b8b4 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -765,6 +765,7 @@ prop_checkUnquotedExpansions4 = verifyNot checkUnquotedExpansions "[[ $(foo) == prop_checkUnquotedExpansions5 = verifyNot checkUnquotedExpansions "for f in $(cmd); do echo $f; done" prop_checkUnquotedExpansions6 = verifyNot checkUnquotedExpansions "$(cmd)" prop_checkUnquotedExpansions7 = verifyNot checkUnquotedExpansions "cat << foo\n$(ls)\nfoo" +prop_checkUnquotedExpansions8 = verifyNot checkUnquotedExpansions "set -- $(seq 1 4)" checkUnquotedExpansions params = check where @@ -774,9 +775,12 @@ checkUnquotedExpansions params = check _ = return () tree = parentMap params examine t = - unless (isQuoteFree tree t || usedAsCommandName tree t) $ + unless (shouldBeSplit t || isQuoteFree tree t || usedAsCommandName tree t) $ warn (getId t) 2046 "Quote this to prevent word splitting." + shouldBeSplit t = + getCommandNameFromExpansion t == Just "seq" + prop_checkRedirectToSame = verify checkRedirectToSame "cat foo > foo" prop_checkRedirectToSame2 = verify checkRedirectToSame "cat lol | sed -e 's/a/b/g' > lol" @@ -1030,6 +1034,7 @@ checkSingleQuotedVariables params t@(T_SingleQuoted id s) = ,"alias" ,"sudo" -- covering "sudo sh" and such ,"dpkg-query" + ,"jq" -- could also check that user provides --arg ] || "awk" `isSuffixOf` commandName || "perl" `isPrefixOf` commandName