From 4fb1080809886f6beaae3818dcdb56a0be3d4de1 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 15 Apr 2017 17:20:33 -0700 Subject: [PATCH] Warn when redirecting to a literal integer. --- ShellCheck/Analytics.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index 10e6f6e..13b522a 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -161,6 +161,7 @@ nodeChecks = [ ,checkUnmatchableCases ,checkSubshellAsTest ,checkSplittingInArrays + ,checkRedirectionToNumber ] @@ -2796,5 +2797,16 @@ checkSplittingInArrays params t = else "Prefer mapfile or read -a to split command output (or quote to avoid splitting)." +prop_checkRedirectionToNumber1 = verify checkRedirectionToNumber "( 1 > 2 )" +prop_checkRedirectionToNumber2 = verify checkRedirectionToNumber "foo 1>2" +prop_checkRedirectionToNumber3 = verifyNot checkRedirectionToNumber "echo foo > '2'" +prop_checkRedirectionToNumber4 = verifyNot checkRedirectionToNumber "foo 1>&2" +checkRedirectionToNumber _ t = case t of + T_IoFile id _ word -> potentially $ do + file <- getUnquotedLiteral word + guard $ all isDigit file + return $ warn id 2210 "This is a file redirection. Was it supposed to be a comparison or fd operation?" + _ -> return () + return [] runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])