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

SC2269: Add self-assignment of array elements to checks #3150

Open
3 of 4 tasks
Simon-Brandt opened this issue Feb 20, 2025 · 0 comments
Open
3 of 4 tasks

SC2269: Add self-assignment of array elements to checks #3150

Simon-Brandt opened this issue Feb 20, 2025 · 0 comments

Comments

@Simon-Brandt
Copy link

For bugs

  • Rule Id (if any, e.g. SC1000): SC2269
  • My shellcheck version (shellcheck --version or 'online'): online
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

For new checks and feature suggestions

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

#!/bin/bash
# SC2269 not triggered for indexed arrays
array=(1 2 3 4)
for i in "${!array[@]}"; do
    array[i]="${array[i]}"
done

or likewise:

#!/bin/bash
# SC2269 not triggered for associative arrays
declare -A array
array[foo]=1
array[bar]=2
array[baz]=3

for k in "${!array[@]}"; do
    array[k]="${array[k]}"
done

Contrast this with

#!/bin/bash
# SC2269 triggered for simple variable assignments
foo=1
foo="${foo}"

Here's what shellcheck (https://www.shellcheck.net/) currently says:

No issues detected!

(both for indexed and associative arrays)

Here's what I wanted or expected to see:

^-- [SC2269](https://www.shellcheck.net/wiki/SC2269) (info): This variable is assigned to itself, so the assignment does nothing.

(as output for the third example with normal variable assignments)

Reason: Assigning array elements to themselves doesn't change their value, just as assigning variables to themselves doesn't change them. Still, for arrays, this may be more involved to check due to the iteration (and not even counting the possible indirect references that ShellCheck cannot see, as in SC2302).

As far as I see, the respective check is done in:

prop_checkAssignToSelf1 = verify checkAssignToSelf "x=$x"
prop_checkAssignToSelf2 = verify checkAssignToSelf "x=${x}"
prop_checkAssignToSelf3 = verify checkAssignToSelf "x=\"$x\""
prop_checkAssignToSelf4 = verifyNot checkAssignToSelf "x=$x mycmd"
checkAssignToSelf _ t =
case t of
T_SimpleCommand _ vars [] -> mapM_ check vars
_ -> return ()
where
check t =
case t of
T_Assignment id Assign name [] t ->
case getWordParts t of
[T_DollarBraced _ _ b] -> do
when (Just name == getLiteralString b) $
msg id
_ -> return ()
_ -> return ()
msg id = info id 2269 "This variable is assigned to itself, so the assignment does nothing."

Might it be possible to extend the checks to array elements, for shells supporting them?

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

1 participant