@LukeAlmighty Single = means assign. That means you can assign in an if statement.

@matrix Yeah, that XD

Technically, you can write entire functions inside of if. Lines and function separations are quite artificial.

We just don't do it, because programming is mentally tasking enough already.

@matrix @LukeAlmighty C famously also supports that, it's been a source of numerous backdoors and most linters tend to forbid it.

@applejack @LukeAlmighty @matrix

There is no such thing as consistent behavior for linters and it’s rather well known that linux kernel has it’s own, but well, the horribly old one called splint still does by default:

$ cat hello.c #include <stdio.h> // puts int main(int argc, char *argv[]) { (void)argc; if ((argv[1] = "--help")) { (void)puts("."); } (void)puts(argv[1]); return 0; } $ make hello CFLAGS="-Wall -Wextra" cc -Wall -Wextra hello.c -o hello hello.c: In function ‘main’: hello.c:6:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 6 | if (argv[1] = "--help") { | ^~~~ $ ./hello . --help $ splint hello.c Splint 3.1.2 --- 03 Feb 2021 hello.c: (in function main) hello.c:5:2: Statement has no effect: (void)argc Statement has no visible effect --- no values are modified. (Use -noeffect to inhibit warning) hello.c:6:7: Observer storage assigned to unqualified reference: argv[1] = "--help" Observer storage is transferred to a non-observer reference. (Use -observertrans to inhibit warning) hello.c:6:17: Storage becomes observer Finished checking --- 2 code warnings $

@applejack @LukeAlmighty @matrix That’s probably because assigning into a while is rather common, for example while((c = getopt(argc, argv, ":d:u")) != -1) is idiomatic in C but I wouldn’t be surprised that the linux kernel linter warns about it.

@lanodan @LukeAlmighty @matrix I'm not a kernel dev. GCC and Clang also behave in the same way with -Wall just suggesting ()

@applejack @LukeAlmighty @matrix Kernel dev isn't really a thing I do either (specially because I hate having to deal with boot time problems) but I'm still a bit familiar with it because of needing to apply some patches and few times adapt them from one version to another.
@LukeAlmighty @matrix yes. It's a common programming error. The best solution to it, in modern languages, is to make = a statement instead of an expression. That costs you a=b=c=d=0 terse initialization, but there are replacements for that, just like there are replacements for deliberate assignment in an if (useful for scoping a variable to the if body while testing it).

@matrix
That wouldn't capture a help flag for a specific command tho

@matrix it makes it easier to understand for sql wizards :^)

@matrix Ye? It's useful. These do the same thing. 2 lines instead of 5

@matrix You get a lot of that with interpreted and dynamically typed languages

@applejack Yeah, Python errors out, but does lot of different bullshit instead

@matrix :cirno_think: dude just wondering lately, are you doing php because its job related or just learning it just cus?
Sign in to participate in the conversation
Game Liberty Mastodon

Mainly gaming/nerd instance for people who value free speech. Everyone is welcome.