Skip to content

Commit b044f5b

Browse files
committed
Don't trigger SC2140 on ${x+"a" "b"} (fixes koalaman#2265)
1 parent 8012f67 commit b044f5b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/ShellCheck/Analytics.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,7 @@ prop_checkInexplicablyUnquoted6 = verifyNot checkInexplicablyUnquoted "\"$dir\"s
17631763
prop_checkInexplicablyUnquoted7 = verifyNot checkInexplicablyUnquoted "${dir/\"foo\"/\"bar\"}"
17641764
prop_checkInexplicablyUnquoted8 = verifyNot checkInexplicablyUnquoted " 'foo'\\\n 'bar'"
17651765
prop_checkInexplicablyUnquoted9 = verifyNot checkInexplicablyUnquoted "[[ $x =~ \"foo\"(\"bar\"|\"baz\") ]]"
1766+
prop_checkInexplicablyUnquoted10 = verifyNot checkInexplicablyUnquoted "cmd ${x+--name=\"$x\" --output=\"$x.out\"}"
17661767
checkInexplicablyUnquoted params (T_NormalWord id tokens) = mapM_ check (tails tokens)
17671768
where
17681769
check (T_SingleQuoted _ _:T_Literal id str:_)
@@ -1774,19 +1775,20 @@ checkInexplicablyUnquoted params (T_NormalWord id tokens) = mapM_ check (tails t
17741775
T_DollarExpansion id _ -> warnAboutExpansion id
17751776
T_DollarBraced id _ _ -> warnAboutExpansion id
17761777
T_Literal id s
1777-
| not (quotesSingleThing a && quotesSingleThing b || isRegex (getPath (parentMap params) trapped)) ->
1778+
| not (quotesSingleThing a && quotesSingleThing b || isSpecial (getPath (parentMap params) trapped)) ->
17781779
warnAboutLiteral id
17791780
_ -> return ()
17801781

17811782
check _ = return ()
17821783

17831784
-- Regexes for [[ .. =~ re ]] are parsed with metacharacters like ()| as unquoted
1784-
-- literals, so avoid overtriggering on these.
1785-
isRegex t =
1785+
-- literals. The same is true for ${x+"foo" "bar"}. Avoid overtriggering on these.
1786+
isSpecial t =
17861787
case t of
17871788
(T_Redirecting {} : _) -> False
1789+
T_DollarBraced {} : _ -> True
17881790
(a:(TC_Binary _ _ "=~" lhs rhs):rest) -> getId a == getId rhs
1789-
_:rest -> isRegex rest
1791+
_:rest -> isSpecial rest
17901792
_ -> False
17911793

17921794
-- If the surrounding quotes quote single things, like "$foo"_and_then_some_"$stuff",

0 commit comments

Comments
 (0)