Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3fa5eff

Browse files
committedMay 6, 2025
Add unnecessary-virtual-specifier to -Wextra
1 parent c956ed0 commit 3fa5eff

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed
 

‎clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ Improvements to Clang's diagnostics
434434
- The ``-Wsign-compare`` warning now treats expressions with bitwise not(~) and minus(-) as signed integers
435435
except for the case where the operand is an unsigned integer
436436
and throws warning if they are compared with unsigned integers (##18878).
437-
- The ``-Wunnecessary-virtual-specifier`` warning has been added to warn about
438-
methods which are marked as virtual inside a ``final`` class, and hence can
439-
never be overridden.
437+
- The ``-Wunnecessary-virtual-specifier`` warning (included in ``-Wextra``) has
438+
been added to warn about methods which are marked as virtual inside a
439+
``final`` class, and hence can never be overridden.
440440

441441
- Improve the diagnostics for chained comparisons to report actual expressions and operators (#GH129069).
442442

‎clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,12 @@ def CXX11WarnSuggestOverride : DiagGroup<"suggest-override">;
421421
def WarnUnnecessaryVirtualSpecifier : DiagGroup<"unnecessary-virtual-specifier"> {
422422
code Documentation = [{
423423
Warns when a ``final`` class contains a virtual method (including virtual
424-
destructors). Since ``final`` classes cannot be subclassed, their methods
425-
cannot be overridden, and hence the ``virtual`` specifier is useless.
424+
destructors) that does not override anything. Since ``final`` classes cannot
425+
be subclassed, their methods cannot be overridden, so there is no point to
426+
introducing new ``virtual`` methods.
426427

427428
The warning also detects virtual methods in classes whose destructor is
428429
``final``, for the same reason.
429-
430-
The warning does not fire on virtual methods which are also marked ``override``.
431430
}];
432431
}
433432

@@ -1163,6 +1162,7 @@ def Extra : DiagGroup<"extra", [
11631162
FUseLdPath,
11641163
CastFunctionTypeMismatch,
11651164
InitStringTooLongMissingNonString,
1165+
WarnUnnecessaryVirtualSpecifier,
11661166
]>;
11671167

11681168
def Most : DiagGroup<"most", [

‎llvm/cmake/modules/HandleLLVMOptions.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,11 @@ if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
882882
# The LLVM libraries have no stable C++ API, so -Wnoexcept-type is not useful.
883883
append("-Wno-noexcept-type" CMAKE_CXX_FLAGS)
884884

885+
# LLVM has a policy of including virtual "anchor" functions to control
886+
# where the vtable is emitted. In `final` classes, these are exactly what
887+
# this warning detects: unnecessary virtual methods.
888+
add_flag_if_supported("-Wno-unnecessary-virtual-specifier" CXX_SUPPORTS_UNNECESSARY_VIRTUAL_FLAG)
889+
885890
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
886891
append("-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
887892
endif()

0 commit comments

Comments
 (0)
Failed to load comments.