mirror of
https://github.com/koalaman/shellcheck
synced 2025-07-06 21:11:35 -07:00
SC2321: Warn about redundant $(()) in arr[$((i))]=x (ref: #1666)
This commit is contained in:
parent
d1d574c091
commit
30bb0e0093
2 changed files with 19 additions and 0 deletions
|
@ -206,6 +206,7 @@ nodeChecks = [
|
|||
,checkCommandIsUnreachable
|
||||
,checkSpacefulnessCfg
|
||||
,checkOverwrittenExitCode
|
||||
,checkUnnecessaryArithmeticExpansionIndex
|
||||
]
|
||||
|
||||
optionalChecks = map fst optionalTreeChecks
|
||||
|
@ -4913,5 +4914,22 @@ checkOverwrittenExitCode params t =
|
|||
_ -> False
|
||||
|
||||
|
||||
prop_checkUnnecessaryArithmeticExpansionIndex1 = verify checkUnnecessaryArithmeticExpansionIndex "a[$((1+1))]=n"
|
||||
prop_checkUnnecessaryArithmeticExpansionIndex2 = verifyNot checkUnnecessaryArithmeticExpansionIndex "a[1+1]=n"
|
||||
prop_checkUnnecessaryArithmeticExpansionIndex3 = verifyNot checkUnnecessaryArithmeticExpansionIndex "a[$(echo $((1+1)))]=n"
|
||||
checkUnnecessaryArithmeticExpansionIndex params t =
|
||||
case t of
|
||||
T_Assignment _ mode var [TA_Sequence _ [ TA_Expansion _ [expansion@(T_DollarArithmetic id _)]]] val ->
|
||||
styleWithFix id 2321 "Array indices are already arithmetic contexts. Prefer removing the $(( and ))." $ fix id
|
||||
_ -> return ()
|
||||
|
||||
where
|
||||
fix id =
|
||||
fixWith [
|
||||
replaceStart id params 3 "", -- Remove "$(("
|
||||
replaceEnd id params 2 "" -- Remove "))"
|
||||
]
|
||||
|
||||
|
||||
return []
|
||||
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue