Count [[ -v foo ]] (and -R) as a variable reference

This commit is contained in:
Vidar Holen 2015-02-13 20:10:18 -08:00
parent cde3ba8769
commit 2fe117728d

View file

@ -2119,13 +2119,12 @@ getReferencedVariables t =
T_DollarBraced id l -> let str = bracedString l in T_DollarBraced id l -> let str = bracedString l in
(t, t, getBracedReference str) : (t, t, getBracedReference str) :
map (\x -> (l, l, x)) (getIndexReferences str) map (\x -> (l, l, x)) (getIndexReferences str)
TA_Expansion id _ -> maybeToList $ do TA_Expansion id _ -> getIfReference t t
str <- getLiteralStringExt literalizer t
guard . not $ null str
when (isDigit $ head str) $ fail "is a number"
return (t, t, getBracedReference str)
T_Assignment id mode str _ word -> T_Assignment id mode str _ word ->
[(t, t, str) | mode == Append] ++ specialReferences str t word [(t, t, str) | mode == Append] ++ specialReferences str t word
TC_Unary id _ "-v" token -> getIfReference t token
TC_Unary id _ "-R" token -> getIfReference t token
x -> getReferencedVariableCommand x x -> getReferencedVariableCommand x
where where
-- Try to reduce false positives for unused vars only referenced from evaluated vars -- Try to reduce false positives for unused vars only referenced from evaluated vars
@ -2142,6 +2141,12 @@ getReferencedVariables t =
literalizer (TA_Index {}) = return "" -- x[0] becomes a reference of x literalizer (TA_Index {}) = return "" -- x[0] becomes a reference of x
literalizer _ = Nothing literalizer _ = Nothing
getIfReference context token = maybeToList $ do
str <- getLiteralStringExt literalizer token
guard . not $ null str
when (isDigit $ head str) $ fail "is a number"
return (context, token, getBracedReference str)
-- Try to get referenced variables from a literal string like "$foo" -- Try to get referenced variables from a literal string like "$foo"
-- Ignores tons of cases like arithmetic evaluation and array indices. -- Ignores tons of cases like arithmetic evaluation and array indices.
prop_getVariablesFromLiteral1 = prop_getVariablesFromLiteral1 =
@ -2422,6 +2427,8 @@ prop_checkUnused18= verifyNotTree checkUnusedAssignments "a=1; arr=( [$a]=42 );
prop_checkUnused19= verifyNotTree checkUnusedAssignments "a=1; let b=a+1; echo $b" prop_checkUnused19= verifyNotTree checkUnusedAssignments "a=1; let b=a+1; echo $b"
prop_checkUnused20= verifyNotTree checkUnusedAssignments "a=1; PS1='$a'" prop_checkUnused20= verifyNotTree checkUnusedAssignments "a=1; PS1='$a'"
prop_checkUnused21= verifyNotTree checkUnusedAssignments "a=1; trap 'echo $a' INT" prop_checkUnused21= verifyNotTree checkUnusedAssignments "a=1; trap 'echo $a' INT"
prop_checkUnused22= verifyNotTree checkUnusedAssignments "a=1; [ -v a ]"
prop_checkUnused23= verifyNotTree checkUnusedAssignments "a=1; [ -R a ]"
checkUnusedAssignments params t = execWriter (mapM_ warnFor unused) checkUnusedAssignments params t = execWriter (mapM_ warnFor unused)
where where
flow = variableFlow params flow = variableFlow params