Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: =?UTF-8?Q?Re=3a_=22=e2=80=9cRust_is_the_future_of_systems_programmi?= =?UTF-8?Q?ng=2c_C_is_the_new_Assembly=e2=80=9d=3a_Intel_principal_engineer?= =?UTF-8?Q?=2c_Josh_Triplett=22?= Date: Thu, 29 Aug 2019 23:35:05 +1200 Lines: 63 Message-ID: References: <0TG9F.183628$vW4.72670@fx24.am4> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net 6b1z85C83PjLZ4bBI2hXqwISeU5XUBy0Wnvh+vbWbFNAaDmpe+ Cancel-Lock: sha1:P6fi0FsC/favO64WE8UW1PXeg4E= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 In-Reply-To: Content-Language: en-US Xref: csiph.com comp.lang.c++:67996 On 29/08/2019 23:24, David Brown wrote: > On 29/08/2019 12:21, Ian Collins wrote: >> On 29/08/2019 21:02, David Brown wrote: >>> >>> One thing that I gather Rust does well, that C++ has trouble with, is >>> that when you do the Rust equivalent of std::move'ing something, any >>> attempt to use the moved-from object is flagged by the compiler.  I >>> think that would be good to spot in C++. >> >> clang-tidy does a pretty good job with "bugprone-use-after-move" >> >> > > Yes, I saw that after googling a bit. > > I would have thought, however, that pretty much any use (other than > assignment of some sort) of a named object after it is moved, is likely > to be a bug. And it is therefore something that could be warning about > by default. > > > #include > > extern int a, b, c; > > > void foo(void) { > auto p = std::make_unique(3); > a = *p; > auto q = std::move(p); > b = *q; > c = *p; > } > > > Both clang and gcc are smart enough to move the value 3 directly into > "a" and "b", and if the last line is omitted, they can remove the call > to "new" (at least in their trunk versions). Both are smart enough to > know that "c = *p;" is undefined behaviour here, and generate "ud2" > instructions on x86. > > Yet neither compiler warns about this clearly wrong behaviour despite > -Wall -Wextra. Use a bigger hammer! $ clang-tidy x.cc 1 warning generated. /tmp/x.cc:11:9: warning: Dereference of null smart pointer 'p' of type 'std::unique_ptr' [clang-analyzer-cplusplus.Move] c = *p; ^ /tmp/x.cc:9:14: note: Smart pointer 'p' of type 'std::unique_ptr' is reset to null when moved from auto q = std::move(p); ^ /tmp/x.cc:11:9: note: Dereference of null smart pointer 'p' of type 'std::unique_ptr' c = *p; -- Ian.