Re-add warnings about 'declare var = value' (fixes #2279)

This commit is contained in:
Vidar Holen 2021-07-24 13:07:05 -07:00
parent 8be60028ef
commit 9eb63c97e6
2 changed files with 32 additions and 0 deletions

View File

@ -4,6 +4,7 @@
- SC2289: Warn when command name contains tabs or linefeeds - SC2289: Warn when command name contains tabs or linefeeds
### Fixed ### Fixed
- SC2290: Warn about misused = in declare & co, which were not caught by SC2270+
### Changed ### Changed
- SC2048: Warning about $\* now also applies to ${array[\*]} - SC2048: Warning about $\* now also applies to ${array[\*]}

View File

@ -95,6 +95,11 @@ commandChecks = [
,checkSourceArgs ,checkSourceArgs
,checkChmodDashr ,checkChmodDashr
,checkXargsDashi ,checkXargsDashi
,checkArgComparison "local"
,checkArgComparison "declare"
,checkArgComparison "export"
,checkArgComparison "readonly"
,checkArgComparison "typeset"
] ]
optionalChecks = map fst optionalCommandChecks optionalChecks = map fst optionalCommandChecks
@ -1143,5 +1148,31 @@ checkXargsDashi = CommandCheck (Basename "xargs") f
return $ info (getId option) 2267 "GNU xargs -i is deprecated in favor of -I{}" return $ info (getId option) 2267 "GNU xargs -i is deprecated in favor of -I{}"
parseOpts = getBsdOpts "0oprtxadR:S:J:L:l:n:P:s:e:E:i:I:" parseOpts = getBsdOpts "0oprtxadR:S:J:L:l:n:P:s:e:E:i:I:"
prop_checkArgComparison1 = verify (checkArgComparison "declare") "declare a = b"
prop_checkArgComparison2 = verify (checkArgComparison "declare") "declare a =b"
prop_checkArgComparison3 = verifyNot (checkArgComparison "declare") "declare a=b"
prop_checkArgComparison4 = verify (checkArgComparison "export") "export a +=b"
prop_checkArgComparison7 = verifyNot (checkArgComparison "declare") "declare -a +i foo"
-- This mirrors checkSecondArgIsComparison but for arguments to local/readonly/declare/export
checkArgComparison str = CommandCheck (Exactly str) wordsWithEqual
where
wordsWithEqual t = mapM_ check $ drop 1 $ arguments t
check arg = sequence_ $ do
str <- getLeadingUnquotedString arg
case str of
'=':_ ->
return $ err (headId arg) 2290 $
"Remove spaces around = to assign."
'+':'=':_ ->
return $ err (headId arg) 2290 $
"Remove spaces around += to append."
_ -> Nothing
headId t =
case t of
T_NormalWord _ (x:_) -> getId x
_ -> getId t
return [] return []
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |]) runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])