diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f7840b1f..fc8736a0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ - SC2255: Suggest using `$((..))` in `[ 2*3 -eq 6 ]` - SC2256: Warn about translated strings that are known variables +### Changed +- SC2230: This check is now off by default + ## v0.7.0 - 2019-07-28 ### Added - Precompiled binaries for macOS and Linux aarch64 diff --git a/shellcheck.1.md b/shellcheck.1.md index 02175dc9a..187d12a95 100644 --- a/shellcheck.1.md +++ b/shellcheck.1.md @@ -275,8 +275,8 @@ Here is an example `.shellcheckrc`: # Turn on warnings for unassigned uppercase variables enable=check-unassigned-uppercase - # Allow using `which` since it gives full paths and is common enough - disable=SC2230 + # Allow [ ! -z foo ] instead of suggesting -n + disable=SC2236 If no `.shellcheckrc` is found in any of the parent directories, ShellCheck will look in `~/.shellcheckrc` followed by the XDG config directory diff --git a/src/ShellCheck/Analyzer.hs b/src/ShellCheck/Analyzer.hs index 33d2ae051..eb231c2d3 100644 --- a/src/ShellCheck/Analyzer.hs +++ b/src/ShellCheck/Analyzer.hs @@ -35,17 +35,18 @@ analyzeScript spec = newAnalysisResult { arComments = filterByAnnotation spec params . nub $ runAnalytics spec - ++ runChecker params (checkers params) + ++ runChecker params (checkers spec params) } where params = makeParameters spec -checkers params = mconcat $ map ($ params) [ - ShellCheck.Checks.Commands.checker, +checkers spec params = mconcat $ map ($ params) [ + ShellCheck.Checks.Commands.checker spec, ShellCheck.Checks.Custom.checker, ShellCheck.Checks.ShellSupport.checker ] optionalChecks = mconcat $ [ - ShellCheck.Analytics.optionalChecks + ShellCheck.Analytics.optionalChecks, + ShellCheck.Checks.Commands.optionalChecks ] diff --git a/src/ShellCheck/Checks/Commands.hs b/src/ShellCheck/Checks/Commands.hs index 7f068623c..548120498 100644 --- a/src/ShellCheck/Checks/Commands.hs +++ b/src/ShellCheck/Checks/Commands.hs @@ -21,7 +21,7 @@ {-# LANGUAGE FlexibleContexts #-} -- This module contains checks that examine specific commands by name. -module ShellCheck.Checks.Commands (checker , ShellCheck.Checks.Commands.runTests) where +module ShellCheck.Checks.Commands (checker, optionalChecks, ShellCheck.Checks.Commands.runTests) where import ShellCheck.AST import ShellCheck.ASTLib @@ -90,13 +90,30 @@ commandChecks = [ ,checkMvArguments, checkCpArguments, checkLnArguments ,checkFindRedirections ,checkReadExpansions - ,checkWhich ,checkSudoRedirect ,checkSudoArgs ,checkSourceArgs ,checkChmodDashr ] +optionalChecks = map fst optionalCommandChecks +optionalCommandChecks :: [(CheckDescription, CommandCheck)] +optionalCommandChecks = [ + (newCheckDescription { + cdName = "deprecate-which", + cdDescription = "Suggest 'command -v' instead of 'which'", + cdPositive = "which javac", + cdNegative = "command -v javac" + }, checkWhich) + ] +optionalCheckMap = Map.fromList $ map (\(desc, check) -> (cdName desc, check)) optionalCommandChecks + +prop_verifyOptionalExamples = all check optionalCommandChecks + where + check (desc, check) = + verify check (cdPositive desc) + && verifyNot check (cdNegative desc) + buildCommandMap :: [CommandCheck] -> Map.Map CommandName (Token -> Analysis) buildCommandMap = foldl' addCheck Map.empty where @@ -128,8 +145,14 @@ getChecker list = Checker { map = buildCommandMap list -checker :: Parameters -> Checker -checker params = getChecker commandChecks +checker :: AnalysisSpec -> Parameters -> Checker +checker spec params = getChecker $ commandChecks ++ optionals + where + keys = asOptionalChecks spec + optionals = + if "all" `elem` keys + then map snd optionalCommandChecks + else mapMaybe (\x -> Map.lookup x optionalCheckMap) keys prop_checkTr1 = verify checkTr "tr [a-f] [A-F]" prop_checkTr2 = verify checkTr "tr 'a-z' 'A-Z'"