From 08329b0698612831ec056a370aa432a53a52d2cf Mon Sep 17 00:00:00 2001 From: Christopher Slycord Date: Thu, 17 Jul 2025 13:14:04 +0900 Subject: [PATCH] x-prefix: add support for "!=" and X (capital x) Changes checkComparisonWithLeadingX to: 1) Work with the "!=" operator in addition to "=" and "==". 2) Support prefixing with "x" and "X". This is helpful since some scripts have comparisons like [ "X$var" = "X" ] and the like --- src/ShellCheck/Analytics.hs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index 2e9a3bd..6bcaabf 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -4320,12 +4320,13 @@ prop_checkComparisonWithLeadingX5 = verify checkComparisonWithLeadingX "[ \"x$fo 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 + TC_Binary id typ op lhs rhs + | op `elem` ["=", "==", "!="] -> + check lhs rhs + T_SimpleCommand _ _ [cmd, lhs, op, rhs] + | getLiteralString cmd == Just "test" && + getLiteralString op `elem` [Just "=", Just "==", Just "!="] -> + check lhs rhs _ -> return () where msg = "Avoid x-prefix in comparisons as it no longer serves a purpose." @@ -4335,19 +4336,20 @@ checkComparisonWithLeadingX params t = return $ styleWithFix (getId lhs) 2268 msg $ fixWith [l, r] fixLeadingX token = - case getWordParts token of - T_Literal id ('x':_):_ -> + case getWordParts token of + T_Literal id (c:_):_ | toLower c == 'x' -> case token of - -- The side is a single, unquoted x, so we have to quote - T_NormalWord _ [T_Literal id "x"] -> + -- The side is a single, unquoted x or X, so we have to quote + T_NormalWord _ [T_Literal id [c]] -> 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 "'" + T_SingleQuoted id (c:rest):_ | toLower c == 'x' -> + -- Replace the single quote and the character x or X + return $ replaceStart id params 2 "'" _ -> Nothing + prop_checkAssignToSelf1 = verify checkAssignToSelf "x=$x" prop_checkAssignToSelf2 = verify checkAssignToSelf "x=${x}" prop_checkAssignToSelf3 = verify checkAssignToSelf "x=\"$x\""