[clang-tools-extra] [clang-tidy] Fix modernize-loop-convert by introducing space (PR #202015)

Gaurav Dhingra via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 7 05:56:57 PDT 2026


================
@@ -698,11 +698,32 @@ void LoopConvertCheck::doConversion(
       std::string ReplaceText;
       SourceRange Range = Usage.Range;
       if (Usage.Expression) {
+        // Decide whether `ReplaceText` needs to be pre-appended with a space
+        // or not. In cases like `delete*it`, we can't just change it to
+        // `deleteit` we need to introduce space after `delete` to make it
+        // `delete it`.
+        Token StarToken;
+        if (Usage.Kind == Usage::UK_Default &&
+            !Lexer::getRawToken(Usage.Range.getBegin(), StarToken,
+                                Context->getSourceManager(), getLangOpts(),
+                                false) &&
+            StarToken.is(tok::star)) {
+          std::optional<Token> PrevToken = Lexer::findPreviousToken(
+              Usage.Range.getBegin(), Context->getSourceManager(),
+              getLangOpts(), true);
+          if (PrevToken) {
----------------
gxyd wrote:

I'm getting a warning from code linter:
```
clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp:566:38: warning: unchecked access to optional value [bugprone-unchecked-optional-access]\n  566 |   const bool IsAdjacentToPrevToken = PrevToken->getEndLoc() == BeginLocation;\n      |                                      ^~~~~~~~~\nclang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp:567:10: warning: unchecked access to optional value [bugprone-unchecked-optional-access]\n  567 |   return PrevToken->isAnyIdentifier() && IsAdjacentToPrevToken;
```

should I revert back to use of `if (PrevToken)` to fix the linter warning or as an AI suggested to me to use `if (PrevToken.has_value())`?

https://github.com/llvm/llvm-project/pull/202015


More information about the cfe-commits mailing list