Skip to content

Commit

Permalink
Don't trigger SC2140 on ${x+"a" "b"} (fixes koalaman#2265)
Browse files Browse the repository at this point in the history
  • Loading branch information
koalaman committed Sep 19, 2021
1 parent 8012f67 commit b044f5b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/ShellCheck/Analytics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,7 @@ prop_checkInexplicablyUnquoted6 = verifyNot checkInexplicablyUnquoted "\"$dir\"s
prop_checkInexplicablyUnquoted7 = verifyNot checkInexplicablyUnquoted "${dir/\"foo\"/\"bar\"}"
prop_checkInexplicablyUnquoted8 = verifyNot checkInexplicablyUnquoted " 'foo'\\\n 'bar'"
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)
where
check (T_SingleQuoted _ _:T_Literal id str:_)
Expand All @@ -1774,19 +1775,20 @@ checkInexplicablyUnquoted params (T_NormalWord id tokens) = mapM_ check (tails t
T_DollarExpansion id _ -> warnAboutExpansion id
T_DollarBraced id _ _ -> warnAboutExpansion id
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
_ -> return ()

check _ = return ()

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

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

0 comments on commit b044f5b

Please sign in to comment.