Skip to content

Skip SC2015 when the last command is true #3114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025

Conversation

Flu
Copy link
Contributor

@Flu Flu commented Jan 7, 2025

In cases like

#!/bin/sh
set -e
foo && bar || true

The last true is used for masking the return value, no matter if both foo and bar executed, therefore SC2015 has no relevance here.

Closes #2977

@SvenStaehs
Copy link

SvenStaehs commented Mar 10, 2025

Thank you! Hope to see this included in the next release, we are due one any day now 😆
My use-case for the record:

#!/usr/bin/env bash
set -euo pipefail
[...]
for attempt in {3..0}; do
    [[ $attempt -gt 0 ]] || die "Failed to [do the thing]."
    # shellcheck disable=SC2015 # "break" never fails (I think), AND "true" has no side-effect, so this rule does not apply. See https://github.com/koalaman/shellcheck/issues/2977
    doSomethingFlaky && break || true
done

checkShorthandIf params x@(T_OrIf _ (T_AndIf id _ b) (T_Pipeline _ _ t))
| not (isOk t || inCondition) && not (isTestCommand b) =
info id 2015 "Note that A && B || C is not if-then-else. C may run when A is true."
where
isOk [t] = isAssignment t || fromMaybe False (do
name <- getCommandBasename t
return $ name `elem` ["echo", "exit", "return", "printf"])
return $ name `elem` ["echo", "exit", "return", "printf", "true"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

colon(1P), :, does nothing and always returns zero as well.

Suggested change
return $ name `elem` ["echo", "exit", "return", "printf", "true"])
return $ name `elem` ["echo", "exit", "return", "printf", "true", ":"])

@koalaman koalaman merged commit de07ec1 into koalaman:master Apr 12, 2025
@koalaman
Copy link
Owner

Merged with the addition of : as suggested. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Skip SC2015 when it end with: || true
4 participants