Warn when a variable is assigned to itself

This commit is contained in:
Vidar Holen 2020-12-11 20:28:36 -08:00
parent 8e332ce879
commit 6ba1af0898
1 changed files with 22 additions and 0 deletions

View File

@ -193,6 +193,7 @@ nodeChecks = [
,checkModifiedArithmeticInRedirection ,checkModifiedArithmeticInRedirection
,checkBlatantRecursion ,checkBlatantRecursion
,checkBadTestAndOr ,checkBadTestAndOr
,checkAssignToSelf
] ]
optionalChecks = map fst optionalTreeChecks optionalChecks = map fst optionalTreeChecks
@ -3974,5 +3975,26 @@ checkComparisonWithLeadingX params t =
return $ replaceStart id params 2 "'" return $ replaceStart id params 2 "'"
_ -> Nothing _ -> Nothing
prop_checkAssignToSelf1 = verify checkAssignToSelf "x=$x"
prop_checkAssignToSelf2 = verify checkAssignToSelf "x=${x}"
prop_checkAssignToSelf3 = verify checkAssignToSelf "x=\"$x\""
prop_checkAssignToSelf4 = verifyNot checkAssignToSelf "x=$x mycmd"
checkAssignToSelf _ t =
case t of
T_SimpleCommand _ vars [] -> mapM_ check vars
_ -> return ()
where
check t =
case t of
T_Assignment id Assign name [] t ->
case getWordParts t of
[T_DollarBraced _ _ b] -> do
when (Just name == getLiteralString b) $
msg id
_ -> return ()
_ -> return ()
msg id = info id 2269 "This variable is assigned to itself, so the assignment does nothing."
return [] return []
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |]) runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])