Don't trigger SC2140 on ${x+"a" "b"} (fixes #2265)

This commit is contained in:
Vidar Holen 2021-09-18 18:49:58 -07:00
parent 8012f6761d
commit b044f5b23a
1 changed files with 6 additions and 4 deletions

View File

@ -1763,6 +1763,7 @@ prop_checkInexplicablyUnquoted6 = verifyNot checkInexplicablyUnquoted "\"$dir\"s
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'"
prop_checkInexplicablyUnquoted9 = verifyNot checkInexplicablyUnquoted "[[ $x =~ \"foo\"(\"bar\"|\"baz\") ]]" prop_checkInexplicablyUnquoted9 = verifyNot checkInexplicablyUnquoted "[[ $x =~ \"foo\"(\"bar\"|\"baz\") ]]"
prop_checkInexplicablyUnquoted10 = verifyNot checkInexplicablyUnquoted "cmd ${x+--name=\"$x\" --output=\"$x.out\"}"
checkInexplicablyUnquoted params (T_NormalWord id tokens) = mapM_ check (tails tokens) 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:_)
@ -1774,19 +1775,20 @@ checkInexplicablyUnquoted params (T_NormalWord id tokens) = mapM_ check (tails t
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
| not (quotesSingleThing a && quotesSingleThing b || isRegex (getPath (parentMap params) trapped)) -> | not (quotesSingleThing a && quotesSingleThing b || isSpecial (getPath (parentMap params) trapped)) ->
warnAboutLiteral id warnAboutLiteral id
_ -> return () _ -> return ()
check _ = return () check _ = return ()
-- Regexes for [[ .. =~ re ]] are parsed with metacharacters like ()| as unquoted -- Regexes for [[ .. =~ re ]] are parsed with metacharacters like ()| as unquoted
-- literals, so avoid overtriggering on these. -- literals. The same is true for ${x+"foo" "bar"}. Avoid overtriggering on these.
isRegex t = isSpecial t =
case t of case t of
(T_Redirecting {} : _) -> False (T_Redirecting {} : _) -> False
T_DollarBraced {} : _ -> True
(a:(TC_Binary _ _ "=~" lhs rhs):rest) -> getId a == getId rhs (a:(TC_Binary _ _ "=~" lhs rhs):rest) -> getId a == getId rhs
_:rest -> isRegex rest _:rest -> isSpecial rest
_ -> False _ -> 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",