@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.
@lanodan @LukeAlmighty @matrix Linters just warn you if you don't wrap it in extra ()
@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 ()
@LukeAlmighty Single = means assign. That means you can assign in an if statement.