Skip to content

Warn on empty local assignment #1297

@rowlap

Description

@rowlap

For new checks and feature suggestions

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

#!/bin/bash

set -u

myfunc() {
        local a=
        a=apple
        echo "a is $a"
}

myfunc

Here's what I wanted or expected to see:

shellcheck currently says nothing about this. The issue is that the empty assignment local a= defeats the purpose of set -u, which won't be triggered on empty string.

If the developer really means empty string this can be written as local a='', otherwise local a is safer in that set -u is still effective against typos.

Other forms of assignment with modifiers are alias a=b / declare a=b / typeset a=b / export a=b / readonly a=b but I'm less sure that any of those are likely to appear with missing values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions