-Wsuggest-override warns for virtual functions that could use the override
keyword.  But as everyone in this PR agrees, the warning shouldn't suggest
adding "override" for "final" functions.  This trivial patch implements that.

Bootstrapped/regtested on x86_64-linux, ok for trunk/9?

2019-05-06  Marek Polacek  <pola...@redhat.com>

        PR c++/78010 - bogus -Wsuggest-override warning on final function.
        * class.c (check_for_override): Don't warn for final functions.

        * g++.dg/warn/Wsuggest-override-2.C: New test.

diff --git gcc/cp/class.c gcc/cp/class.c
index 712169ce7e7..a47777cdd9e 100644
--- gcc/cp/class.c
+++ gcc/cp/class.c
@@ -2780,7 +2780,9 @@ check_for_override (tree decl, tree ctype)
     {
       DECL_VINDEX (decl) = decl;
       overrides_found = true;
-      if (warn_override && !DECL_OVERRIDE_P (decl)
+      if (warn_override
+         && !DECL_OVERRIDE_P (decl)
+         && !DECL_FINAL_P (decl)
          && !DECL_DESTRUCTOR_P (decl))
        warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
                    "%qD can be marked override", decl);
diff --git gcc/testsuite/g++.dg/warn/Wsuggest-override-2.C 
gcc/testsuite/g++.dg/warn/Wsuggest-override-2.C
new file mode 100644
index 00000000000..4948902e930
--- /dev/null
+++ gcc/testsuite/g++.dg/warn/Wsuggest-override-2.C
@@ -0,0 +1,9 @@
+// PR c++/78010
+// { dg-options "-std=c++11 -Wsuggest-override" }
+
+struct A {
+  virtual void f();
+};
+struct B : A {
+  void f() final; // { dg-bogus "can be marked override" }
+};

Reply via email to