Is this the type of things that could be caught by a linter or strict compilation rules? This seems to be to be a failure of the type system.
Is this the type of things that could be caught by a linter or strict compilation rules? This seems to be to be a failure of the type system.
https://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidel...
But strict compilation rules (eg, clang's -Weverything) mainly only work if you treat them as errors (so -Werror), and then some of those strict are then also questionable at best, and just outright annoyingly wrong at worst. For example, unused parameter warnings on virtual methods are a waste of time to deal with. It's not a symptom of a bug most of the time, so it being an error just generates workaround churn or you end up just disabling the warning and then maybe that bites you the few times it would have pointed out an actual issue.
Beyond the blanket ones like clang's -Weverything, it can otherwise be a job to keep up with compiler upgrades and the vast number of warning options they have.
Why is that even a warning? If at least one of the implementers use a parameter and a warning is shown the warning itself is wrong. That’s just broken implementation of the warning?
#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/3ec9v8Pa4Personally 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.
You can find more cases in the bugtracker. To be fair, it seems many of them were fixed in recent releases.
warning C4267: 'argument': conversion from 'size_t' to 'short', possible loss of data
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.
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.https://clang.llvm.org/docs/DiagnosticsReference.html#wshort...
The more general-purpose -Wconversion has many false positives, often around int to char conversion. Some functions like int toupper(int) have an unexpected int return type to deal with special out-of-bound values like EOF.
And it's because the kernel does a lot of non-standard things, mostly because it has to. It is not a normal program.