From 417272216794dbe25b552c46263a82e6fbe96768 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 3 Oct 2015 15:21:57 -0700 Subject: [PATCH] Don't warn about quoted rhs of =~ when not a regex. --- ShellCheck/Analytics.hs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index 51d3ffe..d0ab0da 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -1206,16 +1206,26 @@ checkConditionalAndOrs _ t = otherwise -> return () -prop_checkQuotedCondRegex1 = verify checkQuotedCondRegex "[[ $foo =~ \"bar\" ]]" -prop_checkQuotedCondRegex2 = verify checkQuotedCondRegex "[[ $foo =~ 'cow' ]]" +prop_checkQuotedCondRegex1 = verify checkQuotedCondRegex "[[ $foo =~ \"bar.*\" ]]" +prop_checkQuotedCondRegex2 = verify checkQuotedCondRegex "[[ $foo =~ '(cow|bar)' ]]" prop_checkQuotedCondRegex3 = verifyNot checkQuotedCondRegex "[[ $foo =~ $foo ]]" +prop_checkQuotedCondRegex4 = verifyNot checkQuotedCondRegex "[[ $foo =~ \"bar\" ]]" +prop_checkQuotedCondRegex5 = verifyNot checkQuotedCondRegex "[[ $foo =~ 'cow bar' ]]" checkQuotedCondRegex _ (TC_Binary _ _ "=~" _ rhs) = case rhs of - T_NormalWord id [T_DoubleQuoted _ _] -> error id - T_NormalWord id [T_SingleQuoted _ _] -> error id + T_NormalWord id [T_DoubleQuoted _ _] -> error rhs + T_NormalWord id [T_SingleQuoted _ _] -> error rhs _ -> return () where - error id = err id 2076 "Don't quote rhs of =~, it'll match literally rather than as a regex." + error t = + unless (isConstantNonRe t) $ + err (getId t) 2076 + "Don't quote rhs of =~, it'll match literally rather than as a regex." + re = mkRegex "[][*.+()]" + hasMetachars s = s `matches` re + isConstantNonRe t = fromMaybe False $ do + s <- getLiteralString t + return . not $ hasMetachars s checkQuotedCondRegex _ _ = return () prop_checkGlobbedRegex1 = verify checkGlobbedRegex "[[ $foo =~ *foo* ]]"