diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -340,6 +340,8 @@ can be controlled using ``-fcaret-diagnostics-max-lines=``. - Clang no longer emits ``-Wunused-variable`` warnings for variables declared with ``__attribute__((cleanup(...)))`` to match GCC's behavior. +- Clang now issues expected warnings for situations of comparing with NULL pointers. + (`#42992: `_) Bug Fixes in This Version ------------------------- diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -14844,7 +14844,7 @@ bool IsAddressOf = false; - if (UnaryOperator *UO = dyn_cast(E)) { + if (auto *UO = dyn_cast(E->IgnoreParens())) { if (UO->getOpcode() != UO_AddrOf) return; IsAddressOf = true; diff --git a/clang/test/Sema/conditional-expr.c b/clang/test/Sema/conditional-expr.c --- a/clang/test/Sema/conditional-expr.c +++ b/clang/test/Sema/conditional-expr.c @@ -86,7 +86,8 @@ int Postgresql(void) { char x; - return ((((&x) != ((void *) 0)) ? (*(&x) = ((char) 1)) : (void) ((void *) 0)), (unsigned long) ((void *) 0)); // expected-warning {{C99 forbids conditional expressions with only one void side}} + return ((((&x) != ((void *) 0)) ? (*(&x) = ((char) 1)) : (void) ((void *) 0)), (unsigned long) ((void *) 0)); /* expected-warning {{C99 forbids conditional expressions with only one void side}} + expected-warning {{comparison of address of 'x' not equal to a null pointer is always true}} */ } #define nil ((void*) 0) diff --git a/clang/test/Sema/warn-tautological-compare.c b/clang/test/Sema/warn-tautological-compare.c --- a/clang/test/Sema/warn-tautological-compare.c +++ b/clang/test/Sema/warn-tautological-compare.c @@ -93,3 +93,8 @@ x = array ? 1 : 0; // expected-warning {{address of array}} x = &x ? 1 : 0; // expected-warning {{address of 'x'}} } + +void test4(void) { + int *a = (void *) 0; + int b = (&a) == ((void *) 0); // expected-warning {{comparison of address of 'a' equal to a null pointer is always false}} +}