Optional style warning about [ x$var = xval ]

This commit is contained in:
Vidar Holen 2020-10-19 18:25:18 -07:00
parent 256457c47a
commit 28d3279ba6
2 changed files with 43 additions and 0 deletions

View File

@ -7,6 +7,7 @@
- SC2264: Warn about wrapper functions that blatantly recurse - SC2264: Warn about wrapper functions that blatantly recurse
- SC2265/SC2266: Warn when using & or | with test statements - SC2265/SC2266: Warn when using & or | with test statements
- SC2267: Warn when using xargs -i instead of -I - SC2267: Warn when using xargs -i instead of -I
- Optional avoid-x-comparisons: Style warning SC2268 for `[ x$var = xval ]`
### Fixed ### Fixed
- SC1072/SC1073 now respond to disable annotations, though ignoring parse errors - SC1072/SC1073 now respond to disable annotations, though ignoring parse errors

View File

@ -240,6 +240,13 @@ optionalTreeChecks = [
cdPositive = "echo $VAR", cdPositive = "echo $VAR",
cdNegative = "VAR=hello; echo $VAR" cdNegative = "VAR=hello; echo $VAR"
}, checkUnassignedReferences' True) }, checkUnassignedReferences' True)
,(newCheckDescription {
cdName = "avoid-x-comparisons",
cdDescription = "Warn about 'x'-prefix in comparisons",
cdPositive = "[ \"x$var\" = xval ]",
cdNegative = "[ \"$var\" = val ]"
}, nodeChecksToTreeCheck [checkComparisonWithLeadingX])
] ]
optionalCheckMap :: Map.Map String (Parameters -> Token -> [TokenComment]) optionalCheckMap :: Map.Map String (Parameters -> Token -> [TokenComment])
@ -3926,6 +3933,41 @@ checkBadTestAndOr params t =
T_Annotation _ _ t -> isTest t T_Annotation _ _ t -> isTest t
_ -> False _ -> False
prop_checkComparisonWithLeadingX1 = verify checkComparisonWithLeadingX "[ x$foo = xlol ]"
prop_checkComparisonWithLeadingX2 = verify checkComparisonWithLeadingX "test x$foo = xlol"
prop_checkComparisonWithLeadingX3 = verifyNot checkComparisonWithLeadingX "[ $foo = xbar ]"
prop_checkComparisonWithLeadingX4 = verifyNot checkComparisonWithLeadingX "test $foo = xbar"
prop_checkComparisonWithLeadingX5 = verify checkComparisonWithLeadingX "[ \"x$foo\" = 'xlol' ]"
prop_checkComparisonWithLeadingX6 = verify checkComparisonWithLeadingX "[ x\"$foo\" = x'lol' ]"
checkComparisonWithLeadingX params t =
case t of
TC_Binary id typ op lhs rhs | op == "=" || op == "==" ->
check lhs rhs
T_SimpleCommand _ _ [cmd, lhs, op, rhs] |
getLiteralString cmd == Just "test" &&
getLiteralString op `elem` [Just "=", Just "=="] ->
check lhs rhs
_ -> return ()
where
msg = "Avoid outdated x-prefix in comparisons as it no longer serves a purpose."
check lhs rhs = sequence_ $ do
l <- fixLeadingX lhs
r <- fixLeadingX rhs
return $ styleWithFix (getId lhs) 2268 msg $ fixWith [l, r]
fixLeadingX token =
case getWordParts token of
T_Literal id ('x':_):_ ->
case token of
-- The side is a single, unquoted x, so we have to quote
T_NormalWord _ [T_Literal id "x"] ->
return $ replaceStart id params 1 "\"\""
-- Otherwise we can just delete it
_ -> return $ replaceStart id params 1 ""
T_SingleQuoted id ('x':_):_ ->
-- Replace the single quote and x
return $ replaceStart id params 2 "'"
_ -> Nothing
return [] return []
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |]) runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])