llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

-Wbraced-scalar-init warns inappropriately

Open Leengit opened this issue 4 years ago • 4 comments

Braces around the returned value serve a legitimate purpose in

char f()
{
  int i = 65537;
  return{ i };
}

They tell the compiler to fail because the return value is narrowed; without the braces this would compile. Yet, -Wbraced-scalar-init warns that this is a superfluous use of braces. Likewise if the type of i is instead char the braces legitimately ask the compiler to check for narrowing, which in this case is not occurring; in this situation too, -Wbraced-scalar-init should not be warning that this is a superfluous use of braces. (In practical cases, the returned value might be from a function that has a return type that is not so readily apparent and the code writer should instead be encouraged to use braces in situations where narrowing is a concern!)

This occurs with both:

clang version 10.0.0-4ubuntu1 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin

and:

Ubuntu clang version 12.0.0-3ubuntu1~20.04.5 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin

Leengit avatar Apr 01 '22 16:04 Leengit

Also seeing this warning for our code base. I have a hard time understanding what the purpose of this warning is. It causes people to remove code protecting from narrowing conversions to get rid of the warning, effectively making code worse and more error-prone.

oold avatar Mar 20 '26 16:03 oold

Narrowing is ill-formed and should be a hard error: https://godbolt.org/z/fT1G9rqsW

gcc is the only one not to make it so, I am guessing at one point clang had this as a warning but this is no longer the case.

shafik avatar Apr 02 '26 03:04 shafik

@shafik your link shows the issue; we emit the narrowing warning (which defaults to an error for us) correctly, but we then go on to emit warning: braces around scalar initializer [-Wbraced-scalar-init]. That diagnostic exists to tell users about superfluous braces around a scalar initializer: https://godbolt.org/z/6jdjq3vYW but it seems odd to emit it in this case because the braces are intentional.

We are inconsistent with triggering the braced scalar init diagnostic; it sure seems like int a = { 12 }; should get the same diagnostic under the same logic of "the language accepts this but the braces are not necessary". I suspect we don't emit the diagnostic in this case because people use the braces to catch narrowing issues most often in this form of an initializer, but that same logic applies in all the other cases as well. Given that C++03 is still well-used in the wild, I think it makes sense that we continue to support -Wbraced-scalar-init (because narrowing conversions aren't an issue before C++11: https://godbolt.org/z/3jrTj1Tjj) but I wonder if that diagnostic should be off by default or split into two groups? I think it's reasonable to silence the diagnostic in code like char foo(int i) { return {i}; } or char c = {some_int}; on the assumption that the braces are intentional at this point, but maybe it seems less reasonable for code like struct S { char x; } s = { { 1000 } }; where the extra braces could more plausibly unintentional?

AaronBallman avatar Apr 02 '26 11:04 AaronBallman

See also #57286 about -Wbraced-scalar-init.

MagentaTreehouse avatar Apr 02 '26 14:04 MagentaTreehouse