Consider variables in -z/-n tests to be checked

This commit is contained in:
Vidar Holen 2020-08-08 12:32:20 -07:00
parent 3fa5b7d3bd
commit 50067ddf94
3 changed files with 13 additions and 13 deletions

View File

@ -11,6 +11,7 @@
### Changed
- SC1090: A leading `$x/` or `$(x)/` is now treated as `./` when locating files
- SC2154: Variables appearing in -z/-n tests are no longer considered unassigned
## v0.7.1 - 2020-04-04

View File

@ -2231,7 +2231,6 @@ prop_checkUnassignedReferences40= verifyNotTree checkUnassignedReferences ": ${f
prop_checkUnassignedReferences41= verifyNotTree checkUnassignedReferences "mapfile -t files 123; echo \"${files[@]}\""
prop_checkUnassignedReferences42= verifyNotTree checkUnassignedReferences "mapfile files -t; echo \"${files[@]}\""
prop_checkUnassignedReferences43= verifyNotTree checkUnassignedReferences "mapfile --future files; echo \"${files[@]}\""
prop_checkUnassignedReferences_minusNPlain = verifyNotTree checkUnassignedReferences "if [ -n \"$x\" ]; then echo $x; fi"
prop_checkUnassignedReferences_minusZPlain = verifyNotTree checkUnassignedReferences "if [ -z \"$x\" ]; then echo \"\"; fi"
prop_checkUnassignedReferences_minusNBraced = verifyNotTree checkUnassignedReferences "if [ -n \"${x}\" ]; then echo $x; fi"
@ -2239,9 +2238,6 @@ prop_checkUnassignedReferences_minusZBraced = verifyNotTree checkUnassignedRefe
prop_checkUnassignedReferences_minusNDefault = verifyNotTree checkUnassignedReferences "if [ -n \"${x:-}\" ]; then echo $x; fi"
prop_checkUnassignedReferences_minusZDefault = verifyNotTree checkUnassignedReferences "if [ -z \"${x:-}\" ]; then echo \"\"; fi"
prop_checkUnassignedReferences_minusZInsteadOfN = verifyTree checkUnassignedReferences "if [ -z \"$x\" ]; then echo $x; fi"
prop_checkUnassignedReferences_minusZInsteadOfNBraced = verifyTree checkUnassignedReferences "if [ -z \"${x}\" ]; then echo $x; fi"
checkUnassignedReferences = checkUnassignedReferences' False
checkUnassignedReferences' includeGlobals params t = warnings
where

View File

@ -499,8 +499,9 @@ getModifiedVariables t =
guard . not . null $ str
return (t, token, str, DataString SourceChecked)
TC_Unary _ _ "-n" (T_NormalWord _ [T_DoubleQuoted _ [db@(T_DollarBraced _ _ l)]]) ->
[(t, t, getBracedReference (concat $ oversimplify l), DataString SourceChecked)]
TC_Unary _ _ "-n" token -> markAsChecked t token
TC_Unary _ _ "-z" token -> markAsChecked t token
TC_Nullary _ _ token -> markAsChecked t token
T_DollarBraced _ _ l -> maybeToList $ do
let string = concat $ oversimplify l
@ -519,6 +520,14 @@ getModifiedVariables t =
T_ForIn id str words _ -> [(t, t, str, DataString $ SourceFrom words)]
T_SelectIn id str words _ -> [(t, t, str, DataString $ SourceFrom words)]
_ -> []
where
markAsChecked place token = mapMaybe (f place) $ getWordParts token
f place t = case t of
T_DollarBraced _ _ l ->
let str = getBracedReference $ concat $ oversimplify l in do
guard $ isVariableName str
return (place, t, str, DataString SourceChecked)
_ -> Nothing
isClosingFileOp op =
case op of
@ -726,9 +735,7 @@ getOffsetReferences mods = fromMaybe [] $ do
getReferencedVariables parents t =
case t of
T_DollarBraced id _ l -> let str = concat $ oversimplify l in
if isMinusZTest t
then []
else (t, t, getBracedReference str) :
(t, t, getBracedReference str) :
map (\x -> (l, l, x)) (
getIndexReferences str
++ getOffsetReferences (getBracedModifier str))
@ -782,10 +789,6 @@ getReferencedVariables parents t =
this: TA_Assignment _ "=" lhs _ :_ -> lhs == t
_ -> False
isMinusZTest t = case getPath parents t of
_ : T_DoubleQuoted _ [_] : T_NormalWord _ [_] : TC_Unary _ SingleBracket "-z" _ : _ -> True
_ -> False
dataTypeFrom defaultType v = (case v of T_Array {} -> DataArray; _ -> defaultType) $ SourceFrom [v]