Avoid unhelpful loplugin:stringview from implicit conversions

> compilerplugins/clang/test/stringview.cxx Line 149: instead of an 'rtl::OString' constructed from a 'const rtl::OStringLiteral<4>', pass a 'std::string_view' [loplugin:stringview]
> compilerplugins/clang/test/stringview.cxx Line 150: instead of an 'rtl::OUString' constructed from a 'const rtl::OUStringLiteral<4>', pass a 'std::u16string_view' [loplugin:stringview]

(see the discussion at <https://gerrit.libreoffice.org/c/core/+/124950>
"operator==(OString, OStringLiteral)")

Change-Id: I5481bef8b8bad7bf9bdf4fff29161f0809051f2b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/124990
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Stephan Bergmann
2021-11-10 16:26:05 +01:00
committed by Noel Grandin
parent 48ae3cc1af
commit 0ac45af39c
2 changed files with 19 additions and 12 deletions

View File

@@ -75,18 +75,18 @@ bool StringView::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* cxxOperator
auto op = cxxOperatorCallExpr->getOperator(); auto op = cxxOperatorCallExpr->getOperator();
if (op == OO_Plus && cxxOperatorCallExpr->getNumArgs() == 2) if (op == OO_Plus && cxxOperatorCallExpr->getNumArgs() == 2)
{ {
handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(0))); handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(0));
handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(1))); handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(1));
} }
if (compat::isComparisonOp(cxxOperatorCallExpr)) if (compat::isComparisonOp(cxxOperatorCallExpr))
{ {
handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(0))); handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(0));
handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(1))); handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(1));
} }
else if (op == OO_PlusEqual) else if (op == OO_PlusEqual)
handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(1))); handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(1));
else if (op == OO_Subscript) else if (op == OO_Subscript)
handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(0))); handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(0));
else if (op == OO_Equal) else if (op == OO_Equal)
{ {
if (loplugin::TypeCheck(cxxOperatorCallExpr->getType()) if (loplugin::TypeCheck(cxxOperatorCallExpr->getType())
@@ -98,7 +98,7 @@ bool StringView::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* cxxOperator
.Namespace("rtl") .Namespace("rtl")
.GlobalNamespace()) .GlobalNamespace())
{ {
handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(1))); handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(1));
} }
} }
return true; return true;
@@ -130,7 +130,8 @@ bool StringView::VisitImplicitCastExpr(ImplicitCastExpr const* expr)
void StringView::handleSubExprThatCouldBeView(Expr const* subExpr) void StringView::handleSubExprThatCouldBeView(Expr const* subExpr)
{ {
auto const e = subExpr->IgnoreParens(); auto const e0 = compat::IgnoreImplicit(subExpr);
auto const e = e0->IgnoreParens();
auto const tc = loplugin::TypeCheck(e->getType()); auto const tc = loplugin::TypeCheck(e->getType());
if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace() if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace()
|| tc.Class("OUString").Namespace("rtl").GlobalNamespace())) || tc.Class("OUString").Namespace("rtl").GlobalNamespace()))
@@ -138,9 +139,12 @@ void StringView::handleSubExprThatCouldBeView(Expr const* subExpr)
return; return;
} }
if (auto const e1 = dyn_cast<CXXConstructExpr>(e)) if (auto const e1 = dyn_cast<CXXConstructExpr>(e))
{
if (e0 == subExpr)
{ {
handleCXXConstructExpr(e1); handleCXXConstructExpr(e1);
} }
}
else if (auto const e2 = dyn_cast<CXXFunctionalCastExpr>(e)) else if (auto const e2 = dyn_cast<CXXFunctionalCastExpr>(e))
{ {
auto e3 = e2->getSubExpr(); auto e3 = e2->getSubExpr();
@@ -301,11 +305,11 @@ bool StringView::VisitCXXMemberCallExpr(CXXMemberCallExpr const* expr)
auto const dc = loplugin::DeclCheck(expr->getMethodDecl()); auto const dc = loplugin::DeclCheck(expr->getMethodDecl());
if (dc.Function("append") || dc.Function("indexOf") || dc.Function("lastIndexOf")) if (dc.Function("append") || dc.Function("indexOf") || dc.Function("lastIndexOf"))
{ {
handleSubExprThatCouldBeView(compat::IgnoreImplicit(expr->getArg(0))); handleSubExprThatCouldBeView(expr->getArg(0));
} }
else if (dc.Function("insert")) else if (dc.Function("insert"))
{ {
handleSubExprThatCouldBeView(compat::IgnoreImplicit(expr->getArg(1))); handleSubExprThatCouldBeView(expr->getArg(1));
} }
return true; return true;
} }
@@ -330,7 +334,7 @@ bool StringView::VisitCXXConstructExpr(CXXConstructExpr const* expr)
return true; return true;
} }
if (expr->getNumArgs() > 0) if (expr->getNumArgs() > 0)
handleSubExprThatCouldBeView(compat::IgnoreImplicit(expr->getArg(0))); handleSubExprThatCouldBeView(expr->getArg(0));
return true; return true;
} }

View File

@@ -145,6 +145,9 @@ void f5(char const* s1, sal_Int32 n1, char16_t const* s2, sal_Int32 n2, OString
call_view(OUString(OUString::number(0))); call_view(OUString(OUString::number(0)));
// expected-error-re@+1 {{instead of an 'rtl::OUString' constructed from a 'typename std::enable_if_t<ToStringHelper<OUString>::allowOUStringConcat && ToStringHelper<OUString>::allowOUStringConcat, OUStringConcat<OUString, OUString>{{ ?}}>' (aka 'rtl::OUStringConcat<rtl::OUString, rtl::OUString>'), pass a 'std::u16string_view' via 'rtl::OUStringConcatenation' [loplugin:stringview]}} // expected-error-re@+1 {{instead of an 'rtl::OUString' constructed from a 'typename std::enable_if_t<ToStringHelper<OUString>::allowOUStringConcat && ToStringHelper<OUString>::allowOUStringConcat, OUStringConcat<OUString, OUString>{{ ?}}>' (aka 'rtl::OUStringConcat<rtl::OUString, rtl::OUString>'), pass a 'std::u16string_view' via 'rtl::OUStringConcatenation' [loplugin:stringview]}}
call_view(OUString(s4 + s4)); call_view(OUString(s4 + s4));
(void)(s3 == l1);
(void)(s4 == l2);
} }
void f5(OUString s) void f5(OUString s)