You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -315,6 +315,51 @@ If the resulting values are used in a context where they control memory accesses
315
315
316
316
For C++ warnings about conversions between signed and unsigned integers are disabled by default unless `-Wsign-conversion` is explicitly enabled.
317
317
318
+
This warning only applies where a data conversion may lead to data loss. C and C++ conditionals (such as after an "if" statement) accept types other than booleans, for example, integers (where non-zero is interpreted as true) and pointers (where non-NULL is interpreted as true). This interpretation of truthiness could be considered a data type conversion (in a sense), but this option does not create spurious warnings about these perfectly reasonable constructs.
319
+
320
+
#### Additional Considerations
321
+
322
+
On large, brown-field code bases the `-Wconversion` option can generate hundreds or thousands of warnings, many of which are benign or stem from idiomatic C patterns. The GCC wiki notes that `-Wconversion`*"is designed for a niche of uses […] where the programmer is willing to accept and workaround invalid warnings."*[^Taylor2012] Because of the volume of diagnostics, developers may start ignoring all warnings, defeating the purpose of the flag. For such projects it can be more pragmatic to start with the narrower `-Wsign-conversion` or to enable `-Wconversion` only for selected translation units.
323
+
324
+
Consequently we recommend that green-field projects (new code) enable `-Wconversion` from day one and keep the build warning-free. Add the flag to continuous-integration (CI) checks so new patches cannot re-introduce conversions.
325
+
326
+
For brown-field projects (existing code) we recommend a staged rollout:
327
+
328
+
1. Build once with `-Wconversion` without failing the build (omit `-Werror`) to record a baseline.
329
+
2. Suppress known-benign patterns with `#pragma GCC diagnostic ignored "-Wconversion"` or targeted `-Wno-conversion` flags.
330
+
3. Triage remaining warnings, prioritising high-risk ones (conversions that influences array indexing, object size calculations, or security-sensitive logic) and refactor them or make the casts explicit.
331
+
4. Gradually tighten the flag: warning → error → part of CI.
332
+
333
+
##### Removing implicit casts
334
+
335
+
Where practical, implicit casts that cause data loss should be refactored so that they are unnecessary. For example, they can be:
336
+
337
+
- Eliminated by harmonizing the types
338
+
- Rewritten to reflect the programmer’s intent clearly (e.g., clamping values instead of truncating bits).
339
+
340
+
If such a cast is necessary, convert it from an implicit cast to an explicit cast. Each such cast should be justified with a comment. A problem with replacing implicit conversions with explicit C-style casts is that they can introduce new bugs when used incorrectly. For example, if an implicit cast from a 32-bit value to a 16-bit short is mistakenly replaced with a cast to an 8-bit char, this truncates more bits and misrepresents intent.
341
+
342
+
##### Warning noise from third-party headers
343
+
344
+
Warnings may originate from system or third-party headers (e.g., Linux headers from `/usr/include`). These should not block analysis of your own code. In such cases, wrap the includes with diagnostic pragmas[^Feske21]:
345
+
346
+
~~~C
347
+
/*
348
+
* Disable -Wconversion warnings caused by host headers
349
+
*/
350
+
#pragma GCC diagnostic push
351
+
#pragma GCC diagnostic ignored "-Wconversion"
352
+
353
+
#include<sys/cdefs.h>
354
+
#include<linux/futex.h>
355
+
...
356
+
357
+
#pragma GCC diagnostic pop /* restore -Wconversion warnings */
358
+
~~~
359
+
360
+
[^Taylor2012]: Taylor, Ian Lance, [The new Wconversion option](https://gcc.gnu.org/wiki/NewWconversion), GCC Wiki, 2012-01-06.
361
+
[^Feske21]: Feske, Norman, [Let's make -Wconversion our new friend!](https://genodians.org/nfeske/2021-12-07-wconversion), Genode Labs, 2021-12-07.
362
+
318
363
---
319
364
320
365
### Enable warning about trampolines that require executable stacks
0 commit comments