From 788aee1b7c1c74863c85837fbd53dd45cf810d14 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Fri, 15 Oct 2021 14:39:30 -0700 Subject: [PATCH] Treat typeset similar to declare (fixes #2354) --- src/ShellCheck/Analytics.hs | 1 + src/ShellCheck/AnalyzerLib.hs | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index 912fab5..8960d93 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -2380,6 +2380,7 @@ prop_checkUnused46= verifyTree checkUnusedAssignments "readonly foo=(bar)" prop_checkUnused47= verifyNotTree checkUnusedAssignments "a=1; alias hello='echo $a'" prop_checkUnused48= verifyNotTree checkUnusedAssignments "_a=1" prop_checkUnused49= verifyNotTree checkUnusedAssignments "declare -A array; key=a; [[ -v array[$key] ]]" +prop_checkUnused50= verifyNotTree checkUnusedAssignments "foofunc() { :; }; typeset -fx foofunc" checkUnusedAssignments params t = execWriter (mapM_ warnFor unused) where diff --git a/src/ShellCheck/AnalyzerLib.hs b/src/ShellCheck/AnalyzerLib.hs index e36a14f..311364f 100644 --- a/src/ShellCheck/AnalyzerLib.hs +++ b/src/ShellCheck/AnalyzerLib.hs @@ -571,14 +571,12 @@ isClosingFileOp op = -- Consider 'export/declare -x' a reference, since it makes the var available getReferencedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Literal _ x:_):rest)) = case x of + "declare" -> forDeclare + "typeset" -> forDeclare + "export" -> if "f" `elem` flags then [] else concatMap getReference rest - "declare" -> if - any (`elem` flags) ["x", "p"] && - (not $ any (`elem` flags) ["f", "F"]) - then concatMap getReference rest - else [] "local" -> if "x" `elem` flags then concatMap getReference rest else [] @@ -589,6 +587,13 @@ getReferencedVariableCommand base@(T_SimpleCommand _ _ (T_NormalWord _ (T_Litera "alias" -> [(base, token, name) | token <- rest, name <- getVariablesFromLiteralToken token] _ -> [] where + forDeclare = + if + any (`elem` flags) ["x", "p"] && + (not $ any (`elem` flags) ["f", "F"]) + then concatMap getReference rest + else [] + getReference t@(T_Assignment _ _ name _ value) = [(t, t, name)] getReference t@(T_NormalWord _ [T_Literal _ name]) | not ("-" `isPrefixOf` name) = [(t, t, name)] getReference _ = [] @@ -628,8 +633,8 @@ getModifiedVariableCommand base@(T_SimpleCommand id cmdPrefix (T_NormalWord _ (T "export" -> if "f" `elem` flags then [] else concatMap getModifierParamString rest - "declare" -> if any (`elem` flags) ["F", "f", "p"] then [] else declaredVars - "typeset" -> declaredVars + "declare" -> forDeclare + "typeset" -> forDeclare "local" -> concatMap getModifierParamString rest "readonly" -> @@ -661,6 +666,8 @@ getModifiedVariableCommand base@(T_SimpleCommand id cmdPrefix (T_NormalWord _ (T T_NormalWord id1 [T_DoubleQuoted id2 [T_Literal id3 (stripEquals s)]] stripEqualsFrom t = t + forDeclare = if any (`elem` flags) ["F", "f", "p"] then [] else declaredVars + declaredVars = concatMap (getModifierParam defaultType) rest where defaultType = if any (`elem` flags) ["a", "A"] then DataArray else DataString