Skip to content
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

SC2155 False Positive #3161

Open
EvilSupahFly opened this issue Mar 18, 2025 · 1 comment
Open

SC2155 False Positive #3161

EvilSupahFly opened this issue Mar 18, 2025 · 1 comment

Comments

@EvilSupahFly
Copy link

EvilSupahFly commented Mar 18, 2025

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

@brother
Copy link
Collaborator

brother commented Mar 19, 2025

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"
}

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

No branches or pull requests

2 participants