koalaman/shellcheck

SC1007 uncovered arrays

Opened this issue · 2 comments

For bugs with existing features

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

#!/bin/bash
export a="$(ls -la ./)"
export b=("$(ls -la ./)")
echo "$a"
echo "${b[@]}"

Here's what shellcheck currently says:

In btest.sh line 2:
export a="$(ls -la ./)"
       ^-- SC2155 (warning): Declare and assign separately to avoid masking return values.

Here's what I wanted or expected to see:

In btest.sh line 2:
export a="$(ls -la ./)"
       ^-- SC2155 (warning): Declare and assign separately to avoid masking return values.
In btest.sh line 3:
export b=("$(ls -la ./)")
       ^-- SC2155 (warning): Declare and assign separately to avoid masking return values.

Rationale

ShellCheck should throw a declare and assign separately warning, no matter if we are assigning an array or a variable.

SC2155: declare and assign separately seems to be not a relevant warning at all for the case you provide

SC2155 is totally reasonable for things kinda export a="$(ls -la ./)" or local a="$(ls -la ./)", but there's no way to declare and assign separately, when it goes about readonly variable

This comment by @juliyvchirkov seems unrelated to this issue, since the main idea of the issue and the comment differ.
The attributes may differ, while expected and actual behaviour would be the same, so the specific attribute does not matter.
For the sake of simplicity, I changed the attributes to export in the issue example


Regarding the comment, ShellCheck specifies how to deal with readonly variables in the following wiki page

It should be done like this:

#!/bin/bash
a="$(ls -la ./)"
b=("$(ls -la ./)")
readonly a
readonly b
echo "$a"
echo "${b[@]}"

So the issue should be fixed for all attributes, including readonly