Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC2236/SC2237-equivalent to binary operators: =, !=, -eq, -ne, -gt, -ge, -lt, and -le #3164

Open
2 tasks done
e-kwsm opened this issue Mar 20, 2025 · 1 comment
Open
2 tasks done

Comments

@e-kwsm
Copy link

e-kwsm commented Mar 20, 2025

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/bin/sh
[ ! "$USER" = me ]
! [ "$USER" != me ]
[ ! $$ -eq 0 ]
! [ $$ -gt 0 ]

Here's what shellcheck currently says:

nothing

Here's what I wanted or expected to see:

[ ! "$USER" = me ]
  ^-- SCXXXX (style): Use a != b instead of ! a = b
! [ "$USER" != me ]
^-- SCXXXX (style): Use [ a = b ] instead of ! [ a != b ]
[ ! $$ -eq 0 ]
  ^-- SCXXXX (style): Use a -ne b instead of ! a -eq b
! [ $$ -gt 0 ]
^-- SCXXXX (style): Use [ a -le b ] instead of ! [ a -gt b ]

similarly to SC2236 and SC2237 (double negation).

@e-kwsm
Copy link
Author

e-kwsm commented Mar 20, 2025

I am not familiar with Haskell, but something like below will address the issue

--- a/src/ShellCheck/Analytics.hs
+++ b/src/ShellCheck/Analytics.hs
@@ -3906,12 +3906,35 @@ checkInvertedStringTest _ t =
                 "-n" -> style (getId t) 2236 "Use -z instead of ! -n."
                 "-z" -> style (getId t) 2236 "Use -n instead of ! -z."
                 _ -> return ()
+        TC_Unary _ _ "!" (TC_Binary _ _ op _ _) ->
+            case op of
+                "=" -> style (getId t) 9999 "Use a != b instead of ! a = b."
+                "!=" -> style (getId t) 9999 "Use a = b instead of ! a != b."
+                "-eq" -> style (getId t) 9999 "Use a -ne b instead of ! a -eq b."
+                "-ne" -> style (getId t) 9999 "Use a -eq b instead of ! a -ne b."
+                "-gt" -> style (getId t) 9999 "Use a -le b instead of ! a -gt b."
+                "-ge" -> style (getId t) 9999 "Use a -lt b instead of ! a -ge b."
+                "-lt" -> style (getId t) 9999 "Use a -ge b instead of ! a -lt b."
+                "-le" -> style (getId t) 9999 "Use a -gt b instead of ! a -le b."
+                _ -> return ()
         T_Banged _ (T_Pipeline _ _
           [T_Redirecting _ _ (T_Condition _ _ (TC_Unary _ _ op _))]) ->
             case op of
                 "-n" -> style (getId t) 2237 "Use [ -z .. ] instead of ! [ -n .. ]."
                 "-z" -> style (getId t) 2237 "Use [ -n .. ] instead of ! [ -z .. ]."
                 _ -> return ()
+        T_Banged _ (T_Pipeline _ _
+          [T_Redirecting _ _ (T_Condition _ _ (TC_Binary _ _ op _ _))]) ->
+            case op of
+                "=" -> style (getId t) 9999 "Use [ a != b ] instead of ! [ a = b ]."
+                "!=" -> style (getId t) 9999 "Use [ a = b ] instead of ! [ a != b ]."
+                "-eq" -> style (getId t) 9999 "Use [ a -ne b ] instead of ! [ a -eq b ]."
+                "-ne" -> style (getId t) 9999 "Use [ a -eq b ] instead of ! [ a -ne b ]."
+                "-gt" -> style (getId t) 9999 "Use [ a -le b ] instead of ! [ a -gt b ]."
+                "-ge" -> style (getId t) 9999 "Use [ a -lt b ] instead of ! [ a -ge b ]."
+                "-lt" -> style (getId t) 9999 "Use [ a -ge b ] instead of ! [ a -lt b ]."
+                "-le" -> style (getId t) 9999 "Use [ a -gt b ] instead of ! [ a -le b ]."
+                _ -> return ()
         _ -> return ()

 prop_checkRedirectionToCommand1 = verify checkRedirectionToCommand "ls > rm"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant