Skip to content

Commit 7fc992d

Browse files
committed
Suppress SC2119/SC2120 for ${1:-default} (fixes #2023)
1 parent c553288 commit 7fc992d

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/ShellCheck/Analytics.hs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,8 @@ prop_checkUnpassedInFunctions11 = verifyNotTree checkUnpassedInFunctions "foo()
28252825
prop_checkUnpassedInFunctions12 = verifyNotTree checkUnpassedInFunctions "foo() { echo ${!var*}; }; foo;"
28262826
prop_checkUnpassedInFunctions13 = verifyNotTree checkUnpassedInFunctions "# shellcheck disable=SC2120\nfoo() { echo $1; }\nfoo\n"
28272827
prop_checkUnpassedInFunctions14 = verifyTree checkUnpassedInFunctions "foo() { echo $#; }; foo"
2828+
prop_checkUnpassedInFunctions15 = verifyNotTree checkUnpassedInFunctions "foo() { echo ${1-x}; }; foo"
2829+
prop_checkUnpassedInFunctions16 = verifyNotTree checkUnpassedInFunctions "foo() { echo ${1:-x}; }; foo"
28282830
checkUnpassedInFunctions params root =
28292831
execWriter $ mapM_ warnForGroup referenceGroups
28302832
where
@@ -2841,9 +2843,10 @@ checkUnpassedInFunctions params root =
28412843
case x of
28422844
Assignment (_, _, str, _) -> isPositional str
28432845
_ -> False
2846+
28442847
isPositionalReference function x =
28452848
case x of
2846-
Reference (_, t, str) -> isPositional str && t `isDirectChildOf` function
2849+
Reference (_, t, str) -> isPositional str && t `isDirectChildOf` function && not (hasDefaultValue t)
28472850
_ -> False
28482851

28492852
isDirectChildOf child parent = fromMaybe False $ do
@@ -2857,6 +2860,7 @@ checkUnpassedInFunctions params root =
28572860
referenceList :: [(String, Bool, Token)]
28582861
referenceList = execWriter $
28592862
doAnalysis (sequence_ . checkCommand) root
2863+
28602864
checkCommand :: Token -> Maybe (Writer [(String, Bool, Token)] ())
28612865
checkCommand t@(T_SimpleCommand _ _ (cmd:args)) = do
28622866
str <- getLiteralString cmd
@@ -2867,6 +2871,21 @@ checkUnpassedInFunctions params root =
28672871
isPositional str = str == "*" || str == "@" || str == "#"
28682872
|| (all isDigit str && str /= "0" && str /= "")
28692873

2874+
-- True if t is a variable that specifies a default value,
2875+
-- such as ${1-x} or ${1:-x}.
2876+
hasDefaultValue t =
2877+
case t of
2878+
T_DollarBraced _ True l ->
2879+
let str = concat $ oversimplify l
2880+
in isDefaultValueModifier $ getBracedModifier str
2881+
_ -> False
2882+
2883+
isDefaultValueModifier str =
2884+
case str of
2885+
'-':_ -> True
2886+
':':'-':_ -> True
2887+
_ -> False
2888+
28702889
isArgumentless (_, b, _) = b
28712890
referenceGroups = Map.elems $ foldr updateWith Map.empty referenceList
28722891
updateWith x@(name, _, _) = Map.insertWith (++) name [x]

0 commit comments

Comments
 (0)