From 8bc98d89a77267c4f421ba78958737bb3225d38d Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 5 Dec 2015 16:50:11 -0800 Subject: [PATCH] Let SC2094 ignore echo/printf/sponge. --- ShellCheck/Analytics.hs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index 6320714..f32ef7b 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -896,6 +896,8 @@ prop_checkRedirectToSame2 = verify checkRedirectToSame "cat lol | sed -e 's/a/b/ prop_checkRedirectToSame3 = verifyNot checkRedirectToSame "cat lol | sed -e 's/a/b/g' > foo.bar && mv foo.bar lol" prop_checkRedirectToSame4 = verifyNot checkRedirectToSame "foo /dev/null > /dev/null" prop_checkRedirectToSame5 = verifyNot checkRedirectToSame "foo > bar 2> bar" +prop_checkRedirectToSame6 = verifyNot checkRedirectToSame "echo foo > foo" +prop_checkRedirectToSame7 = verifyNot checkRedirectToSame "sed 's/foo/bar/g' file | sponge file" checkRedirectToSame params s@(T_Pipeline _ _ list) = mapM_ (\l -> (mapM_ (\x -> doAnalysis (checkOccurrences x) l) (getAllRedirs list))) list where @@ -905,7 +907,8 @@ checkRedirectToSame params s@(T_Pipeline _ _ list) = when (exceptId /= newId && x == y && not (isOutput t && isOutput u) - && not (special t)) $ do + && not (special t) + && not (any isHarmlessCommand [t,u])) $ do addComment $ note newId addComment $ note exceptId checkOccurrences _ _ = return () @@ -928,6 +931,11 @@ checkRedirectToSame params s@(T_Pipeline _ _ list) = T_DGREAT _ -> True _ -> False _ -> False + isHarmlessCommand arg = fromMaybe False $ do + cmd <- getClosestCommand (parentMap params) arg + name <- getCommandBasename cmd + return $ name `elem` ["echo", "printf", "sponge"] + checkRedirectToSame _ _ = return () @@ -1104,9 +1112,7 @@ checkSingleQuotedVariables params t@(T_SingleQuoted id s) = commandName = fromMaybe "" $ do cmd <- getClosestCommand parents t name <- getCommandBasename cmd - if name == "find" - then return $ getFindCommand cmd - else return name + return $ if name == "find" then getFindCommand cmd else name isProbablyOk = any isOkAssignment (take 3 $ getPath parents t)