From 3e2cb2611908434306e61d6264020f1866fa86df Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Mon, 17 Sep 2018 17:46:49 -0700 Subject: [PATCH] Add SC2238 about redirections to command names --- CHANGELOG.md | 1 + src/ShellCheck/Analytics.hs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33f9338..44e38d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Added - Command line option --severity/-S for filtering by minimum severity - SC2236/SC2237: Suggest -n/-z instead of ! -z/-n +- SC2238: Warn when redirecting to a known command name, e.g. ls > rm ### Changed - Most warnings now have useful end positions - SC1117 about unknown double-quoted escape sequences has been retired diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index 394dee4..58098f4 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -169,6 +169,7 @@ nodeChecks = [ ,checkForLoopGlobVariables ,checkSubshelledTests ,checkInvertedStringTest + ,checkRedirectionToCommand ] @@ -3027,6 +3028,15 @@ checkInvertedStringTest _ t = _ -> return () _ -> return () +prop_checkRedirectionToCommand1 = verify checkRedirectionToCommand "ls > rm" +prop_checkRedirectionToCommand2 = verifyNot checkRedirectionToCommand "ls > 'rm'" +prop_checkRedirectionToCommand3 = verifyNot checkRedirectionToCommand "ls > myfile" +checkRedirectionToCommand _ t = + case t of + T_IoFile _ _ (T_NormalWord id [T_Literal _ str]) | str `elem` commonCommands -> + unless (str == "file") $ -- This would be confusing + warn id 2238 "Redirecting to/from command name instead of file. Did you want pipes/xargs (or quote to ignore)?" + _ -> return () return [] runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])