From 64cc7c691a8b909108c657f048fa214ed6acf995 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 22 Jun 2014 14:16:24 -0700 Subject: [PATCH] Warn about precedence in find -name -o -name -exec. --- ShellCheck/Analytics.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index da251ae..51b15fa 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -206,6 +206,7 @@ nodeChecks = [ ,checkShouldUseGrepQ ,checkTestGlobs ,checkConcatenatedDollarAt + ,checkFindActionPrecedence ] @@ -2861,6 +2862,24 @@ checkTestGlobs params (TC_Unary _ _ op token) | isGlob token = op ++ " doesn't work with globs. Use a for loop." checkTestGlobs _ _ = return () +prop_checkFindActionPrecedence1 = verify checkFindActionPrecedence "find . -name '*.wav' -o -name '*.au' -exec rm {} +" +prop_checkFindActionPrecedence2 = verifyNot checkFindActionPrecedence "find . -name '*.wav' -o \\( -name '*.au' -exec rm {} + \\)" +prop_checkFindActionPrecedence3 = verifyNot checkFindActionPrecedence "find . -name '*.wav' -o -name '*.au'" +checkFindActionPrecedence params = checkCommand "find" (const f) + where + pattern = [isMatch, const True, isParam ["-o"], isMatch, const True, isAction] + f list | length list < length pattern = return () + f list@(_:rest) = + if all id (zipWith ($) pattern list) + then warnFor (list !! ((length pattern)-1)) + else f rest + isMatch = isParam [ "-name", "-regex", "-iname", "-iregex" ] + isAction = isParam [ "-exec", "-execdir", "-delete", "-print" ] + isParam strs t = fromMaybe False $ do + param <- getLiteralString t + return $ param `elem` strs + warnFor t = warn (getId t) 2146 "This action ignores everything before the -o. Use \\( \\) to group." + return [] runTests = $quickCheckAll