From d603ee1e89b57ca4297fb65d38e534b6c6395ba8 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 24 Nov 2013 16:15:10 -0800 Subject: [PATCH] Don't warn for A&&B||C if C is echo/exit/assignment --- ShellCheck/Analytics.hs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index d596c4f..9acf67b 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -611,8 +611,16 @@ checkRedirectToSame _ _ = return () prop_checkShorthandIf = verify checkShorthandIf "[[ ! -z file ]] && scp file host || rm file" prop_checkShorthandIf2 = verifyNot checkShorthandIf "[[ ! -z file ]] && { scp file host || echo 'Eek'; }" -checkShorthandIf (T_AndIf id _ (T_OrIf _ _ _)) = +prop_checkShorthandIf3 = verifyNot checkShorthandIf "foo && bar || echo baz" +prop_checkShorthandIf4 = verifyNot checkShorthandIf "foo && a=b || a=c" +checkShorthandIf (T_AndIf id _ (T_OrIf _ _ (T_Pipeline _ t))) + | not $ isOk t = info id 2015 "Note that A && B || C is not if-then-else. C may run when A is true." + where + isOk [t] = isAssignment t || (fromMaybe False $ do + name <- getCommandBasename t + return $ name `elem` ["echo", "exit"]) + isOk _ = False checkShorthandIf _ = return () @@ -987,9 +995,12 @@ getCommandName (T_SimpleCommand _ _ (w:_)) = getCommandName _ = Nothing getCommandBasename = liftM basename . getCommandName - basename = reverse . (takeWhile (/= '/')) . reverse +isAssignment (T_Redirecting _ _ w) = isAssignment w +isAssignment (T_SimpleCommand _ (w:_) []) = True +isAssignment _ = False + prop_checkPrintfVar1 = verify checkPrintfVar "printf \"Lol: $s\"" prop_checkPrintfVar2 = verifyNot checkPrintfVar "printf 'Lol: $s'" prop_checkPrintfVar3 = verify checkPrintfVar "printf -v cow $(cmd)"