www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - why is this flagged as poor style?

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
The style checker for phobos flags this code:

static if (!is(typeof(unaryFun!fun(r.front)) == Flag!"each"))
     cast(void) unaryFun!fun(r.front);
else
     if (unaryFun!fun(r.front) == No.each) return No.each;

with "Mismatched static if. Use 'else static if' here."

But in fact the intention is to execute a run-time `if` on the `else` 
branch of the `static if`.

Is this worth fixing?
Sep 22 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/22/20 5:35 PM, Andrei Alexandrescu wrote:
 The style checker for phobos flags this code:
 
 static if (!is(typeof(unaryFun!fun(r.front)) == Flag!"each"))
      cast(void) unaryFun!fun(r.front);
 else
      if (unaryFun!fun(r.front) == No.each) return No.each;
 
 with "Mismatched static if. Use 'else static if' here."
 
 But in fact the intention is to execute a run-time `if` on the `else` 
 branch of the `static if`.
 
 Is this worth fixing?
 
I think it needs braces (I'd do them for both blocks). When you are mixing static if and if (or version etc.), make it very clear the difference. -Steve
Sep 22 2020
parent Brian Schott <briancschott gmail.com> writes:
On Tuesday, 22 September 2020 at 21:50:11 UTC, Steven 
Schveighoffer wrote:
 I think it needs braces (I'd do them for both blocks).
The D-Scanner check that's begin discussed here has a unit test checking that adding braces will silence the warning.
Sep 22 2020