summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2017-06-02 14:00:08 +0200
committerStephan Bergmann <sbergman@redhat.com>2017-06-02 14:00:08 +0200
commitc855400e9686ddd8bcba5691393f839f6f52c966 (patch)
tree1287f7436dc391a75715752d0b1ed08c52ac44dc /compilerplugins
parent95645cbf0a220c338600130bdeca49743b252972 (diff)
Reduce loplugin:redundantcast warnings about functional casts even futher
Change-Id: Ieae9b5c9c7c6d9b8459e5d163f55d8f5024adfae
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/redundantcast.cxx9
-rw-r--r--compilerplugins/clang/test/redundantcast.cxx10
-rw-r--r--compilerplugins/clang/test/stringcopy.cxx2
3 files changed, 12 insertions, 9 deletions
diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx
index 6d740ddca883..9604365dc945 100644
--- a/compilerplugins/clang/redundantcast.cxx
+++ b/compilerplugins/clang/redundantcast.cxx
@@ -539,9 +539,12 @@ bool RedundantCast::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * exp
//
// std::initializer_list<Foo>{bar, baz}
//
- // ), at least for now:
+ // ), and only to cases where the sub-expression already is a prvalue (and
+ // thus the cast is unlikely meant to create a temporary):
auto const sub = compat::getSubExprAsWritten(expr);
- if (isa<InitListExpr>(sub) || isa<CXXStdInitializerListExpr>(sub)) {
+ if (sub->getValueKind() != VK_RValue || isa<InitListExpr>(sub)
+ || isa<CXXStdInitializerListExpr>(sub))
+ {
return true;
}
@@ -580,7 +583,7 @@ bool RedundantCast::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * exp
}
auto const t1 = expr->getTypeAsWritten();
- auto const t2 = compat::getSubExprAsWritten(expr)->getType();
+ auto const t2 = sub->getType();
if (t1 != t2)
return true;
if (!isOkToRemoveArithmeticCast(t1, t2, expr->getSubExpr()))
diff --git a/compilerplugins/clang/test/redundantcast.cxx b/compilerplugins/clang/test/redundantcast.cxx
index 1d2646a0f463..22130b32196c 100644
--- a/compilerplugins/clang/test/redundantcast.cxx
+++ b/compilerplugins/clang/test/redundantcast.cxx
@@ -143,12 +143,12 @@ void testStaticCast() {
// non-class lvalue, non-const:
int ni{};
(void) static_cast<int>(ni); // expected-error {{static_cast from 'int' lvalue to 'int' prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}}
- /* => */ (void) int(ni); //TODO: expected-error {{redundant functional cast from 'int' to 'int' [loplugin:redundantcast]}}
+ /* => */ (void) int(ni);
(void) static_cast<int &>(ni); // expected-error {{static_cast from 'int' lvalue to 'int &' lvalue is redundant [loplugin:redundantcast]}}
(void) static_cast<int &&>(ni);
(void) static_cast<int const>(ni); // expected-error {{in static_cast from 'int' lvalue to 'const int' prvalue, remove redundant top-level const qualifier [loplugin:redundantcast]}}
/* => */ (void) static_cast<int>(ni); // expected-error {{static_cast from 'int' lvalue to 'int' prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}}
- /* => */ (void) int(ni); //TODO: expected-error {{redundant functional cast from 'int' to 'int' [loplugin:redundantcast]}}
+ /* => */ (void) int(ni);
(void) static_cast<int const &>(ni); // expected-error {{static_cast from 'int' lvalue to 'const int &' lvalue should be written as const_cast [loplugin:redundantcast]}}
/* => */ (void) const_cast<int const &>(ni);
(void) static_cast<int const &&>(ni); // expected-error {{static_cast from 'int' lvalue to 'const int &&' xvalue should be written as const_cast [loplugin:redundantcast]}}
@@ -168,7 +168,7 @@ void testStaticCast() {
// non-class xvalue, non-const:
(void) static_cast<int>(nix()); // expected-error {{static_cast from 'int' xvalue to 'int' prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}}
- /* => */ (void) int(nix()); //TODO: expected-error {{redundant functional cast from 'int' to 'int' [loplugin:redundantcast]}}
+ /* => */ (void) int(nix());
// (void) static_cast<int &>(nix());
(void) static_cast<int &&>(nix()); // expected-error {{static_cast from 'int' xvalue to 'int &&' xvalue is redundant [loplugin:redundantcast]}}
(void) static_cast<int const>(nix()); // expected-error {{in static_cast from 'int' xvalue to 'const int' prvalue, remove redundant top-level const qualifier [loplugin:redundantcast]}}
@@ -209,7 +209,7 @@ void testStaticCast() {
// class lvalue, non-const:
S ns{};
(void) static_cast<S>(ns); // expected-error {{static_cast from 'S' lvalue to 'S' prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}}
- /* => */ (void) S(ns); //TODO: expected-error {{redundant functional cast from 'S' to 'S' [loplugin:redundantcast]}}
+ /* => */ (void) S(ns);
(void) static_cast<S &>(ns); // expected-error {{static_cast from 'S' lvalue to 'S &' lvalue is redundant [loplugin:redundantcast]}}
(void) static_cast<S &&>(ns);
(void) static_cast<S const>(ns); // expected-error {{static_cast from 'S' lvalue to 'const S' prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}}
@@ -232,7 +232,7 @@ void testStaticCast() {
// class xvalue, non-const:
(void) static_cast<S>(nsx()); // expected-error {{static_cast from 'S' xvalue to 'S' prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}}
- /* => */ (void) S(nsx()); //TODO: expected-error {{redundant functional cast from 'S' to 'S' [loplugin:redundantcast]}}
+ /* => */ (void) S(nsx());
// (void) static_cast<S &>(nsx());
(void) static_cast<S &&>(nsx()); // expected-error {{static_cast from 'S' xvalue to 'S &&' xvalue is redundant [loplugin:redundantcast]}}
(void) static_cast<S const>(nsx()); // expected-error {{static_cast from 'S' xvalue to 'const S' prvalue is redundant or should be written as an explicit construction of a temporary [loplugin:redundantcast]}}
diff --git a/compilerplugins/clang/test/stringcopy.cxx b/compilerplugins/clang/test/stringcopy.cxx
index 01d10ceef833..c801b7096f74 100644
--- a/compilerplugins/clang/test/stringcopy.cxx
+++ b/compilerplugins/clang/test/stringcopy.cxx
@@ -13,7 +13,7 @@
int main() {
OUString s;
- (void) OUString(s); // expected-error {{redundant copy construction from 'rtl::OUString' to 'rtl::OUString' [loplugin:stringcopy]}} expected-error {{redundant functional cast from 'rtl::OUString' to 'rtl::OUString' [loplugin:redundantcast]}}
+ (void) OUString(s); // expected-error {{redundant copy construction from 'rtl::OUString' to 'rtl::OUString' [loplugin:stringcopy]}}
using T1 = OUString;
(void) T1(s); // expected-error {{redundant copy construction from 'rtl::OUString' to 'T1' (aka 'rtl::OUString') [loplugin:stringcopy]}}
using T2 = OUString const;