<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/123066>123066</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang-tidy - a dynamic_cast triggers clang-analyzer-core.CallAndMessage warning
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
fekir
</td>
</tr>
</table>
<pre>
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.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyslcFu4zYQhp9mfBnYoElLtg46yEoM7GF76G4PPQWUOJK4oUmXpBx4D3n2gpYdJ6mL7qFBAAnmzHz__BxSMgTdW6ISsi1kDzM5xsH5sqNn7WeNU6eydjZoRR47Z4x70bbHYPXhQBGBb4YYDwFEBXwHfNc71TgTF873wHc_ge9sU3QDPf74C3gBrAJWvb6-vqY3LhR12hL-8e3x6eHP36qvX-qnuvr2HVgVoh_biI0MBOstsAoR8ah9HKXBo9MKO-eAb1JR8Xn99Zz2bhHWD9PLpawir4-kQFRX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