From 371dcdda3aa7c1a2af5344273424d02c9c4b4ad7 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 16 Sep 2017 10:26:28 -0700 Subject: [PATCH] Warn about missing default case for getopts. --- ShellCheck/Checks/Commands.hs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ShellCheck/Checks/Commands.hs b/ShellCheck/Checks/Commands.hs index f2220d4..4322ef0 100644 --- a/ShellCheck/Checks/Commands.hs +++ b/ShellCheck/Checks/Commands.hs @@ -715,7 +715,7 @@ checkDeprecatedFgrep = CommandCheck (Basename "fgrep") $ prop_checkWhileGetoptsCase1 = verify checkWhileGetoptsCase "while getopts 'a:b' x; do case $x in a) foo;; esac; done" prop_checkWhileGetoptsCase2 = verify checkWhileGetoptsCase "while getopts 'a:' x; do case $x in a) foo;; b) bar;; esac; done" -prop_checkWhileGetoptsCase3 = verifyNot checkWhileGetoptsCase "while getopts 'a:b' x; do case $x in a) foo;; b) bar;; esac; done" +prop_checkWhileGetoptsCase3 = verifyNot checkWhileGetoptsCase "while getopts 'a:b' x; do case $x in a) foo;; b) bar;; *) :;esac; done" prop_checkWhileGetoptsCase4 = verifyNot checkWhileGetoptsCase "while getopts 'a:123' x; do case $x in a) foo;; [0-9]) bar;; esac; done" prop_checkWhileGetoptsCase5 = verifyNot checkWhileGetoptsCase "while getopts 'a:' x; do case $x in a) foo;; \\?) bar;; *) baz;; esac; done" checkWhileGetoptsCase = CommandCheck (Exactly "getopts") f @@ -732,9 +732,12 @@ checkWhileGetoptsCase = CommandCheck (Exactly "getopts") f check :: Id -> [String] -> Token -> Analysis check optId opts (T_CaseExpression id _ list) = do - unless (Nothing `Map.member` handledMap) $ + unless (Nothing `Map.member` handledMap) $ do mapM_ (warnUnhandled optId id) $ catMaybes $ Map.keys notHandled + unless (any (`Map.member` handledMap) [Just "*",Just "?"]) $ + warn id 2220 "Invalid flags are not handled. Add a *) case." + mapM_ warnRedundant $ Map.toList notRequested where @@ -763,6 +766,7 @@ checkWhileGetoptsCase = CommandCheck (Exactly "getopts") f case t of T_Glob _ ('[':c:']':[]) -> return [c] T_Glob _ "*" -> return "*" + T_Glob _ "?" -> return "?" _ -> Nothing whileLoop t =