[PATCH] D66847: [analyzer] Fix analyzer warnings.
Artem Dergachev via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 27 18:44:28 PDT 2019
NoQ added a comment.
In D66847#1648209 <https://reviews.llvm.org/D66847#1648209>, @Szelethus wrote:
> if (isa<FunctionDecl>(D)) {
> const auto *FD = dyn_cast<FunctionDecl>(D); // warn: D is known to be a FunctionDecl, prefer using cast<>
> // ...
> }
>
In D66847#1648339 <https://reviews.llvm.org/D66847#1648339>, @Szelethus wrote:
> if (A) {
> const auto *B = dyn_cast_or_null<FunctionDecl>(A); // warn: A is known to be non-null, prefer dyn_cast
> }
>
Both of these are must-problems. They are hard to solve via symbolic execution because all execution paths need to be taken into account simultaneously. Eg.:
if (isa<FunctionDecl>(D)) {
if (coin)
D = getDecl(); // Maybe not a FunctionDecl.
// It's not enough to know that D is a FunctionDecl assuming coin is false.
const auto *FD = dyn_cast<FunctionDecl>(D); // no-warning:
}
We could maybe make a syntactic checker or a data flow checker to find these anti-patterns. @Charusso also had an idea for a clang-tidy checker that emits fixits for code like `dyn_cast<T>(x)->foo()` ("use 'cast' instead").
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D66847/new/
https://reviews.llvm.org/D66847
More information about the cfe-commits
mailing list