koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
36.45k stars 1.78k forks source link

SC2154/check-unassigned-uppercase does not report error for $1,... #3080

Open kkmuffme opened 1 week ago

kkmuffme commented 1 week ago

For bugs

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/bin/bash
if [[ -n "$4" ]]
then
    echo "$5"
fi

Here's what shellcheck currently says:

No error

Here's what I wanted or expected to see:

5 is referenced but not assigned

The global variables $1, $2,... (not $0) should be checked like any other uppercase variable when check-unassigned-uppercase is used. The numbers are easily fatfingered and this is an easy mistake to make when restructuring code too.

wileyhy commented 4 days ago

Usually, positional parameters at the global scope are neither declared nor assigned. They may be assigned with:

~ $ set -- x yz If a script writer plans for $4 to only ever be '--dir', and for $5 to be defined if and only if $4 exists, then that would be some valid code there, no?

Wiley

On Sun, Nov 10, 2024, 20:23 kkmuffme @.***> wrote:

For bugs

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

!/bin/bashif [[ -n "$4" ]]then

echo "$5"fi

Here's what shellcheck currently says:

No error Here's what I wanted or expected to see:

5 is referenced but not assigned

The global variables $1, $2,... (not $0) should be checked like any other uppercase variable when check-unassigned-uppercase is used. The numbers are easily fatfingered and this is an easy mistake to make when restructuring code too.

— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/3080, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F23RWRM4RPIMWW4S6IL2AAWKJAVCNFSM6AAAAABRQ7NJUSVHI2DSMVQWIX3LMV43ASLTON2WKOZSGY2DQMBZGUYDGMY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

kkmuffme commented 16 hours ago

I think we are talking about quite different use cases. From what I come across, the most common case is bash foo.sh a b c d