-
Notifications
You must be signed in to change notification settings - Fork 13.6k
modernize-use-designated-initializers
reported for pre-C++20 code
#83732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@llvm/issue-subscribers-clang-tidy Author: Oliver Stöneberg (firewave)
```cpp
struct S
{
int i1;
int i2;
};
void f()
<source>:9:8: warning: use designated initializer list to initialize 'S' [modernize-use-designated-initializers]
|
Interesting - I wasn't aware of that. A short test show they actually do work in C++11 mode with Clang 3.5 and GCC 5.1 which are the oldest compilers we need to support. But they do not work with Visual Studio until Unfortunately there is no AppleClang in Godbolt so I cannot test if they are available in that. |
That's not true if you enable |
We use it with C++17 now, and before that we used it even before C++11 as GCC extension. https://godbolt.org/z/77d5jM7T3 You can use it also in C: https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html |
Of course, that's disabling the warning :) A clang-tidy check should not require people to disable compiler warnings. Designated initializers is a C++20 feature according to the C++ Standard. The fact that compilers implement it earlier is a compiler extension. Compiler extensions are forbidden in certain coding standards, e.g. MISRA, AUTOSAR, etc. That's why Yes, this feature is part of the C standard since long time ago, only made it into C++ in C++20 for some reason. If this is wanted, it should be handled as an opt-in option (off by default). |
WDYT @AaronBallman @njames93 ? |
I see 2 options: |
I don't think it's a problem to recommend using designated initializers in pre-C++20 mode as that's a documented (https://clang.llvm.org/docs/LanguageExtensions.html#language-extensions-back-ported-to-previous-standards), conforming language extension we support. By default, I think we should silence That said, I think it's reasonable to give users control over whether they get the recommendation in pre-C++20 mode or not. We could use a configuration option for this, or we could explore making clang-tidy aware of |
Maybe then some generic option that would be added by default to every check to skip language checks for that check. In such case user could configure: |
Yes, a global option makes sense. I don't foresee the need to configure this on a per-check basis. I think making it smarter by checking the I suppose it's now time to bikeshed about the name :) What about |
StrictCppStandardCompliance sounds better, default true/on, but only as a config option, not as an program parameter. |
Designated initializers are a great feature. For projects and with toolchains where deviating from the standard is possible, I'd certainly consider using this feature ahead of upgrading to C++20. Unfortunately, there are plenty of scenarios where this isn't possible. It's common for software in some safety-critical domains to be locked into C++14 and GCC-based toolchains. For those projects, a check that actively breaks conforming code isn't welcome. I don't want to have to deviate from this rule; it's valid, just not applicable to me at this time. So whatever solution is chosen, please can we make it an opt-in? Other checks observe standards conformance. Casually enabling C++20 in my own project, the following well-behaved checks start to trigger:
I am grateful to the authors of those checks for making sure they apply to my combination of compiler flags. |
…tions Add StrictCStandardCompliance and StrictCppStandardCompliance options that default to true. Closes llvm#83732
…tions (llvm#94651) Add StrictCStandardCompliance and StrictCppStandardCompliance options that default to true. Closes llvm#83732 Signed-off-by: Hafidz Muzakky <[email protected]>
Running with
-std=c++11
produces the following warning:https://godbolt.org/z/EzerEWsKh
Designated initializers are not available until C++20: https://en.cppreference.com/w/cpp/language/aggregate_initialization.
CC @SimplyDanny @PiotrZSL
The text was updated successfully, but these errors were encountered: