File tree Expand file tree Collapse file tree 2 files changed +26
-15
lines changed Expand file tree Collapse file tree 2 files changed +26
-15
lines changed Original file line number Diff line number Diff line change @@ -382,17 +382,27 @@ bool eligibleForExtraction(const SelectionTree::Node *N) {
382
382
if (BinOp.parse (*N) && BinaryOperator::isAssignmentOp (BinOp.Kind ))
383
383
return false ;
384
384
385
+ const SelectionTree::Node &OuterImplicit = N->outerImplicit ();
386
+ const auto *Parent = OuterImplicit.Parent ;
387
+ if (!Parent)
388
+ return false ;
385
389
// We don't want to extract expressions used as statements, that would leave
386
390
// a `dummy;` around that has no effect.
387
391
// Unfortunately because the AST doesn't have ExprStmt, we have to check in
388
392
// this roundabout way.
389
- const SelectionTree::Node &OuterImplicit = N->outerImplicit ();
390
- if (!OuterImplicit.Parent ||
391
- childExprIsStmt (OuterImplicit.Parent ->ASTNode .get <Stmt>(),
393
+ if (childExprIsStmt (Parent->ASTNode .get <Stmt>(),
392
394
OuterImplicit.ASTNode .get <Expr>()))
393
395
return false ;
394
396
395
- // FIXME: ban extracting the RHS of an assignment: `a = [[foo()]]`
397
+ // Disable extraction of full RHS on assignment operations, e.g:
398
+ // auto x = [[RHS_EXPR]];
399
+ // This would just result in duplicating the code.
400
+ if (const auto *BO = Parent->ASTNode .get <BinaryOperator>()) {
401
+ if (BO->isAssignmentOp () &&
402
+ BO->getRHS () == OuterImplicit.ASTNode .get <Expr>())
403
+ return false ;
404
+ }
405
+
396
406
return true ;
397
407
}
398
408
Original file line number Diff line number Diff line change @@ -215,26 +215,26 @@ TEST_F(ExtractVariableTest, Test) {
215
215
int x = [[1]], y = [[a + 1]], a = [[1]], z = a + 1;
216
216
// if without else
217
217
if([[1]])
218
- a = [[1]];
218
+ a = [[1]] + 1 ;
219
219
// if with else
220
220
if(a < [[3]])
221
221
if(a == [[4]])
222
- a = [[5]];
222
+ a = [[5]] + 1 ;
223
223
else
224
- a = [[5]];
224
+ a = [[5]] + 1 ;
225
225
else if (a < [[4]])
226
- a = [[4]];
226
+ a = [[4]] + 1 ;
227
227
else
228
- a = [[5]];
228
+ a = [[5]] + 1 ;
229
229
// for loop
230
- for(a = [[1]]; a > [[[[3]] + [[4]]]]; a++)
231
- a = [[2]];
230
+ for(a = [[1]] + 1 ; a > [[[[3]] + [[4]]]]; a++)
231
+ a = [[2]] + 1 ;
232
232
// while
233
233
while(a < [[1]])
234
- a = [[1]];
234
+ a = [[1]] + 1 ;
235
235
// do while
236
236
do
237
- a = [[1]];
237
+ a = [[1]] + 1 ;
238
238
while(a < [[3]]);
239
239
}
240
240
)cpp" ;
@@ -291,6 +291,7 @@ TEST_F(ExtractVariableTest, Test) {
291
291
xyz([[a *= 5]]);
292
292
// Variable DeclRefExpr
293
293
a = [[b]];
294
+ a = [[xyz()]];
294
295
// statement expression
295
296
[[xyz()]];
296
297
while (a)
@@ -373,10 +374,10 @@ TEST_F(ExtractVariableTest, Test) {
373
374
})cpp" },
374
375
// attribute testing
375
376
{R"cpp( void f(int a) {
376
- [ [gsl::suppress("type")] ] for (;;) a = [[1]];
377
+ [ [gsl::suppress("type")] ] for (;;) a = [[1]] + 1 ;
377
378
})cpp" ,
378
379
R"cpp( void f(int a) {
379
- auto dummy = 1; [ [gsl::suppress("type")] ] for (;;) a = dummy;
380
+ auto dummy = 1; [ [gsl::suppress("type")] ] for (;;) a = dummy + 1 ;
380
381
})cpp" },
381
382
// MemberExpr
382
383
{R"cpp( class T {
You can’t perform that action at this time.
0 commit comments