]> git.ipfire.org Git - thirdparty/libvirt.git/commit
syntax-check: Introduce sc_prohibit_local_with_subshell rule
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 16 Jan 2026 08:12:30 +0000 (09:12 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 19 Jan 2026 13:22:09 +0000 (14:22 +0100)
commit28482f26305b3a15bb7d43f99e73c8bb8b91fef6
tree7014b826f3c77e11b4a2d91dd2554eea2f525ce3
parent24420308565850a50c17101f0f52eb109ebc60e6
syntax-check: Introduce sc_prohibit_local_with_subshell rule

In shell, the following function doesn't echo '1' but '0':

  func() {
      local var=$(false)
      echo $?
  }

This is because '$?' does not refer to 'false' but 'local'. The
bash_builtins(1) manpage explains it well. And it also mentions
other commands behaving the same: export, declare and readonly.
Since it is really easy to miss this pattern, introduce a
syntax-check rule. Mind you, the following pattern (which passes
the rule) does check for the subshell exit code:

  func() {
      local var
      var=$(false)
      echo $?
  }

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
build-aux/syntax-check.mk