4 ms·
AFAIK most compilers by default will output a warning in this case.
by gruez 5y ago
AFAIK most compilers by default will output a warning in this case.
- jeffbee 5y agoI don't see any warnings for this narrowing parameter conversion. #include "stddef.h" short foo(short a) { return a % 42; } size_t bar(void) { size_t sz = ~0UL; return foo(sz); } https://godbolt.org/z/3ec9v8Pa4 https://godbolt.org/z/3ec9v8Pa4
- xeeeeeeeeeeenu 5y agoIt warns if you add -Wconversion. Unfortunately, that flag generates lots of false positives (at least in gcc), so using it isn't always a good idea.
- jeffbee 5y agoIt isn't ever a good idea. C programmers are just supposed to know about the conversion and promotion rules, every time they add a line, even though the rules are actually insanely complicated. Compiler warnings can't overcome this because programmers have no way of just discovering one of GCC's 726 warning flags, only a few of which are enabled by -Wall and -Wextra, most of which are way too noisy to serve a useful purpose.
- morio 5y agoWhat kind of false positives are you seeing with gcc? Personally I have never seen gcc spitting out a false positive. IMO it's always a good idea to explicitly downcast even if you know that it's 'safe'. That way someone else will see instantly what's going on. The fact that Rust requires it should tell us something.
- xeeeeeeeeeeenu 5y agoFor example, I've just reported this: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101537 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101537 You can find more cases in the bugtracker. To be fair, it seems many of them were fixed in recent releases.
- Cloudef 5y agoThe 1 and 0 are ints. It most likely complaints because the sign isn't same.
- ototot 5y agoIn my own small projects, I always add -Wconversion to build configuration. I think the false positive is affordable when you start from small piece of code.
- saurik 5y agoYeah; AFAIK you need something like clang-tidy's cppcoreguidelines-narrowing-conversions check (which everyone should definitely be using). (edit: But I'm wrong! That check is apparently similar to the -Wconversion mentioned by someone else.)
- gruez 5y agoOn MSVC, default project settings I get: warning C4267: 'argument': conversion from 'size_t' to 'short', possible loss of data https://godbolt.org/z/nYeWT7zv6 https://godbolt.org/z/nYeWT7zv6 (/W3 is the default warning level when creating a new project) I saw these warnings so often that I assumed that every compiler had them.
- im3w1l 5y agoToo bad that most projects are so full of integer size and signedness warnings that people get warning fatigue and just completely ignore them.
- loeg 5y agoGCC and Clang (the predominant Linux and Mac compilers) mostly don’t warn by default, you need -Wall or other flags (-Wextra, -Weverything, or specific flags like -Wconversion).
- nly 5y agoThey can't warn by default, because on some platforms long->int isnt a narrowing conversion.
- aaronmdjones 5y agoThe compiler knows what platform it's compiling the code for, though.
- usefulcat 5y agoGCC's -Wconversion has some issues. For example, good luck getting gcc to /not/ emit a warning for this code, in C or C++. I have yet to find the appropriate cast to avoid a warning. Clang does not warn for this. typedef struct { unsigned value : 4; } S; void foo(S* s, unsigned value) { // error: conversion from 'unsigned int' to 'unsigned char:4' may change value s->value = value; } I mean, I guess I can see the rationale.. it's just annoying to have to resort to using pragmas to turn off -Wconversion whenever I need to assign to a bitfield.
- Denvercoder9 5y agoDoes it still warn if you do `s->value = value & 0x0F`? That seems like a reasonable alternative to pragmas if it works.