Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: C23 (C2x) changes Date: Mon, 27 Sep 2021 11:15:52 -0700 Organization: None to speak of Lines: 68 Message-ID: <87fstpkhpj.fsf@nosuchdomain.example.com> References: <145dc56b-c3e8-4743-94d3-fb15d921272bn@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="39ff22a84492e5ec37e25bfe4071ee27"; logging-data="14470"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/AT68Qn8GVXQhpixhsNKX5" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:6QDkwQDC3S1+nbccZ3ThxsohtVw= sha1:8riirLi2gKZUnPGDT5fcCy0Snpk= Xref: csiph.com comp.lang.c:162849 Thiago Adams writes: [...] > Something new in C23 are attributes. > One of then is nodiscard : > > http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2596.pdf > > " > EXAMPLE 2 > [[nodiscard]] int important_func(void); > void call(void) { > int i = important_func(); > } > No diagnostic for the call to important_func is encouraged despite the value of i not being used. > " > [[nodiscard("must check armed state")]] > bool arm_detonator(int); > void call(void) { > arm_detonator(3); > detonate(); > } > > "A diagnostic for the call toarm_detonator using the string literal "must check armed state" from the attribute argument > clause is encouraged." > > This called my attention because the treatment is different for > int or _bool > I have no idea why. No, there's no difference in the treatment for int and _Bool. The two relevant examples are: [[nodiscard]] int important_func(void); void call(void) { int i = important_func(); } No diagnostic for the call to important_func is encouraged despite the value of i not being used. and [[nodiscard("must check armed state")]] bool arm_detonator(int); void call(void) { arm_detonator(3); detonate(); } A diagnostic for the call to arm_detonator using the string literal "must check armed state" from the attribute argument clause is encouraged. The difference isn't the types, it's the way the result is used. In the first example, the result of the function is used to initialize an object, so it's "used", even though that object's stored value is never used. In the second example, the result isn't assigned to anything. The point is that compilers aren't required to do data flow analysis; any immediate use of the result is enough to turn off the warning. (Of course compilers can warn about anything they like, and compilers commonly do warn about unused variables, but that warning isn't connected to the [[nodiscard]] attribute.) -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */