Add check for ambiguous nullary test
Given an input like `if [[ $(a) ]]; then ...`, this is a implicit `-n` test, so it works like `if [[ -n $(a) ]]; then ...`. Users might confuse this for a check for the exit code of the command a, which should be tested with: if a; then ... We warn the user to be more explicity and specifity the `-n`. Fixes #1416
This commit is contained in:
parent
08ca1ee6e9
commit
95a8cf93c9
|
@ -170,6 +170,7 @@ nodeChecks = [
|
||||||
,checkSubshelledTests
|
,checkSubshelledTests
|
||||||
,checkInvertedStringTest
|
,checkInvertedStringTest
|
||||||
,checkRedirectionToCommand
|
,checkRedirectionToCommand
|
||||||
|
,checkNullaryExpansionTest
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -3096,5 +3097,21 @@ checkRedirectionToCommand _ t =
|
||||||
warn id 2238 "Redirecting to/from command name instead of file. Did you want pipes/xargs (or quote to ignore)?"
|
warn id 2238 "Redirecting to/from command name instead of file. Did you want pipes/xargs (or quote to ignore)?"
|
||||||
_ -> return ()
|
_ -> return ()
|
||||||
|
|
||||||
|
prop_checkNullaryExpansionTest1 = verify checkNullaryExpansionTest "[[ $(a) ]]"
|
||||||
|
prop_checkNullaryExpansionTest2 = verify checkNullaryExpansionTest "[[ $a ]]"
|
||||||
|
prop_checkNullaryExpansionTest3 = verifyNot checkNullaryExpansionTest "[[ $a=1 ]]"
|
||||||
|
prop_checkNullaryExpansionTest4 = verifyNot checkNullaryExpansionTest "[[ -n $(a) ]]"
|
||||||
|
checkNullaryExpansionTest _ t =
|
||||||
|
case t of
|
||||||
|
TC_Nullary _ _ (T_NormalWord id [T_DollarExpansion _ [T_Pipeline _ [] [x]]]) ->
|
||||||
|
when (isJust (getCommand x)) $
|
||||||
|
style id 2243 (
|
||||||
|
"To check for the exit code of a command, remove the conditional expression, e.g. if foo; ...")
|
||||||
|
TC_Nullary _ _ (T_NormalWord id [t2]) ->
|
||||||
|
when ((not . isConstant) t2) $
|
||||||
|
style id 2244 (
|
||||||
|
"Use -n to check for null string, e.g. [[ -n $var ]].")
|
||||||
|
_ -> return ()
|
||||||
|
|
||||||
return []
|
return []
|
||||||
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
|
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
|
||||||
|
|
Loading…
Reference in New Issue