From 9ae776530b8bc2d95de1b4fb085eb88e0f51e09a Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Mon, 27 Jan 2014 22:47:48 -0800 Subject: [PATCH] Check for [[ i + 1 = 2 ]] --- ShellCheck/Data.hs | 2 +- ShellCheck/Parser.hs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ShellCheck/Data.hs b/ShellCheck/Data.hs index 1256a9d..05ea677 100644 --- a/ShellCheck/Data.hs +++ b/ShellCheck/Data.hs @@ -2,7 +2,7 @@ module ShellCheck.Data where internalVariables = [ -- Generic - "", "_", + "", "_", "rest", "REST", -- Bash "BASH", "BASHOPTS", "BASHPID", "BASH_ALIASES", "BASH_ARGC", diff --git a/ShellCheck/Parser.hs b/ShellCheck/Parser.hs index ca56f48..18bb227 100644 --- a/ShellCheck/Parser.hs +++ b/ShellCheck/Parser.hs @@ -34,8 +34,6 @@ import System.IO import Text.Parsec.Error import GHC.Exts (sortWith) -lastError = 1074 - backslash = char '\\' linefeed = (optional carriageReturn) >> char '\n' singleQuote = char '\'' <|> unicodeSingleQuote @@ -269,6 +267,7 @@ readConditionContents single = do where typ = if single then SingleBracket else DoubleBracket readCondBinaryOp = try $ do + optional guardArithmetic id <- getNextId op <- (choice $ (map tryOp ["==", "!=", "<=", ">=", "=~", ">", "<", "=", "\\<=", "\\>=", "\\<", "\\>"])) <|> otherOp hardCondSpacing @@ -284,6 +283,13 @@ readConditionContents single = do when (s == "-a" || s == "-o") $ fail "Wrong operator" return $ TC_Binary id typ s + guardArithmetic = do + try . lookAhead $ disregard (oneOf "+*/%") <|> disregard (string "- ") + parseProblem ErrorC 1076 $ + if single + then "Trying to do math? Use e.g. [ $((i/2+7)) -ge 18 ]." + else "Trying to do math? Use e.g. [[ $((i/2+7)) -ge 18 ]]." + readCondUnaryExp = do op <- readCondUnaryOp pos <- getPosition @@ -323,6 +329,7 @@ readConditionContents single = do endedWith _ _ = False readCondAndOp = do + optional guardArithmetic id <- getNextId x <- try (string "&&" <|> string "-a") when (single && x == "&&") $ addNoteFor id $ Note ErrorC 1022 "You can't use && inside [..]. Use [[..]] instead." @@ -331,6 +338,7 @@ readConditionContents single = do return $ TC_And id typ x readCondOrOp = do + optional guardArithmetic id <- getNextId x <- try (string "||" <|> string "-o") when (single && x == "||") $ addNoteFor id $ Note ErrorC 1024 "You can't use || inside [..]. Use [[..]] instead."