skip to content

With GCC, what do -Wconversion and -Wshadow catch that -Wall -Wextra miss, and why does C++ need -Wsign-conversion separately?

level: middleimportance: should knowfreq 50%

answer

  1. value-changing implicit conversions
  2. an explicit cast is the opt-out
  3. C++ splits off signedness
  4. locals hiding members or outer names
  5. -Wshadow=local and compatible-local

basics

~20 s

-Wconversion warns on implicit conversions that may change a value, such as double to int; -Wshadow warns when a declaration hides another variable, parameter or class member. In C++, GCC's -Wconversion skips signed/unsigned changes unless -Wsign-conversion is enabled explicitly.

solid answer

~40 s

`-Wconversion` reports implicit conversions that may alter a value: real to integer, to a smaller type, and (in C) signed to unsigned. It stays quiet for explicit casts like `static_cast<int>(x)` and when the value cannot change. For C++, the GCC manual says signed/unsigned warnings "are disabled by default in C++ unless `-Wsign-conversion` is explicitly enabled", so `unsigned flags = -1;` needs that flag too. `-Wshadow` warns when a local or type declaration hides another variable, parameter or class member; `-Wshadow=local` and `-Wshadow=compatible-local` narrow it to locals. Neither flag is in `-Wall` or `-Wextra`, and on older code both are noisy, so teams add them deliberately. Clang differs: its `-Wconversion` already includes `-Wsign-conversion`.

code

cpp · 15 lines
cpp
#include <cstdio>
#include <vector>

int main() {
    std::vector<int> ids{7, 8, 9};
    double ratio = 2.5;
    int scaled = ratio * 4;                    // -Wconversion (value may change)
    int exact = static_cast<int>(ratio * 4);   // explicit cast: no warning
    unsigned flags = -1;                       // C++: needs -Wsign-conversion
    for (int id : ids) {
        int scaled = id * 2;                   // -Wshadow: hides the outer 'scaled'
        std::printf("%d ", scaled);
    }
    std::printf("\n%d %d %u\n", scaled, exact, flags);
}

go deeper

for a junior

Recall that conversion and shadowing warnings are opt-in, and that an explicit cast is how you mark a conversion as intended.

for a middle

Explain the C++ signedness split between -Wconversion and -Wsign-conversion, and the narrower -Wshadow forms.

for a senior

Discuss which of these flags pays off on an existing codebase, how noisy each is, and how Clang's different grouping changes the counts.

for a principal

Decide which opt-in flags join the team baseline and which stay advisory, weighing bug yield against churn in old code.

## Why these warnings are opt-in `-Wall` and `-Wextra` are curated sets of warnings about code that is easy to fix. Conversion and shadowing warnings fire on a lot of code that is correct, so GCC leaves them out of both. They are still among the most useful flags to add: a truncating conversion or a shadowed name compiles silently and produces a wrong value at run time. ## What -Wconversion reports The GCC 16 manual: "Warn for implicit conversions that may alter a value." Its own examples cover three families: - **real to integer**, like `abs(x)` when `x` is `double` - **to a smaller type**, like `sqrtf(M_PI)` (a `double` argument to a `float` parameter) - **signed to unsigned**, like `unsigned ui = -1` (in C) It does not warn when: - the conversion is **explicit**: `static_cast<int>(x)` or `(unsigned)-1` - the value **cannot change**: `abs(2.0)` - arithmetic on a small type is converted back to that type; that needs the separate `-Warith-conversion` In C++ it also warns about some confusing overload resolution for user-defined conversions, and about conversion operators that can never be used. ## The C++ signedness split One sentence in the manual matters for every C++ codebase: "Warnings about conversions between signed and unsigned integers are disabled by default in C++ unless `-Wsign-conversion` is explicitly enabled." In C, `-Wconversion` enables `-Wsign-conversion`; in C++ it does not. A C++ team that wants `unsigned flags = -1;` or `int` indices passed where `size_t` is expected reported must pass both: ```bash g++ -std=c++23 -Wall -Wextra -Wconversion -Wsign-conversion -c pricing.cpp ``` `-Wsign-conversion` is usually the noisiest flag of all on older code, because loop counters and container sizes mix `int` and `size_t` everywhere. Related flags: | Flag | Reports | Enabled by | |---|---|---| | `-Wfloat-conversion` | loss of floating-point precision, including real to integer | `-Wconversion` | | `-Wsign-conversion` | implicit sign changes | `-Wconversion` in C only | | `-Wsign-compare` | signed/unsigned comparisons | `-Wall` in C++ | | `-Wnarrowing` | narrowing inside `{}` initialization | `-Wall` in C++ | | `-Wold-style-cast` | C-style casts in C++ | nothing; opt-in | ## What -Wshadow reports `-Wshadow` warns "whenever a local variable or type declaration shadows another variable, parameter, type, class member (in C++) ... or whenever a built-in function is shadowed." In C++ that covers the classic bugs: - a loop body redeclaring a variable from the enclosing scope, so the outer one never changes - a constructor parameter named like the member it initializes - a local hiding a namespace-scope variable The manual adds a C++ subtlety: a local shadowing an explicit `typedef` is reported, but one shadowing a struct, class or enum name is not. Narrower forms exist for teams that find the full flag too noisy: 1. `-Wshadow=global` is the same as `-Wshadow`: any shadowing. 2. `-Wshadow=local` reports a local shadowing another local or parameter. 3. `-Wshadow=compatible-local` reports only when the two types are compatible, the case most likely to be a silent bug. When plain `-Wshadow` is on, the manual says `-Wno-shadow=local` and `-Wno-shadow=compatible-local` are ignored. ## The repair, not just the silence - For a deliberate conversion, write it with `static_cast`. That silences the warning and documents the intent in a form `-Wold-style-cast` accepts. - For a real bug, change the type: use `std::size_t` for indices, or a wider type for the result. - For shadowing, rename. Teams often adopt a member naming convention so that constructor parameters cannot collide. ## Clang is not the same here Clang accepts the same flag names, but its groups differ. Running Apple clang 21 on the example below, its `-Wconversion` reported the `unsigned flags = -1;` line as a sign conversion without `-Wsign-conversion`, which GCC's C++ front end would not. A build that targets both compilers sees different counts from the same flags.

  • Why might a team enable -Wconversion but not -Wsign-conversion?
    Signed/unsigned mixing between `int` loop counters and `size_t` container sizes is everywhere in older C++, so `-Wsign-conversion` can produce thousands of reports that are mostly harmless. Value-changing conversions such as `double` to `int` are rarer and more often real bugs, so they are the cheaper win.
  • Does -Wshadow warn when a constructor parameter has the same name as the member it initializes?
    Yes in GCC: the manual lists class members among what a local declaration can shadow in C++, so `Sprite(int width) : width(width)` is reported. The code is correct, which is why many teams pick a member naming convention or use `-Wshadow=local` instead.

  • -Wconversion in C++ also reports every signed/unsigned change
  • An explicit static_cast still triggers -Wconversion
  • -Wshadow is part of -Wall
  • -Wconversion warns even when the value cannot change
  • Clang and GCC report identical sets for -Wconversion