Check if =~ is applied to something that looks like a glob

This commit is contained in:
Vidar Holen 2013-01-25 20:06:18 -08:00
parent 02efc2e945
commit 0d3dded238
1 changed files with 13 additions and 0 deletions

View File

@ -76,6 +76,7 @@ basicChecks = [
,checkForInCat
,checkFindExec
,checkValidCondOps
,checkGlobbedRegex
]
treeChecks = [
checkUnquotedExpansions
@ -461,6 +462,18 @@ checkQuotedCondRegex (TC_Binary _ _ "=~" _ rhs) =
error id = err id $ "Don't quote rhs of =~, it'll match literally rather than as a regex."
checkQuotedCondRegex _ = return ()
prop_checkGlobbedRegex1 = verify checkGlobbedRegex "[[ $foo =~ *foo* ]]"
prop_checkGlobbedRegex2 = verify checkGlobbedRegex "[[ $foo =~ f* ]]"
prop_checkGlobbedRegex2a = verify checkGlobbedRegex "[[ $foo =~ \\#* ]]"
prop_checkGlobbedRegex3 = verifyNot checkGlobbedRegex "[[ $foo =~ $foo ]]"
prop_checkGlobbedRegex4 = verifyNot checkGlobbedRegex "[[ $foo =~ ^c.* ]]"
checkGlobbedRegex (TC_Binary _ DoubleBracket "=~" _ rhs) =
case rhs of
T_NormalWord id ((T_Glob _ "*"):_) -> warn id $ "=~ is for regex. Use == for globs."
T_NormalWord id ([(T_Literal _ [c]), (T_Glob _ "*")]) -> warn id $ "=~ is for regex. Either ^anchor$ this, or treat it as a glob with == ."
_ -> return ()
checkGlobbedRegex _ = return ()
prop_checkConstantIfs1 = verify checkConstantIfs "[[ foo != bar ]]"
prop_checkConstantIfs2 = verify checkConstantIfs "[[ n -le 4 ]]"