shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2155 False Positive

Open EvilSupahFly opened this issue 9 months ago • 1 comments

In this function:

report() {
    local status
    local timestamp
    local ERR_MSG
    local PASSMSG
    local NOTEMSG
    local output_message
    local log_message
    local message

    local status=$1  # F = failure, P = pass, N = notice (neutral), B = Blank
    local message=$2
    local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
    local ERR_MSG="[$timestamp] ERROR: "
    local PASSMSG="[$timestamp] SUCCESS: "
    local NOTEMSG="[$timestamp] NOTICE: "

    # Ensure color variables are defined
    if [[ -z "$RED" ]]; then
        output_message="[$timestamp] $message"
        log_message="[$timestamp] $message"  # Plain message for log
    else
        case "$status" in
            F) output_message="${RED}$ERR_MSG ${WHITE}$message${RESET}"
               log_message="$ERR_MSG $message" ;;  # Plain message for log
            P) output_message="${GREEN}$PASSMSG ${WHITE}$message${RESET}"
               log_message="$PASSMSG $message" ;;  # Plain message for log
            N) output_message="${YELLOW}$NOTEMSG ${WHITE}$message${RESET}"
               log_message="$NOTEMSG $message" ;;  # Plain message for log
            B) output_message="${WHITE}$message${RESET}"
               log_message="$message" ;;  # Plain message for log
            *) output_message="${RED}$ERR_MSG ${WHITE}$status is an unsupported status flag.${RESET}"
               log_message="$ERR_MSG $status is an unsupported status flag." ;;  # Plain message for log
        esac
    fi

    # Output to terminal
    echo -e "$output_message"
    # Log to run.log
    doLog "rebuild" "$log_message"  # Log the plain message
}

The line local timestamp=$(date +"%Y-%m-%d %H:%M:%S") gets flagged with SC2155 despite the previous entry of local timestamp.

Image

EvilSupahFly avatar Mar 18 '25 15:03 EvilSupahFly

If you remove local keyword before the assignment the warning will not be present.

The minimal version of your example

#!/bin/bash

report() {
    local timestamp
    local timestamp=$(date +"%Y-%m-%d %H:%M:%S")

    echo -e "$timestamp"
}

Then become

#!/bin/bash

report() {
    local timestamp
    timestamp=$(date +"%Y-%m-%d %H:%M:%S")

    echo -e "$timestamp"
}

brother avatar Mar 19 '25 09:03 brother