[clang] Warn about virtual methods in `final` classes (PR #131188)
Devon Loehr via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 14 11:25:55 PDT 2025
================
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunnecessary-virtual-specifier %s
+
+struct Foo final {
+ Foo() = default;
+ virtual ~Foo() = default; // expected-warning {{virtual method}}
+ virtual Foo& operator=(Foo& other) = default; // expected-warning {{virtual method}}
+ virtual Foo& operator=(Foo&& other) = default; // expected-warning {{virtual method}}
+ void f();
+ virtual void f(int); // expected-warning {{virtual method}}
+ int g(int x) { return x; };
+ virtual int g(bool); // expected-warning {{virtual method}}
+ static int s();
+};
+
+struct BarBase {
+ virtual ~BarBase() = delete;
+ virtual void virt() {}
+ virtual int virt(int);
+ int nonvirt();
+};
+
+struct Bar final : BarBase {
+ ~Bar() override = delete;
+ void virt() override {};
+ // `virtual ... override;` is a common pattern, so don't warn
+ virtual int virt(int) override;
+ virtual int virt(bool); // expected-warning {{virtual method}}
+ int nonvirt();
+};
----------------
DKLoehr wrote:
Got it, changed.
https://github.com/llvm/llvm-project/pull/131188
More information about the cfe-commits
mailing list