SC2257: Warn when changing arithmetic variables in redirections

This commit is contained in:
Vidar Holen 2020-02-17 18:02:23 -08:00
parent 4c9210af79
commit 7b998239af
2 changed files with 37 additions and 0 deletions

View File

@ -10,6 +10,7 @@
- SC2254: Suggest quoting expansions in case statements - SC2254: Suggest quoting expansions in case statements
- SC2255: Suggest using `$((..))` in `[ 2*3 -eq 6 ]` - SC2255: Suggest using `$((..))` in `[ 2*3 -eq 6 ]`
- SC2256: Warn about translated strings that are known variables - SC2256: Warn about translated strings that are known variables
- SC2257: Warn about arithmetic mutation in redirections
### Changed ### Changed
- SC2230: 'command -v' suggestion is now off by default (-i deprecate-which) - SC2230: 'command -v' suggestion is now off by default (-i deprecate-which)

View File

@ -189,6 +189,7 @@ nodeChecks = [
,checkDollarQuoteParen ,checkDollarQuoteParen
,checkUselessBang ,checkUselessBang
,checkTranslatedStringVariable ,checkTranslatedStringVariable
,checkModifiedArithmeticInRedirection
] ]
optionalChecks = map fst optionalTreeChecks optionalChecks = map fst optionalTreeChecks
@ -3530,5 +3531,40 @@ checkUselessBang params t = when (hasSetE params) $ mapM_ check (getNonReturning
x:rest -> x : dropLast rest x:rest -> x : dropLast rest
_ -> [] _ -> []
prop_checkModifiedArithmeticInRedirection1 = verify checkModifiedArithmeticInRedirection "ls > $((i++))"
prop_checkModifiedArithmeticInRedirection2 = verify checkModifiedArithmeticInRedirection "cat < \"foo$((i++)).txt\""
prop_checkModifiedArithmeticInRedirection3 = verifyNot checkModifiedArithmeticInRedirection "while true; do true; done > $((i++))"
prop_checkModifiedArithmeticInRedirection4 = verify checkModifiedArithmeticInRedirection "cat <<< $((i++))"
prop_checkModifiedArithmeticInRedirection5 = verify checkModifiedArithmeticInRedirection "cat << foo\n$((i++))\nfoo\n"
checkModifiedArithmeticInRedirection _ t =
case t of
T_Redirecting _ redirs (T_SimpleCommand _ _ (_:_)) -> mapM_ checkRedirs redirs
_ -> return ()
where
checkRedirs t =
case t of
T_FdRedirect _ _ (T_IoFile _ _ word) ->
mapM_ checkArithmetic $ getWordParts word
T_FdRedirect _ _ (T_HereString _ word) ->
mapM_ checkArithmetic $ getWordParts word
T_FdRedirect _ _ (T_HereDoc _ _ _ _ list) ->
mapM_ checkArithmetic list
_ -> return ()
checkArithmetic t =
case t of
T_DollarArithmetic _ x -> checkModifying x
_ -> return ()
checkModifying t =
case t of
TA_Sequence _ list -> mapM_ checkModifying list
TA_Unary id s _ | s `elem` ["|++", "++|", "|--", "--|"] -> warnFor id
TA_Assignment id _ _ _ -> warnFor id
TA_Binary _ _ x y -> mapM_ checkModifying [x ,y]
TA_Trinary _ x y z -> mapM_ checkModifying [x, y, z]
_ -> return ()
warnFor id =
warn id 2257 "Arithmetic modifications in command redirections may be discarded. Do them separately."
return [] return []
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |]) runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])