Bug 120831 - Raise a diagnostic when a class/struct that is marked as final introduces a virtual method
Summary: Raise a diagnostic when a class/struct that is marked as final introduces a v...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 15.2.1
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2025-06-26 03:28 UTC by Arash Partow
Modified: 2025-06-29 10:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2025-06-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.