Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Feature request: a new warning option
- X-seq: zsh-users 24328
- From: Roman Perepelitsa <roman.perepelitsa@xxxxxxxxx>
- To: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
- Subject: Re: Feature request: a new warning option
- Date: Wed, 9 Oct 2019 10:45:15 +0200
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=xekR42brWZqThBGe3rMuytBjHFxWJTXwS826rkBncq4=; b=UbQynZLpGNZYApu+DWv/sNHTb8KIElA+VvHi27e3ssCzfO8CmLPNRVnb2VLffUGB2s GNB3+BhIGIzkY+Lgxqww7n2o91f2WWFtdQo4ydGLnAO9Amvb9xZOt5K7piDbudP03Ymv X1Rg4ZQkx7riJqs5NAwaWIIAW9mzK8at2g3tOpSh9TBmTPo33Gb+IfCe63oT/t0140SD pqfKVz8Jxih1J3rrIFpDEdCUwJrhlqdPrSGmz99zHsfTBPSzJQBy1qPMTcLGWESttHOG M3uCkK0rv5fmIjJuGm7s6Boi8sBulhUHUmt2mpRzLaq584iZ68w+by06/6Bi1KrAjGLE SQyQ==
- In-reply-to: <CAKc7PVDN-GspoiS-iVR5ThdbDVbLWVNJWcZ=TyY0=9ydtPswAw@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CAKc7PVDN-GspoiS-iVR5ThdbDVbLWVNJWcZ=TyY0=9ydtPswAw@mail.gmail.com>
On Wed, Oct 9, 2019 at 6:03 AM Sebastian Gniazdowski
<sgniazdowski@xxxxxxxxx> wrote:
>
> Hello,
> how about detecting situations in the code like the following:
>
> fun() { some code possibly returning false }
> (( condition )) && fun || print "Some 'else'-instruction"
I've made a quick-and-dirty search over powerlevel10k source code to
see how often this warning would have false positives. Here are a few
examples of `x && y || z` constructs where `y` can fail and yet the
code is correct.
(( $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )) &&
gitstatus_start POWERLEVEL9K ||
_p9k_gitstatus_disabled=1
mkdir -p ${dst:h} && cp -f $src $dst || return
_p9k_cached_cmd_stdout node -v && [[ $_p9k_ret == v?* ]] || return
`x && y || z` is not `x ? y : z` even though it's often abused as
such. I think the solution is to use `x && y || z` when you mean it,
which is to say *only* when both `x` and `y` may be falsy. The ternary
can be expressed with an `if`.
Compare:
(( condition )) && fun || print "Some 'else'-instruction"
vs
if (( condition )) { fun } else { print "Some 'else'-instruction" }
The latter is longer but is at least as clear. It also comes with
curlies that make it easy to split the statement into multiple lines
if necessary, or to add extra statements inside the branches.
I myself am guilty of using `x && y || z` in place of a ternary
and this leads to bugs that your proposed warning is meant to flag.
However, if I were to enable this warning, I wouldn't be able to
use `x && y || z` when it is the best tool for the job. Hence I'm
leaning towards unlearning my current habbit and starting to use
`if-else` more.
What do you think?
Roman.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author