Don't warn about "a"b"c" in =~ regex (fixes #1565)

This commit is contained in:
Vidar Holen 2019-05-04 12:18:45 -07:00
parent e2e65e1350
commit 37b24cc129
1 changed files with 12 additions and 2 deletions

View File

@ -1424,7 +1424,8 @@ prop_checkInexplicablyUnquoted5 = verifyNot checkInexplicablyUnquoted "\"$dir\"/
prop_checkInexplicablyUnquoted6 = verifyNot checkInexplicablyUnquoted "\"$dir\"some_stuff\"$file\"" prop_checkInexplicablyUnquoted6 = verifyNot checkInexplicablyUnquoted "\"$dir\"some_stuff\"$file\""
prop_checkInexplicablyUnquoted7 = verifyNot checkInexplicablyUnquoted "${dir/\"foo\"/\"bar\"}" prop_checkInexplicablyUnquoted7 = verifyNot checkInexplicablyUnquoted "${dir/\"foo\"/\"bar\"}"
prop_checkInexplicablyUnquoted8 = verifyNot checkInexplicablyUnquoted " 'foo'\\\n 'bar'" prop_checkInexplicablyUnquoted8 = verifyNot checkInexplicablyUnquoted " 'foo'\\\n 'bar'"
checkInexplicablyUnquoted _ (T_NormalWord id tokens) = mapM_ check (tails tokens) prop_checkInexplicablyUnquoted9 = verifyNot checkInexplicablyUnquoted "[[ $x =~ \"foo\"(\"bar\"|\"baz\") ]]"
checkInexplicablyUnquoted params (T_NormalWord id tokens) = mapM_ check (tails tokens)
where where
check (T_SingleQuoted _ _:T_Literal id str:_) check (T_SingleQuoted _ _:T_Literal id str:_)
| not (null str) && all isAlphaNum str = | not (null str) && all isAlphaNum str =
@ -1435,12 +1436,21 @@ checkInexplicablyUnquoted _ (T_NormalWord id tokens) = mapM_ check (tails tokens
T_DollarExpansion id _ -> warnAboutExpansion id T_DollarExpansion id _ -> warnAboutExpansion id
T_DollarBraced id _ -> warnAboutExpansion id T_DollarBraced id _ -> warnAboutExpansion id
T_Literal id s -> T_Literal id s ->
unless (quotesSingleThing a && quotesSingleThing b) $ unless (quotesSingleThing a && quotesSingleThing b || isRegex (getPath (parentMap params) trapped)) $
warnAboutLiteral id warnAboutLiteral id
_ -> return () _ -> return ()
check _ = return () check _ = return ()
-- Regexes for [[ .. =~ re ]] are parsed with metacharacters like ()| as unquoted
-- literals, so avoid overtriggering on these.
isRegex t =
case t of
(T_Redirecting {} : _) -> False
(a:(TC_Binary _ _ "=~" lhs rhs):rest) -> getId a == getId rhs
_:rest -> isRegex rest
_ -> False
-- If the surrounding quotes quote single things, like "$foo"_and_then_some_"$stuff", -- If the surrounding quotes quote single things, like "$foo"_and_then_some_"$stuff",
-- the quotes were probably intentional and harmless. -- the quotes were probably intentional and harmless.
quotesSingleThing x = case x of quotesSingleThing x = case x of