[clang] [clang][Sema] Warn on self move for inlined static cast (PR #76646)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 29 23:09:45 PST 2024
================
@@ -18843,17 +18843,26 @@ void Sema::DiagnoseSelfMove(const Expr *LHSExpr, const Expr *RHSExpr,
LHSExpr = LHSExpr->IgnoreParenImpCasts();
RHSExpr = RHSExpr->IgnoreParenImpCasts();
- // Check for a call expression
+ // Check for a call expression or static_cast expression
const CallExpr *CE = dyn_cast<CallExpr>(RHSExpr);
- if (!CE || CE->getNumArgs() != 1)
+ const auto *CXXSCE = dyn_cast<CXXStaticCastExpr>(RHSExpr);
+ if (!CE && !CXXSCE)
return;
// Check for a call to std::move
- if (!CE->isCallToStdMove())
+ if (CE && (CE->getNumArgs() != 1 || !CE->isCallToStdMove()))
return;
- // Get argument from std::move
- RHSExpr = CE->getArg(0);
+ // Check for a static_cast<T&&>(..) to an xvalue which we can treat as an
+ // inlined std::move
+ if (CXXSCE && !CXXSCE->isXValue())
+ return;
+
+ // Get argument from std::move or static_cast
+ if (CE)
+ RHSExpr = CE->getArg(0);
+ else
+ RHSExpr = CXXSCE->getSubExpr();
----------------
tbaederr wrote:
```suggestion
if (const auto *CE = dyn_cast<CallExpr>(RHSExpr);
CE && CE->getNumArgs() == 1 && CE->isCallToStdMove())
RHSExpr = CE->getArg(0);
else if (const auto *CXXSCE = dyn_cast<CXXStaticCastExpr>(RHSExpr);
CXXSCE && CXXSCE->isXValue())
RHSExpr = CXXSCE->getSubExpr();
else
return;
```
I think that would be shorter and cleaner. (This replaces the few lines above as well but I can't add a suggestion like that in Github).
https://github.com/llvm/llvm-project/pull/76646
More information about the cfe-commits
mailing list