Which Clang warning is equivalent to Wzero-as-null-pointer-constant from GCC?
Asked Answered
P

3

6

Our project uses C++11/14, and we want to use nullptr instead of 0 or NULL with pointers, even when 0 (as an integer literal) is allowed.

I have the following code:

int main()
{
    int *ptr1 = nullptr; // #1
    int *ptr2 = 0;       // #2
}

If I compile with GCC (5.3.0) and the flag -Wzero-as-null-pointer-constant it warnings in #2, but I can't find a similar flag in Clang. If I compile the code with Clang (3.7.1) and the flag -Weverything, I don't get any warning about #2.

So, is there any way to get a similar warning for this in Clang?

Provisional answered 22/1, 2016 at 18:15 Comment(3)
It's even worse than that. In C++ you're allowed to use any integer constant expression evaluating to zero, not just a literal... e.g. !!!!!1.Hhd
@Hhd I think that was the behavior in C++98, but C++14 seems to change that. See this answer. If I understand correctly, !!!!!1 is an integral constant expression (that evaluates to false) and not an integer literal.Provisional
IIRC clang actually comes with a tool to automatically replace old null pointer constants with nullptr.Litmus
A
3

Clang doesn't support these kind of warnings (i.e., there's no -Wzero-as-null-pointer-constant equivalent in Clang). You can see it your self if you add -Weverything option (mind do it only for testing), which enables all Clang's warnings.

Live Demo

Azimuth answered 22/1, 2016 at 18:27 Comment(1)
This is no longer true.Illaudable
T
8

clang has this warning as of 5.0; I added it here.

Tuchman answered 5/5, 2017 at 16:47 Comment(0)
A
3

Clang doesn't support these kind of warnings (i.e., there's no -Wzero-as-null-pointer-constant equivalent in Clang). You can see it your self if you add -Weverything option (mind do it only for testing), which enables all Clang's warnings.

Live Demo

Azimuth answered 22/1, 2016 at 18:27 Comment(1)
This is no longer true.Illaudable
H
1

Use -Wzero-as-null-pointer-constant.

  • Available at least since clang10 (around 2017).
  • Same as gcc flag.

so bad it is not enabled by default by -Wall -Wextra...

Helmholtz answered 10/6, 2023 at 13:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.