Allow SC2243 and SC2244 to trigger with quotes, add fix

This commit is contained in:
Vidar Holen 2018-12-28 19:01:31 -08:00
parent f6bc009331
commit 73822c3588
4 changed files with 28 additions and 15 deletions

View File

@ -1,6 +1,7 @@
## Since previous release ## Since previous release
### Added ### Added
- Preliminary support for fix suggestions - Preliminary support for fix suggestions
- SC2243/SC2244: Suggest using explicit -n for `[ $foo ]`
## v0.6.0 - 2018-12-02 ## v0.6.0 - 2018-12-02
### Added ### Added

View File

@ -485,8 +485,11 @@ wordsCanBeEqual x y = fromMaybe True $
-- Is this an expansion that can be quoted, -- Is this an expansion that can be quoted,
-- e.g. $(foo) `foo` $foo (but not {foo,})? -- e.g. $(foo) `foo` $foo (but not {foo,})?
isQuoteableExpansion t = case t of isQuoteableExpansion t = case t of
T_DollarBraced {} -> True
_ -> isCommandSubstitution t
isCommandSubstitution t = case t of
T_DollarExpansion {} -> True T_DollarExpansion {} -> True
T_DollarBraceCommandExpansion {} -> True T_DollarBraceCommandExpansion {} -> True
T_Backticked {} -> True T_Backticked {} -> True
T_DollarBraced {} -> True
_ -> False _ -> False

View File

@ -3101,16 +3101,22 @@ prop_checkNullaryExpansionTest1 = verify checkNullaryExpansionTest "[[ $(a) ]]"
prop_checkNullaryExpansionTest2 = verify checkNullaryExpansionTest "[[ $a ]]" prop_checkNullaryExpansionTest2 = verify checkNullaryExpansionTest "[[ $a ]]"
prop_checkNullaryExpansionTest3 = verifyNot checkNullaryExpansionTest "[[ $a=1 ]]" prop_checkNullaryExpansionTest3 = verifyNot checkNullaryExpansionTest "[[ $a=1 ]]"
prop_checkNullaryExpansionTest4 = verifyNot checkNullaryExpansionTest "[[ -n $(a) ]]" prop_checkNullaryExpansionTest4 = verifyNot checkNullaryExpansionTest "[[ -n $(a) ]]"
checkNullaryExpansionTest _ t = prop_checkNullaryExpansionTest5 = verify checkNullaryExpansionTest "[[ \"$a$b\" ]]"
prop_checkNullaryExpansionTest6 = verify checkNullaryExpansionTest "[[ `x` ]]"
checkNullaryExpansionTest params t =
case t of case t of
TC_Nullary _ _ (T_NormalWord id [T_DollarExpansion _ [T_Pipeline _ [] [x]]]) -> TC_Nullary _ _ word ->
when (isJust (getCommand x)) $ case getWordParts word of
style id 2243 ( [t] | isCommandSubstitution t ->
"To check for the exit code of a command, remove the conditional expression, e.g. if foo; ...") styleWithFix id 2243 "Prefer explicit -n to check for output (or run command without [/[[ to check for success)." fix
TC_Nullary _ _ (T_NormalWord id [t2]) ->
when ((not . isConstant) t2) $ -- If they're constant, you get SC2157 &co
style id 2244 ( x | all (not . isConstant) x ->
"Use -n to check for null string, e.g. [[ -n $var ]].") styleWithFix id 2244 "Prefer explicit -n to check non-empty string (or use =/-ne to check boolean/integer)." fix
_ -> return ()
where
id = getId word
fix = fixWith [replaceStart id params 0 "-n "]
_ -> return () _ -> return ()
return [] return []

View File

@ -155,11 +155,14 @@ err id code str = addComment $ makeComment ErrorC id code str
info id code str = addComment $ makeComment InfoC id code str info id code str = addComment $ makeComment InfoC id code str
style id code str = addComment $ makeComment StyleC id code str style id code str = addComment $ makeComment StyleC id code str
warnWithFix id code str fix = addComment $ warnWithFix :: MonadWriter [TokenComment] m => Id -> Code -> String -> Fix -> m ()
let comment = makeComment WarningC id code str in warnWithFix = addCommentWithFix WarningC
comment { styleWithFix :: MonadWriter [TokenComment] m => Id -> Code -> String -> Fix -> m ()
tcFix = Just fix styleWithFix = addCommentWithFix StyleC
}
addCommentWithFix :: MonadWriter [TokenComment] m => Severity -> Id -> Code -> String -> Fix -> m ()
addCommentWithFix severity id code str fix =
addComment $ makeCommentWithFix severity id code str fix
makeCommentWithFix :: Severity -> Id -> Code -> String -> Fix -> TokenComment makeCommentWithFix :: Severity -> Id -> Code -> String -> Fix -> TokenComment
makeCommentWithFix severity id code str fix = makeCommentWithFix severity id code str fix =