From 8f5f91f0418f106d90e2d525ae883964cf47e25c Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 17 Oct 2015 10:31:14 -0700 Subject: [PATCH] Warn about ]] with no corresponding [[. --- ShellCheck/Analytics.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index c2b5efc..3b539b4 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -216,6 +216,7 @@ nodeChecks = [ ,checkReadWithoutR ,checkExportedExpansions ,checkLoopVariableReassignment + ,checkTrailingBracket ] @@ -3574,5 +3575,32 @@ checkLoopVariableReassignment params token = _ _ _ -> return var _ -> fail "not loop" +prop_checkTrailingBracket1 = verify checkTrailingBracket "if -z n ]]; then true; fi " +prop_checkTrailingBracket2 = verifyNot checkTrailingBracket "if [[ -z n ]]; then true; fi " +prop_checkTrailingBracket3 = verify checkTrailingBracket "a || b ] && thing" +prop_checkTrailingBracket4 = verifyNot checkTrailingBracket "run [ foo ]" +prop_checkTrailingBracket5 = verifyNot checkTrailingBracket "run bar ']'" +checkTrailingBracket _ token = + case token of + T_SimpleCommand _ _ tokens@(_:_) -> check (last tokens) token + otherwise -> return () + where + check t command = + case t of + T_NormalWord id [T_Literal _ str] -> potentially $ do + guard $ str `elem` [ "]]", "]" ] + let opposite = invert str + parameters = oversimplify command + guard $ opposite `notElem` parameters + return $ warn id 2171 $ + "Found trailing " ++ str ++ " outside test. Missing " ++ opposite ++ "?" + otherwise -> return () + invert s = + case s of + "]]" -> "[[" + "]" -> "[" + x -> x + + return [] runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])