[clang-tools-extra] d04c2ed - [clang-tidy] Improve documentation of bugprone-pointer-arithmetic-on-polymorphic-object (#108324)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 12 21:35:28 PDT 2024
Author: Mainak Sil
Date: 2024-09-13T00:35:24-04:00
New Revision: d04c2ed60c6a1acef75b0cfff6f1bf9a5a7bea89
URL: https://github.com/llvm/llvm-project/commit/d04c2ed60c6a1acef75b0cfff6f1bf9a5a7bea89
DIFF: https://github.com/llvm/llvm-project/commit/d04c2ed60c6a1acef75b0cfff6f1bf9a5a7bea89.diff
LOG: [clang-tidy] Improve documentation of bugprone-pointer-arithmetic-on-polymorphic-object (#108324)
Fix #107831.
Added:
Modified:
clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
Removed:
################################################################################
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
index 1884acd5f12b3e..95509ef3c724de 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.rst
@@ -19,20 +19,28 @@ Example:
.. code-block:: c++
struct Base {
- virtual void ~Base();
+ virtual ~Base();
+ int i;
};
struct Derived : public Base {};
- void foo() {
- Base *b = new Derived[10];
-
+ void foo(Base* b) {
b += 1;
// warning: pointer arithmetic on class that declares a virtual function can
// result in undefined behavior if the dynamic type
diff ers from the
// pointer type
+ }
+
+ int bar(const Derived d[]) {
+ return d[1].i; // warning due to pointer arithmetic on polymorphic object
+ }
- delete[] static_cast<Derived*>(b);
+ // Making Derived final suppresses the warning
+ struct FinalDerived final : public Base {};
+
+ int baz(const FinalDerived d[]) {
+ return d[1].i; // no warning as FinalDerived is final
}
Options
@@ -47,17 +55,9 @@ Options
.. code-block:: c++
- void bar() {
- Base *b = new Base[10];
+ void bar(Base b[], Derived d[]) {
b += 1; // warning, as Base declares a virtual destructor
-
- delete[] b;
-
- Derived *d = new Derived[10]; // Derived overrides the destructor, and
- // declares no other virtual functions
d += 1; // warning only if IgnoreVirtualDeclarationsOnly is set to false
-
- delete[] d;
}
References
More information about the cfe-commits
mailing list