Skip to content

"Inline Method" assist may change code semantics when arguments contain invocations #52844

Open
@DanTup

Description

@DanTup

Given this code which has an invocation as an argument to a function:

void a() {
  b(expensiveFunction());
}

void b(String a) { // Perform "Inline Method" here
  print(a);
  print(a);
}

int count = 1;
String expensiveFunction() => '${count++}';

If you use "Inline Method" on b (which maybe should be "Inline Function"?), you end up with this:

void a() {
  print(expensiveFunction());
  print(expensiveFunction());
}

int count = 1;
String expensiveFunction() => '${count++}';

expensiveFunction() is now called twice. This could be more expensive than the original code, and if the function has side-effects could change the behaviour.

Local variables should probably be introduced for such cases, although this might get noisy (and appear unnecessary) for getters?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-refactoringIssues with analysis server refactoringsdevexp-serverIssues related to some aspect of the analysis servertype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions