[clang-tools-extra] [clang-tidy] Avoid readability-use-anyofallof diagnostics on temporary ranges before C++20 (PR #185791)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 16 00:52:22 PDT 2026
================
@@ -35,6 +35,13 @@ AST_MATCHER_P(Stmt, nextStmt, ast_matchers::internal::Matcher<Stmt>,
return InnerMatcher.matches(**I, Finder, Builder);
}
+
+AST_MATCHER(Expr, isUnsupportedRangeInit) {
+ const Expr *E = Node.IgnoreParenImpCasts();
+ if (Finder->getASTContext().getLangOpts().CPlusPlus20)
+ return isa<CXXStdInitializerListExpr>(E);
----------------
zeyi2 wrote:
> I would've expected:
> ```c++
> const Expr *E = Node.IgnoreParenImpCasts();
> if (Finder->getASTContext().getLangOpts().CPlusPlus20)
> // the ranges versions will handle temporaries correctly
> return false;
>
> return E->isPRValue();
> ```
IMHO return `false` directly may be too broad, please see the comment above.
> Maybe what should happen instead is if the init is a temporary, the message is slightly different hinting at the lifetime, or do the same with a note?
Thanks for the suggestion!
A quick draft: `note: reusing the temporary range directly in the replacement may be unsafe; consider materializing it in a local variable first`?
That said, I'm a bit unsure whether this should be done in this PR or as a follow-up, as this PR is intended as a narrow fix for the current false-positive. Changing the diagnostic wording/notes would expand the scope into a design question for the check. IMO this is better tracked separately.
If you think this should be part of this PR, I can update the patch accordingly. WDYT?
https://github.com/llvm/llvm-project/pull/185791
More information about the cfe-commits
mailing list