Skip to content

Add unnecessary-virtual-specifier to -Wextra #138741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ Improvements to Clang's diagnostics
- The ``-Wsign-compare`` warning now treats expressions with bitwise not(~) and minus(-) as signed integers
except for the case where the operand is an unsigned integer
and throws warning if they are compared with unsigned integers (##18878).
- The ``-Wunnecessary-virtual-specifier`` warning has been added to warn about
methods which are marked as virtual inside a ``final`` class, and hence can
never be overridden.
- The ``-Wunnecessary-virtual-specifier`` warning (included in ``-Wextra``) has
been added to warn about methods which are marked as virtual inside a
``final`` class, and hence can never be overridden.

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

Expand Down
8 changes: 4 additions & 4 deletions clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,12 @@ def CXX11WarnSuggestOverride : DiagGroup<"suggest-override">;
def WarnUnnecessaryVirtualSpecifier : DiagGroup<"unnecessary-virtual-specifier"> {
code Documentation = [{
Warns when a ``final`` class contains a virtual method (including virtual
destructors). Since ``final`` classes cannot be subclassed, their methods
cannot be overridden, and hence the ``virtual`` specifier is useless.
destructors) that does not override anything. Since ``final`` classes cannot
be subclassed, their methods cannot be overridden, so there is no point to
introducing new ``virtual`` methods.

The warning also detects virtual methods in classes whose destructor is
``final``, for the same reason.

The warning does not fire on virtual methods which are also marked ``override``.
}];
}

Expand Down Expand Up @@ -1163,6 +1162,7 @@ def Extra : DiagGroup<"extra", [
FUseLdPath,
CastFunctionTypeMismatch,
InitStringTooLongMissingNonString,
WarnUnnecessaryVirtualSpecifier,
]>;

def Most : DiagGroup<"most", [
Expand Down
1 change: 1 addition & 0 deletions libcxx/src/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wtautological-constant-out-of-range-compare")
_LIBCPP_BEGIN_NAMESPACE_STD

namespace {
// Whitespace change DO NOT MERGE

// handle all next_prime(i) for i in [1, 210), special case 0
const unsigned small_primes[] = {
Expand Down
5 changes: 5 additions & 0 deletions llvm/cmake/modules/HandleLLVMOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,11 @@ if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
# The LLVM libraries have no stable C++ API, so -Wnoexcept-type is not useful.
append("-Wno-noexcept-type" CMAKE_CXX_FLAGS)

# LLVM has a policy of including virtual "anchor" functions to control
# where the vtable is emitted. In `final` classes, these are exactly what
# this warning detects: unnecessary virtual methods.
add_flag_if_supported("-Wno-unnecessary-virtual-specifier" CXX_SUPPORTS_UNNECESSARY_VIRTUAL_FLAG)

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
append("-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
endif()
Expand Down