From bf1003eae36589d7cc78cb1cca210257620e0d60 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 27 Apr 2019 15:20:07 -0700 Subject: [PATCH] Auto-disable SC2119 when disabling SC2120 (fixes #703) --- CHANGELOG.md | 1 + src/ShellCheck/ASTLib.hs | 10 ++++++++++ src/ShellCheck/Analytics.hs | 18 +++++++++++++----- src/ShellCheck/AnalyzerLib.hs | 11 +++++------ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d81b547..79f7910 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ ### Changed - If a directive or shebang is not specified, a `.bash/.bats/.dash/.ksh` extension will be used to infer the shell type when present. +- Disabling SC2120 on a function now disables SC2119 on call sites ## v0.6.0 - 2018-12-02 ### Added diff --git a/src/ShellCheck/ASTLib.hs b/src/ShellCheck/ASTLib.hs index 66269bc..7e8023e 100644 --- a/src/ShellCheck/ASTLib.hs +++ b/src/ShellCheck/ASTLib.hs @@ -501,3 +501,13 @@ isCommandSubstitution t = case t of T_DollarBraceCommandExpansion {} -> True T_Backticked {} -> True _ -> False + + +-- Is this a T_Annotation that ignores a specific code? +isAnnotationIgnoringCode code t = + case t of + T_Annotation _ anns _ -> any hasNum anns + _ -> False + where + hasNum (DisableComment ts) = code == ts + hasNum _ = False diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index 3ef42d2..163a3cc 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -2304,6 +2304,7 @@ prop_checkUnpassedInFunctions9 = verifyNotTree checkUnpassedInFunctions "foo() { prop_checkUnpassedInFunctions10= verifyNotTree checkUnpassedInFunctions "foo() { echo $!; }; foo;" prop_checkUnpassedInFunctions11= verifyNotTree checkUnpassedInFunctions "foo() { bar() { echo $1; }; bar baz; }; foo;" prop_checkUnpassedInFunctions12= verifyNotTree checkUnpassedInFunctions "foo() { echo ${!var*}; }; foo;" +prop_checkUnpassedInFunctions13= verifyNotTree checkUnpassedInFunctions "# shellcheck disable=SC2120\nfoo() { echo $1; }\nfoo\n" checkUnpassedInFunctions params root = execWriter $ mapM_ warnForGroup referenceGroups where @@ -2355,17 +2356,24 @@ checkUnpassedInFunctions params root = updateWith x@(name, _, _) = Map.insertWith (++) name [x] warnForGroup group = - when (all isArgumentless group) $ do - mapM_ suggestParams group - warnForDeclaration group + when (all isArgumentless group) $ + -- Allow ignoring SC2120 on the function to ignore all calls + let (name, func) = getFunction group + ignoring = shouldIgnoreCode params 2120 func + in unless ignoring $ do + mapM_ suggestParams group + warnForDeclaration func name suggestParams (name, _, thing) = info (getId thing) 2119 $ "Use " ++ name ++ " \"$@\" if function's $1 should mean script's $1." - warnForDeclaration ((name, _, _):_) = - warn (getId . fromJust $ Map.lookup name functionMap) 2120 $ + warnForDeclaration func name = + warn (getId func) 2120 $ name ++ " references arguments, but none are ever passed." + getFunction ((name, _, _):_) = + (name, fromJust $ Map.lookup name functionMap) + prop_checkOverridingPath1 = verify checkOverridingPath "PATH=\"$var/$foo\"" prop_checkOverridingPath2 = verify checkOverridingPath "PATH=\"mydir\"" diff --git a/src/ShellCheck/AnalyzerLib.hs b/src/ShellCheck/AnalyzerLib.hs index c4f7cfa..361ce8b 100644 --- a/src/ShellCheck/AnalyzerLib.hs +++ b/src/ShellCheck/AnalyzerLib.hs @@ -882,16 +882,15 @@ filterByAnnotation asSpec params = shouldIgnore note = any (shouldIgnoreFor (getCode note)) $ getPath parents (T_Bang $ tcId note) - shouldIgnoreFor num (T_Annotation _ anns _) = - any hasNum anns - where - hasNum (DisableComment ts) = num == ts - hasNum _ = False shouldIgnoreFor _ T_Include {} = not $ asCheckSourced asSpec - shouldIgnoreFor _ _ = False + shouldIgnoreFor code t = isAnnotationIgnoringCode code t parents = parentMap params getCode = cCode . tcComment +shouldIgnoreCode params code t = + any (isAnnotationIgnoringCode code) $ + getPath (parentMap params) t + -- Is this a ${#anything}, to get string length or array count? isCountingReference (T_DollarBraced id token) = case concat $ oversimplify token of