From 499c99372eaef411fc5223393c6799005eebc085 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 14 Nov 2021 21:34:21 -0800 Subject: [PATCH] Rewrite SC2032 warning and mention line number (fixes #2353) --- src/ShellCheck/Analytics.hs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index 188d683..4a7f364 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -2297,7 +2297,7 @@ checkFunctionsUsedExternally params t = let args = skipOver t argv let argStrings = map (\x -> (fromMaybe "" $ getLiteralString x, x)) args let candidates = getPotentialCommands name argStrings - mapM_ (checkArg name) candidates + mapM_ (checkArg name (getId t)) candidates _ -> return () checkCommand _ _ = return () @@ -2323,14 +2323,19 @@ checkFunctionsUsedExternally params t = functionsAndAliases = Map.union (functions t) (aliases t) - checkArg cmd (_, arg) = sequence_ $ do + patternContext id = + case posLine . fst <$> Map.lookup id (tokenPositions params) of + Just l -> " on line " <> show l <> "." + _ -> "." + + checkArg cmd cmdId (_, arg) = sequence_ $ do literalArg <- getUnquotedLiteral arg -- only consider unquoted literals definitionId <- Map.lookup literalArg functionsAndAliases return $ do warn (getId arg) 2033 - "Shell functions can't be passed to external commands." + "Shell functions can't be passed to external commands. Use separate script or sh -c." info definitionId 2032 $ - "Use own script or sh -c '..' to run this from " ++ cmd ++ "." + "This function can't be invoked via " ++ cmd ++ patternContext cmdId prop_checkUnused0 = verifyNotTree checkUnusedAssignments "var=foo; echo $var" prop_checkUnused1 = verifyTree checkUnusedAssignments "var=foo; echo $bar"