mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-09-21 19:29:29 +08:00
Don't warn about undefined HOSTNAME if it's being assigned.
This commit is contained in:
@@ -597,6 +597,8 @@ prop_checkBashisms33= verify checkBashisms "#!/bin/sh\necho -n foo"
|
|||||||
prop_checkBashisms34= verifyNot checkBashisms "#!/bin/dash\necho -n foo"
|
prop_checkBashisms34= verifyNot checkBashisms "#!/bin/dash\necho -n foo"
|
||||||
prop_checkBashisms35= verifyNot checkBashisms "#!/bin/dash\nlocal foo"
|
prop_checkBashisms35= verifyNot checkBashisms "#!/bin/dash\nlocal foo"
|
||||||
prop_checkBashisms36= verifyNot checkBashisms "#!/bin/dash\nread -p foo -r bar"
|
prop_checkBashisms36= verifyNot checkBashisms "#!/bin/dash\nread -p foo -r bar"
|
||||||
|
prop_checkBashisms37= verifyNot checkBashisms "HOSTNAME=foo; echo $HOSTNAME"
|
||||||
|
prop_checkBashisms38= verify checkBashisms "RANDOM=9; echo $RANDOM"
|
||||||
checkBashisms params = bashism
|
checkBashisms params = bashism
|
||||||
where
|
where
|
||||||
isDash = shellType params == Dash
|
isDash = shellType params == Dash
|
||||||
@@ -641,10 +643,11 @@ checkBashisms params = bashism
|
|||||||
warnMsg id $ fromJust str ++ " is"
|
warnMsg id $ fromJust str ++ " is"
|
||||||
where
|
where
|
||||||
str = getLiteralString t
|
str = getLiteralString t
|
||||||
isBashism = isJust str && fromJust str `elem` bashVars
|
isBashism = isJust str && isBashVariable (fromJust str)
|
||||||
bashism t@(T_DollarBraced id token) = do
|
bashism t@(T_DollarBraced id token) = do
|
||||||
mapM_ check expansion
|
mapM_ check expansion
|
||||||
when (var `elem` bashVars) $ warnMsg id $ var ++ " is"
|
when (isBashVariable var) $
|
||||||
|
warnMsg id $ var ++ " is"
|
||||||
where
|
where
|
||||||
str = bracedString t
|
str = bracedString t
|
||||||
var = getBracedReference str
|
var = getBracedReference str
|
||||||
@@ -727,9 +730,19 @@ checkBashisms params = bashism
|
|||||||
(re $ "^[" ++ varChars ++ "]+(\\[.*\\])?/", "string replacement is")
|
(re $ "^[" ++ varChars ++ "]+(\\[.*\\])?/", "string replacement is")
|
||||||
]
|
]
|
||||||
bashVars = [
|
bashVars = [
|
||||||
"RANDOM", "LINENO", "OSTYPE", "MACHTYPE", "HOSTTYPE", "HOSTNAME",
|
"LINENO", "OSTYPE", "MACHTYPE", "HOSTTYPE", "HOSTNAME",
|
||||||
"DIRSTACK", "EUID", "UID", "SECONDS", "SHLVL", "PIPESTATUS", "SHELLOPTS"
|
"DIRSTACK", "EUID", "UID", "SHLVL", "PIPESTATUS", "SHELLOPTS"
|
||||||
]
|
]
|
||||||
|
bashDynamicVars = [ "RANDOM", "SECONDS" ]
|
||||||
|
isBashVariable var =
|
||||||
|
var `elem` bashDynamicVars
|
||||||
|
|| var `elem` bashVars && not (isAssigned var)
|
||||||
|
isAssigned var = any f (variableFlow params)
|
||||||
|
where
|
||||||
|
f x = case x of
|
||||||
|
Assignment (_, _, name, _) -> name == var
|
||||||
|
_ -> False
|
||||||
|
|
||||||
|
|
||||||
prop_checkForInQuoted = verify checkForInQuoted "for f in \"$(ls)\"; do echo foo; done"
|
prop_checkForInQuoted = verify checkForInQuoted "for f in \"$(ls)\"; do echo foo; done"
|
||||||
prop_checkForInQuoted2 = verifyNot checkForInQuoted "for f in \"$@\"; do echo foo; done"
|
prop_checkForInQuoted2 = verifyNot checkForInQuoted "for f in \"$@\"; do echo foo; done"
|
||||||
|
Reference in New Issue
Block a user