Version 24.2.2.2, tag libreoffice-24.2.2.2
[LibreOffice.git] / compilerplugins / clang / constmove.cxx
blob63bafa188979f284c6a8ef6246b9b9542b8c0a80
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 // Find occurrences of std::move on const-qualified types. While there might theoretically be
11 // legitimate uses for such (for which this plugin would generate false positives and would need to
12 // be updated), in practice they tend to point at suspicious code that should be cleaned up in some
13 // way.
15 #ifndef LO_CLANG_SHARED_PLUGINS
17 #include "check.hxx"
18 #include "plugin.hxx"
20 namespace
22 class ConstMove final : public loplugin::FilteringPlugin<ConstMove>
24 public:
25 explicit ConstMove(loplugin::InstantiationData const& data)
26 : FilteringPlugin(data)
30 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
32 void run() override
34 if (preRun())
36 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
40 bool VisitCallExpr(CallExpr const* expr)
42 if (ignoreLocation(expr))
44 return true;
46 auto const t = expr->getType();
47 if (!t.isConstQualified())
49 return true;
51 auto const d = expr->getDirectCallee();
52 if (d == nullptr)
54 return true;
56 if (!loplugin::DeclCheck(d).Function("move").StdNamespace())
58 return true;
60 switch (expr->getNumArgs())
62 case 0:
63 return true;
64 case 1:
65 break;
66 default:
67 if (!isa<CXXDefaultArgExpr>(expr->getArg(1)))
69 return true;
71 break;
73 report(DiagnosticsEngine::Warning, "suspicious 'std::move' from %0 to const-qualified %1",
74 expr->getExprLoc())
75 << expr->getArg(0)->IgnoreImplicit()->getType() << t << expr->getSourceRange();
76 return true;
80 static loplugin::Plugin::Registration<ConstMove> constmove("constmove");
83 #endif
85 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */