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

-Wbraced-scalar-init false positive with designated initializer

Open dwblaikie opened this issue 4 years ago • 10 comments

https://godbolt.org/z/61Ksso41s

struct Foo {
  int x;
};

void test() {
  Foo f1{.x{3}};
  Foo f2 = {.x{3}};
  (void)f1;
  (void)f2;
}
<source>:6:12: warning: braces around scalar initializer [-Wbraced-scalar-init]
  Foo f1{.x{3}};
           ^~~
            
<source>:7:15: warning: braces around scalar initializer [-Wbraced-scalar-init]
  Foo f2 = {.x{3}};
              ^~~

dwblaikie avatar Aug 23 '22 00:08 dwblaikie

@llvm/issue-subscribers-c-1

llvmbot avatar Aug 23 '22 00:08 llvmbot

Wow, at a minimum I am not a fan of the diagnostic message for -Wbraced-scalar-init -- it tells me what the code does, not why it's a problem let alone how to address it.

Can you help me understand what this diagnostic flag is intended to protect against? That might help me to better understand why you think this instance is a false positive. (From my playing around with it... it seems like everything is a false positive because it's just saying "you're doing this thing the language allows you to do.")

AaronBallman avatar Aug 23 '22 14:08 AaronBallman

@llvm/issue-subscribers-c

llvmbot avatar Aug 23 '22 14:08 llvmbot

Looks like @zygoloid improved this around 420fa12dfddc33b0d0b54666340bfa630b61b5bc & focussed on redundant braces, so it warns on things like return {1} on a function with an int return, or func({1}) where func takes an int parameter.

Looks like parts of the warning date back as far as f8ecff2a94023ae4e58205c9af4c64595d7f3083 (ah, the halcyon days of 2008, where people checked in incomplete code undef #ifdef 0 so they could work on it from home... )

dwblaikie avatar Aug 23 '22 19:08 dwblaikie

It's defaulted on in Clang probably since then, so for the better part of 15 years (doesn't mean it's right - it does look rather stylistic at best, but I don't know/haven't looked in more detail) - and this report came from an internal (Google) developer & I wanted to forward it along externally, but I don't have much invested in this/haven't formed any strong opinion on whether the warning is good/bad/etc.

dwblaikie avatar Aug 23 '22 19:08 dwblaikie

Thanks for the historical context! I agree that this seems like a false-positive in C++ where braced initialization is a thing.

I still question whether this diagnostic adds a whole lot of value or not, but that's a different bug report. ;-)

AaronBallman avatar Aug 23 '22 20:08 AaronBallman

I still question whether this diagnostic adds a whole lot of value or not, but that's a different bug report. ;-)

@AaronBallman, where would the value be? Isn't it warning about perfectly valid code?


Incidentally, in the following the warning is a bit inconsistent.

#include <string>

enum class Race { Hobbit, Elf };

struct Character {
    std::string name{};
    char const* alias{};
    int age{};
    Race race{};
};

using namespace std::literals;

int main() {
    {
        Character const legolas{
            .name{"Legolas"}}; // no warning
    }
    {
        Character const legolas{
            .name{"Legolas"s}}; // no warning
    }
    {
        Character const legolas{
            .alias{"Greenleaf"}}; // warning
    }
    {
        Character const legolas{
            .age{999}}; // warning
    }
    {
        Character const legolas{
            .race{Race::Elf}}; // warning
    }  
}

(Compiler Explorer demo)

Aster89 avatar Sep 27 '23 16:09 Aster89

Just came here to report this, and found it had already been reported two years ago. As far as I can see, this has been an issue since Clang 17.

The warning is supposedly intended to catch C-style initialisation in the form:

int x = {123};

(see https://stackoverflow.com/a/3462768/1678468)

The fundamental problem, IMO, is that it should simply not apply in any circumstance where there isn't an = accompanying it.

Below is an example showing it occurring with designated initialisers, where it's a spurious warning:

#include <string>

struct my_struct {
  int a;
  std::string b;
  struct {
    int c;
    int d;
  } composite;
};

auto main()->int {

  my_struct first{
    .a = 123,
    .b = "testing",
    .composite = {
      .c = 321,
      .d = 999,
    },
  };

  my_struct second{
    .a{123},           // warning: braces around scalar initializer
    .b{"testing"},
    .composite{
      .c{321},         // warning: braces around scalar initializer
      .d{999},         // warning: braces around scalar initializer
    },
  };

  return EXIT_SUCCESS;
}

https://godbolt.org/z/sGYzGfWcj

In the context above, any .a{123} appears to be treated as int a = {123}, triggering the warning. It should be treated as equivalent to int a{123} instead, which should not generate any warnings.

slowriot avatar Nov 10 '24 13:11 slowriot

The fundamental problem, IMO, is that it should simply not apply in any circumstance where there isn't an = accompanying it.

This feels correct to me. The designated initializer code is largely focused on C, so I suspect we need to have some extra handling for C++ in this case.

AaronBallman avatar Nov 12 '24 19:11 AaronBallman

Previously reported here #53423

MagentaTreehouse avatar Jan 06 '25 00:01 MagentaTreehouse