Bug 120831

Summary: Raise a diagnostic when a class/struct that is marked as final introduces a virtual method
Product: gcc Reporter: Arash Partow <arash>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: enhancement CC: daniel.kruegler, webrown.cpp
Priority: P3 Keywords: diagnostic
Version: 15.2.1   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2025-06-26 00:00:00
Bug Depends on:    
Bug Blocks: 87403    

Description Arash Partow 2025-06-26 03:28:32 UTC
In clang++ v21, the following code correctly raises the diagnostic below:

  struct base
  {
      virtual ~base() = default;
      virtual void foo() {}
  };

  struct derived final : public base
  {
      void foo() override {}

      virtual void bar() {} // raise error due to this.
  };




Diagnostic

  <source>:11:18: error: virtual method 'bar' is inside a 'final' class and can never be overridden [-Werror,-Wunnecessary-virtual-specifier]
     11 |     virtual void bar() {}


Where the arguments are:

-pedantic-errors -Wall -Wextra -Werror  -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wint-in-bool-context -Wmissing-declarations -Wredundant-decls -Wundef -Wunused-function -Wnon-virtual-dtor -Woverloaded-virtual -Wno-missing-template-arg-list-after-template-kw


However afaict, G++ does not provide such a diagnostic - unless I'm possibly missing the appropriate option.

It should be noted that the -Wunnecessary-virtual-specifier seems to, for now, be clang++ specific.


https://godbolt.org/z/rPG6r8EPa
Comment 1 Jonathan Wakely 2025-06-26 10:12:47 UTC
Probably not something that will trigger often, but could be useful occasionally.