[clang] Warn about virtual methods in `final` classes (PR #131188)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 13 12:19:57 PDT 2025
================
@@ -0,0 +1,27 @@
+// 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 int virt(int) override; // expected-warning {{virtual method}}
----------------
cor3ntin wrote:
I'm not sure the warning is useful at all when the function is an override (especially in code that is old enough not to have used override from the get-go). And there is no downside to marking them virtual - nor do I think it's confusing.
https://github.com/llvm/llvm-project/pull/131188
More information about the cfe-commits
mailing list