From 128d5d6013c88496c7409c50bad70beefcc5d5a2 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 21 Jan 2017 16:20:55 -0800 Subject: [PATCH] Don't warn about grep pattern issues when using -F. --- ShellCheck/ASTLib.hs | 5 ++++- ShellCheck/Checks/Commands.hs | 26 +++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/ShellCheck/ASTLib.hs b/ShellCheck/ASTLib.hs index 06fb14b..1a25a84 100644 --- a/ShellCheck/ASTLib.hs +++ b/ShellCheck/ASTLib.hs @@ -93,7 +93,7 @@ oversimplify token = (T_Annotation _ _ s) -> oversimplify s -- Workaround for let "foo = bar" parsing (TA_Sequence _ [TA_Expansion _ v]) -> concatMap oversimplify v - otherwise -> [] + _ -> [] -- Turn a SimpleCommand foo -avz --bar=baz into args "a", "v", "z", "bar", @@ -115,6 +115,9 @@ getAllFlags = getFlagsUntil (== "--") -- Get all flags in a BSD way, up until first non-flag argument or -- getLeadingFlags = getFlagsUntil (\x -> x == "--" || (not $ "-" `isPrefixOf` x)) +-- Check if a command has a flag. +hasFlag cmd str = str `elem` (map snd $ getAllFlags cmd) + -- Given a T_DollarBraced, return a simplified version of the string contents. bracedString (T_DollarBraced _ l) = concat $ oversimplify l diff --git a/ShellCheck/Checks/Commands.hs b/ShellCheck/Checks/Commands.hs index b809eba..d1d8268 100644 --- a/ShellCheck/Checks/Commands.hs +++ b/ShellCheck/Checks/Commands.hs @@ -194,23 +194,27 @@ prop_checkGrepRe8 = verify checkGrepRe "ls | grep foo*.jpg" prop_checkGrepRe9 = verifyNot checkGrepRe "grep '[0-9]*' file" prop_checkGrepRe10= verifyNot checkGrepRe "grep '^aa*' file" prop_checkGrepRe11= verifyNot checkGrepRe "grep --include=*.png foo" +prop_checkGrepRe12= verifyNot checkGrepRe "grep -F 'Foo*' file" -checkGrepRe = CommandCheck (Basename "grep") (f . arguments) where +checkGrepRe = CommandCheck (Basename "grep") check where + check cmd = f cmd (arguments cmd) -- --regex=*(extglob) doesn't work. Fixme? skippable (Just s) = not ("--regex=" `isPrefixOf` s) && "-" `isPrefixOf` s skippable _ = False - f [] = return () - f (x:r) | skippable (getLiteralStringExt (const $ return "_") x) = f r - f (re:_) = do + f _ [] = return () + f cmd (x:r) | skippable (getLiteralStringExt (const $ return "_") x) = f cmd r + f cmd (re:_) = do when (isGlob re) $ warn (getId re) 2062 "Quote the grep pattern so the shell won't interpret it." - let string = concat $ oversimplify re - if isConfusedGlobRegex string then - warn (getId re) 2063 "Grep uses regex, but this looks like a glob." - else potentially $ do - char <- getSuspiciousRegexWildcard string - return $ info (getId re) 2022 $ - "Note that unlike globs, " ++ [char] ++ "* here matches '" ++ [char, char, char] ++ "' but not '" ++ wordStartingWith char ++ "'." + + unless (cmd `hasFlag` "F") $ do + let string = concat $ oversimplify re + if isConfusedGlobRegex string then + warn (getId re) 2063 "Grep uses regex, but this looks like a glob." + else potentially $ do + char <- getSuspiciousRegexWildcard string + return $ info (getId re) 2022 $ + "Note that unlike globs, " ++ [char] ++ "* here matches '" ++ [char, char, char] ++ "' but not '" ++ wordStartingWith char ++ "'." wordStartingWith c = head . filter ([c] `isPrefixOf`) $ candidates