mirror of
				https://github.com/koalaman/shellcheck.git
				synced 2025-10-31 22:52:50 +08:00 
			
		
		
		
	SC2181: Add '!' in suggestion as appropriate (fixes #2189)
This commit is contained in:
		| @@ -3118,24 +3118,29 @@ prop_checkReturnAgainstZero8 = verify checkReturnAgainstZero "(( $? ))" | |||||||
| prop_checkReturnAgainstZero9 = verify checkReturnAgainstZero "(( ! $? ))" | prop_checkReturnAgainstZero9 = verify checkReturnAgainstZero "(( ! $? ))" | ||||||
| checkReturnAgainstZero _ token = | checkReturnAgainstZero _ token = | ||||||
|     case token of |     case token of | ||||||
|         TC_Binary id _ _ lhs rhs -> check lhs rhs |         TC_Binary id _ op lhs rhs -> check op lhs rhs | ||||||
|         TA_Binary id _ lhs rhs -> check lhs rhs |         TA_Binary id op lhs rhs -> check op lhs rhs | ||||||
|         TA_Unary id _ exp |         TA_Unary id op exp | ||||||
|             | isExitCode exp -> message (getId exp) |             | isExitCode exp -> message (checksSuccessLhs op) (getId exp) | ||||||
|         TA_Sequence _ [exp] |         TA_Sequence _ [exp] | ||||||
|             | isExitCode exp -> message (getId exp) |             | isExitCode exp -> message False (getId exp) | ||||||
|         _ -> return () |         _ -> return () | ||||||
|   where |   where | ||||||
|     check lhs rhs = |     -- Is "$? op 0" trying to check if the command succeeded? | ||||||
|  |     checksSuccessLhs op = not $ op `elem` ["-gt", "-ne", "!=", "!"] | ||||||
|  |     -- Is "0 op $?" trying to check if the command succeeded? | ||||||
|  |     checksSuccessRhs op = op `notElem` ["-ne", "!="] | ||||||
|  |     check op lhs rhs = | ||||||
|         if isZero rhs && isExitCode lhs |         if isZero rhs && isExitCode lhs | ||||||
|         then message (getId lhs) |         then message (checksSuccessLhs op) (getId lhs) | ||||||
|         else when (isZero lhs && isExitCode rhs) $ message (getId rhs) |         else when (isZero lhs && isExitCode rhs) $ message (checksSuccessRhs op) (getId rhs) | ||||||
|     isZero t = getLiteralString t == Just "0" |     isZero t = getLiteralString t == Just "0" | ||||||
|     isExitCode t = |     isExitCode t = | ||||||
|         case getWordParts t of |         case getWordParts t of | ||||||
|             [T_DollarBraced _ _ l] -> concat (oversimplify l) == "?" |             [T_DollarBraced _ _ l] -> concat (oversimplify l) == "?" | ||||||
|             _ -> False |             _ -> False | ||||||
|     message id = style id 2181 "Check exit code directly with e.g. 'if mycmd;', not indirectly with $?." |     message forSuccess id = style id 2181 $ | ||||||
|  |         "Check exit code directly with e.g. 'if " ++ (if forSuccess then "" else "! ") ++ "mycmd;', not indirectly with $?." | ||||||
|  |  | ||||||
| prop_checkRedirectedNowhere1 = verify checkRedirectedNowhere "> file" | prop_checkRedirectedNowhere1 = verify checkRedirectedNowhere "> file" | ||||||
| prop_checkRedirectedNowhere2 = verify checkRedirectedNowhere "> file | grep foo" | prop_checkRedirectedNowhere2 = verify checkRedirectedNowhere "> file | grep foo" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user