Issue 123066
Summary clang-tidy - a dynamic_cast triggers clang-analyzer-core.CallAndMessage warning
Labels
Assignees
Reporter fekir
    Consider following snippet (https://godbolt.org/z/nb9fheEjq)

~~~~
#define USE_DYNAMIC_CAST
struct base{
    virtual void foo();
    virtual ~base();
};
struct derived:base{};
struct derived2:base{};

void bar(base* b){
    #ifdef USE_DYNAMIC_CAST
    auto* d = dynamic_cast<derived*>(b);
    if(d){
        d->foo();
    }
 #endif
    b->foo();
}
~~~~

depending if `USE_DYNAMIC_CAST` is defined or not, clang-tidy triggers following warning


~~~~
[<source>:39:5: warning: Called C++ object pointer is null [clang-analyzer-core.CallAndMessage]]
   39 |     b->foo();
      | ^
[<source>:34:8: note: Assuming 'd' is null]
   34 |     if(d)
      | ^
[<source>:34:5: note: Taking false branch]
   34 |     if(d)
      | ^
[<source>:39:5: note: Called C++ object pointer is null]
   39 | b->foo();
~~~~

It seems to imply, that if `d` is `nullptr`, then also `b` must have been null, which is not true.

Removing the check `if(d)` also silences the warning, but since `b` does not necessarily point to a `derived` object, it would be the wrong thing to do.

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to