Skip to content

Commit f2af3dc

Browse files
committed
Suppress SC2319/SC2320 when $? is assigned to a variable (fixes #3486)
1 parent 9af7ee2 commit f2af3dc

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/ShellCheck/Analytics.hs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5093,10 +5093,13 @@ prop_checkOverwrittenExitCode1 = verify checkOverwrittenExitCode "x; [ $? -eq 1
50935093
prop_checkOverwrittenExitCode2 = verifyNot checkOverwrittenExitCode "x; [ $? -eq 1 ]"
50945094
prop_checkOverwrittenExitCode3 = verify checkOverwrittenExitCode "x; echo \"Exit is $?\"; [ $? -eq 0 ]"
50955095
prop_checkOverwrittenExitCode4 = verifyNot checkOverwrittenExitCode "x; [ $? -eq 0 ] && echo Success"
5096-
prop_checkOverwrittenExitCode5 = verify checkOverwrittenExitCode "x; if [ $? -eq 0 ]; then var=$?; fi"
5097-
prop_checkOverwrittenExitCode6 = verify checkOverwrittenExitCode "x; [ $? -gt 0 ] && fail=$?"
5096+
prop_checkOverwrittenExitCode5 = verify checkOverwrittenExitCode "x; if [ $? -eq 0 ]; then exit $?; fi"
5097+
prop_checkOverwrittenExitCode6 = verify checkOverwrittenExitCode "x; [ $? -eq 0 ] && exit $?"
50985098
prop_checkOverwrittenExitCode7 = verifyNot checkOverwrittenExitCode "[ 1 -eq 2 ]; status=$?"
50995099
prop_checkOverwrittenExitCode8 = verifyNot checkOverwrittenExitCode "[ 1 -eq 2 ]; exit $?"
5100+
prop_checkOverwrittenExitCode9 = verifyNot checkOverwrittenExitCode "[[ -e \"./lockfile\" ]] || retval=$?"
5101+
prop_checkOverwrittenExitCode10 = verifyNot checkOverwrittenExitCode "[ -e \"./lockfile\" ] || retval=$?"
5102+
prop_checkOverwrittenExitCode11 = verifyNot checkOverwrittenExitCode "[ 1 -eq 2 ] && status=$?"
51005103
checkOverwrittenExitCode params t =
51015104
case t of
51025105
T_DollarBraced id _ val | getLiteralString val == Just "?" -> check id
@@ -5111,9 +5114,12 @@ checkOverwrittenExitCode params t =
51115114
let idToToken = idMap params
51125115
exitCodeTokens <- traverse (\k -> Map.lookup k idToToken) $ S.toList exitCodeIds
51135116
return $ do
5114-
when (all isCondition exitCodeTokens && not (usedUnconditionally cfga t exitCodeIds)) $
5117+
when (all isCondition exitCodeTokens
5118+
&& not (usedUnconditionally cfga t exitCodeIds)
5119+
&& not isInAssignment) $
51155120
warn id 2319 "This $? refers to a condition, not a command. Assign to a variable to avoid it being overwritten."
5116-
when (all isPrinting exitCodeTokens) $
5121+
when (all isPrinting exitCodeTokens
5122+
&& not isInAssignment) $
51175123
warn id 2320 "This $? refers to echo/printf, not a previous command. Assign to variable to avoid it being overwritten."
51185124

51195125
isCondition t =
@@ -5133,6 +5139,11 @@ checkOverwrittenExitCode params t =
51335139
Just "printf" -> True
51345140
_ -> False
51355141

5142+
isInAssignment = any isAssignmentToken $ getPath (parentMap params) t
5143+
where
5144+
isAssignmentToken (T_Assignment _ _ _ _ _) = True
5145+
isAssignmentToken _ = False
5146+
51365147

51375148
prop_checkUnnecessaryArithmeticExpansionIndex1 = verify checkUnnecessaryArithmeticExpansionIndex "a[$((1+1))]=n"
51385149
prop_checkUnnecessaryArithmeticExpansionIndex2 = verifyNot checkUnnecessaryArithmeticExpansionIndex "a[1+1]=n"

0 commit comments

Comments
 (0)