Skip to content

[clang-tidy] Improve bugprone-multi-level-implicit-pointer-conversion #94524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ AST_MATCHER(ImplicitCastExpr, isMultiLevelPointerConversion) {
return SourcePtrLevel != TargetPtrLevel;
}

AST_MATCHER(QualType, isPointerType) {
const QualType Type =
Node.getCanonicalType().getNonReferenceType().getUnqualifiedType();

return !Type.isNull() && Type->isPointerType();
}

} // namespace

void MultiLevelImplicitPointerConversionCheck::registerMatchers(
MatchFinder *Finder) {
Finder->addMatcher(
implicitCastExpr(hasCastKind(CK_BitCast), isMultiLevelPointerConversion())
implicitCastExpr(hasCastKind(CK_BitCast), isMultiLevelPointerConversion(),
unless(hasParent(explicitCastExpr(
hasDestinationType(isPointerType())))))
.bind("expr"),
this);
}
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ Changes in existing checks
check by ignoring ``__func__`` macro in lambda captures, initializers of
default parameters and nested function declarations.

- Improved :doc:`bugprone-multi-level-implicit-pointer-conversion
<clang-tidy/checks/bugprone/multi-level-implicit-pointer-conversion>` check
by ignoring implicit pointer conversions that are part of a cast expression.

- Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
<clang-tidy/checks/bugprone/non-zero-enum-to-bool-conversion>` check by
eliminating false positives resulting from direct usage of bitwise operators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ void test()

takeSecondLevelVoidPtr(getSecondLevelVoidPtr());
}

namespace PR93959 {
void free(void*);

void test() {
char **p = nullptr;
free(p);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: multilevel pointer conversion from 'char **' to 'void *', please use explicit cast [bugprone-multi-level-implicit-pointer-conversion]
free((void *)p);
free(static_cast<void *>(p));
}
}
Loading