From 03943bbd4a468fb51b8a0102daa815c887a45711 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Thu, 11 Jan 2018 14:08:41 -0500 Subject: [PATCH] PR c++/82728 - wrong -Wunused-but-set-variable PR c++/82799 PR c++/83690 * call.c (perform_implicit_conversion_flags): Call mark_rvalue_use. * decl.c (case_conversion): Likewise. * semantics.c (finish_static_assert): Call perform_implicit_conversion_flags. From-SVN: r256550 --- gcc/cp/ChangeLog | 10 ++++++++++ gcc/cp/call.c | 5 +++++ gcc/cp/decl.c | 2 ++ gcc/cp/semantics.c | 8 +++++--- .../g++.dg/cpp0x/lambda/lambda-switch2.C | 18 ++++++++++++++++++ gcc/testsuite/g++.dg/warn/Wunused-var-27.C | 14 ++++++++++++++ gcc/testsuite/g++.dg/warn/Wunused-var-28.C | 15 +++++++++++++++ gcc/testsuite/g++.dg/warn/Wunused-var-29.C | 10 ++++++++++ 8 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-switch2.C create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-var-27.C create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-var-28.C create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-var-29.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 8731982779f..840cbd4f3e1 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,13 @@ +2018-01-11 Jason Merrill + + PR c++/82728 - wrong -Wunused-but-set-variable + PR c++/82799 + PR c++/83690 + * call.c (perform_implicit_conversion_flags): Call mark_rvalue_use. + * decl.c (case_conversion): Likewise. + * semantics.c (finish_static_assert): Call + perform_implicit_conversion_flags. + 2018-01-11 Nathan Sidwell * method.c (enum mangling_flags): Delete long-dead enum. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index c822a70a017..5f2c6becb35 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -10514,6 +10514,11 @@ perform_implicit_conversion_flags (tree type, tree expr, void *p; location_t loc = EXPR_LOC_OR_LOC (expr, input_location); + if (TREE_CODE (type) == REFERENCE_TYPE) + expr = mark_lvalue_use (expr); + else + expr = mark_rvalue_use (expr); + if (error_operand_p (expr)) return error_mark_node; diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 6ba657801d9..ee469d35137 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -3541,6 +3541,8 @@ case_conversion (tree type, tree value) if (value == NULL_TREE) return value; + value = mark_rvalue_use (value); + if (cxx_dialect >= cxx11 && (SCOPED_ENUM_P (type) || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value)))) diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 60608c797f2..f9c5285f724 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -8608,6 +8608,8 @@ void finish_static_assert (tree condition, tree message, location_t location, bool member_p) { + tsubst_flags_t complain = tf_warning_or_error; + if (message == NULL_TREE || message == error_mark_node || condition == NULL_TREE @@ -8640,9 +8642,9 @@ finish_static_assert (tree condition, tree message, location_t location, } /* Fold the expression and convert it to a boolean value. */ - condition = instantiate_non_dependent_expr (condition); - condition = cp_convert (boolean_type_node, condition, tf_warning_or_error); - condition = maybe_constant_value (condition); + condition = perform_implicit_conversion_flags (boolean_type_node, condition, + complain, LOOKUP_NORMAL); + condition = fold_non_dependent_expr (condition); if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition)) /* Do nothing; the condition is satisfied. */ diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-switch2.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-switch2.C new file mode 100644 index 00000000000..677f3056bf8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-switch2.C @@ -0,0 +1,18 @@ +// PR c++/82728 +// { dg-do compile { target c++11 } } + +void +foo () +{ + const int i = 1; + + [=]() + { + switch (0) + { + case i: + break; + } + static_assert (i, "i"); + }; +} diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-27.C b/gcc/testsuite/g++.dg/warn/Wunused-var-27.C new file mode 100644 index 00000000000..0ef71531284 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-27.C @@ -0,0 +1,14 @@ +// PR c++/82728 +// { dg-do compile } +// { dg-options "-Wunused-but-set-variable" } + +void +foo () +{ + const int i = 1; // { dg-bogus "set but not used" } + switch (0) + { + case i: + break; + } +} diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-28.C b/gcc/testsuite/g++.dg/warn/Wunused-var-28.C new file mode 100644 index 00000000000..69601671535 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-28.C @@ -0,0 +1,15 @@ +// PR c++/82799 +// { dg-do compile } +// { dg-options "-Wunused-but-set-variable" } + +enum E { b }; +struct C { + template + int foo () + { + const bool i = 0; // { dg-bogus "set but not used" } + const int r = i ? 7 : 9; + return r; + } + void bar () { foo (); } +}; diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-29.C b/gcc/testsuite/g++.dg/warn/Wunused-var-29.C new file mode 100644 index 00000000000..24eeb958f4a --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-29.C @@ -0,0 +1,10 @@ +// PR c++/83690 +// { dg-do compile { target c++11 } } +// { dg-options "-Wunused-but-set-variable" } + +void +foo () +{ + constexpr bool foo = true; // { dg-bogus "set but not used" } + static_assert (foo, "foo"); +} -- 2.30.2