Account for array index in SC2154 ${var:?} (fixes #1166)

This commit is contained in:
Vidar Holen 2018-04-28 12:09:54 -07:00
parent 7ec2fa2d3e
commit bb4ce86fab
1 changed files with 4 additions and 1 deletions

View File

@ -1884,6 +1884,7 @@ prop_checkUnassignedReferences31= verifyNotTree checkUnassignedReferences "X=1;
prop_checkUnassignedReferences32= verifyNotTree checkUnassignedReferences "if [[ -v \"foo[1]\" ]]; then echo ${foo[@]}; fi" prop_checkUnassignedReferences32= verifyNotTree checkUnassignedReferences "if [[ -v \"foo[1]\" ]]; then echo ${foo[@]}; fi"
prop_checkUnassignedReferences33= verifyNotTree checkUnassignedReferences "f() { local -A foo; echo \"${foo[@]}\"; }" prop_checkUnassignedReferences33= verifyNotTree checkUnassignedReferences "f() { local -A foo; echo \"${foo[@]}\"; }"
prop_checkUnassignedReferences34= verifyNotTree checkUnassignedReferences "declare -A foo; (( foo[bar] ))" prop_checkUnassignedReferences34= verifyNotTree checkUnassignedReferences "declare -A foo; (( foo[bar] ))"
prop_checkUnassignedReferences35= verifyNotTree checkUnassignedReferences "echo ${arr[foo-bar]:?fail}"
checkUnassignedReferences params t = warnings checkUnassignedReferences params t = warnings
where where
(readMap, writeMap) = execState (mapM tally $ variableFlow params) (Map.empty, Map.empty) (readMap, writeMap) = execState (mapM tally $ variableFlow params) (Map.empty, Map.empty)
@ -1943,11 +1944,13 @@ checkUnassignedReferences params t = warnings
isArray _ = False isArray _ = False
isGuarded (T_DollarBraced _ v) = isGuarded (T_DollarBraced _ v) =
any (`isPrefixOf` rest) ["-", ":-", "?", ":?"] rest `matches` guardRegex
where where
name = concat $ oversimplify v name = concat $ oversimplify v
rest = dropWhile isVariableChar $ dropWhile (`elem` "#!") name rest = dropWhile isVariableChar $ dropWhile (`elem` "#!") name
isGuarded _ = False isGuarded _ = False
-- :? or :- with optional array index and colon
guardRegex = mkRegex "^(\\[.*\\])?:?[-?]"
match var candidate = match var candidate =
if var /= candidate && map toLower var == map toLower candidate if var /= candidate && map toLower var == map toLower candidate