[clang] [FixIt] Improve Source Ranges and Fix-It Hints for Unused Lambda Captures (PR #117953)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 27 17:56:44 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (charan-003)
<details>
<summary>Changes</summary>
**Key Changes**
**Enhancements in SemaLambda.cpp:**
Updated the ExplicitCaptureRanges logic to compute more precise source ranges for unused lambda captures.
- Fixed the handling of edge cases, including:
- Trailing and leading whitespace in captures.
- Redundant commas.
- Mixed captures (e.g., [=, &a, b]).
-
**Test Improvements in fixit-unused-lambda-capture.cpp:**
- Added new tests to validate correct Fix-It suggestions for edge cases:
- Redundant commas like [a, , b].
- Leading/trailing whitespace in capture lists.
- Mixed capture styles (e.g., [=, &a]).
- Single and multiple unused captures in different scenarios.
---
Full diff: https://github.com/llvm/llvm-project/pull/117953.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaLambda.cpp (+10-3)
- (modified) clang/test/FixIt/fixit-unused-lambda-capture.cpp (+32)
``````````diff
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index a67c0b2b367d1a..e7417d1a884dcd 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1164,8 +1164,11 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
/*FunctionScopeIndexToStopAtPtr*/ nullptr,
C->Kind == LCK_StarThis);
if (!LSI->Captures.empty())
- LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
- continue;
+ {
+ SourceRange TrimmedRange = Lexer::makeFileCharRange(
+ C->ExplicitRange, SM, LangOpts);
+ LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = TrimmedRange;
+ }
}
assert(C->Id && "missing identifier for capture");
@@ -1329,7 +1332,11 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc);
}
if (!LSI->Captures.empty())
- LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
+ {
+ SourceRange TrimmedRange = Lexer::makeFileCharRange(
+ C->ExplicitRange, SM, LangOpts);
+ LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = TrimmedRange;
+}
}
finishLambdaExplicitCaptures(LSI);
LSI->ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
diff --git a/clang/test/FixIt/fixit-unused-lambda-capture.cpp b/clang/test/FixIt/fixit-unused-lambda-capture.cpp
index ce0c78d677099a..ae43d4ebbdf821 100644
--- a/clang/test/FixIt/fixit-unused-lambda-capture.cpp
+++ b/clang/test/FixIt/fixit-unused-lambda-capture.cpp
@@ -66,6 +66,38 @@ void test() {
// CHECK: [z = (n = i)] {};
[j,z = (n = i)] {};
// CHECK: [z = (n = i)] {};
+
+ // New Edge Cases
+
+ // Test 1: Leading and trailing whitespace
+ [i, j] { return i; };
+ // CHECK: [i] { return i; };
+ [i , j] { return j; };
+ // CHECK: [j] { return j; };
+ [i , j , k] { return j + k; };
+ // CHECK: [j,k] { return j + k; };
+
+ // Test 2: Single unused capture
+ [i] {};
+ // CHECK: [] {};
+ [&i] {};
+ // CHECK: [] {};
+
+ // Test 3: Multiple commas
+ [i,,j] { return j; };
+ // CHECK: [j] { return j; };
+ [,i,j,,k] { return k; };
+ // CHECK: [k] { return k; };
+
+ // Test 4: Mixed captures
+ [=, &i, j] { return i; };
+ // CHECK: [&i] { return i; };
+ [&, i] {};
+ // CHECK: [&] {};
+
+ // Test 5: Capture with comments
+ [/*capture*/ i, j] { return j; };
+ // CHECK: [/*capture*/ j] { return j; };
}
class ThisTest {
``````````
</details>
https://github.com/llvm/llvm-project/pull/117953
More information about the cfe-commits
mailing list