From 4a2b2c73968a7877c13c5aa4295b0f7dd0bc86e9 Mon Sep 17 00:00:00 2001 From: Gandalf- Date: Sun, 6 Jan 2019 17:42:17 -0800 Subject: [PATCH] Issue 1404 grep glob false positives https://github.com/koalaman/shellcheck/issues/1404 Some grep flags support globs; these are now all checked prevent false positives. --- src/ShellCheck/Checks/Commands.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ShellCheck/Checks/Commands.hs b/src/ShellCheck/Checks/Commands.hs index f4ead5b..e8ee853 100644 --- a/src/ShellCheck/Checks/Commands.hs +++ b/src/ShellCheck/Checks/Commands.hs @@ -208,6 +208,10 @@ prop_checkGrepRe12= verifyNot checkGrepRe "grep -F 'Foo*' file" prop_checkGrepRe13= verifyNot checkGrepRe "grep -- -foo bar*" prop_checkGrepRe14= verifyNot checkGrepRe "grep -e -foo bar*" prop_checkGrepRe15= verifyNot checkGrepRe "grep --regex -foo bar*" +prop_checkGrepRe16= verifyNot checkGrepRe "grep --include 'Foo*' file" +prop_checkGrepRe17= verifyNot checkGrepRe "grep --exclude 'Foo*' file" +prop_checkGrepRe18= verifyNot checkGrepRe "grep --exclude-dir 'Foo*' file" +prop_checkGrepRe19= verify checkGrepRe "grep -- 'Foo*' file" checkGrepRe = CommandCheck (Basename "grep") check where check cmd = f cmd (arguments cmd) @@ -230,7 +234,7 @@ checkGrepRe = CommandCheck (Basename "grep") check where when (isGlob re) $ warn (getId re) 2062 "Quote the grep pattern so the shell won't interpret it." - unless (cmd `hasFlag` "F") $ do + unless (any (hasFlag cmd) grepGlobFlags) $ do let string = concat $ oversimplify re if isConfusedGlobRegex string then warn (getId re) 2063 "Grep uses regex, but this looks like a glob." @@ -238,6 +242,8 @@ checkGrepRe = CommandCheck (Basename "grep") check where char <- getSuspiciousRegexWildcard string return $ info (getId re) 2022 $ "Note that unlike globs, " ++ [char] ++ "* here matches '" ++ [char, char, char] ++ "' but not '" ++ wordStartingWith char ++ "'." + where + grepGlobFlags = ["F", "include", "exclude", "exclude-dir"] wordStartingWith c = head . filter ([c] `isPrefixOf`) $ candidates