Skip to content

Extract Method creates an unnecessary parameter for variableΒ #59775

@DanTup

Description

@DanTup

This was originally raised at Dart-Code/Dart-Code#5373 by @stephane-archer.

If you extract code that sets a variable, it becomes a return value for the extracted method. The original variable is also passed as an argument, even if it has never been read in the extracted code and could just be declared inside the new function.

For example:

void f() {
  int? i;
  i = 1; // extract this line
  print(i);
}

Becomes:

void f() {
  int? i;
  i = g(i);
  print(i);
}

int? g(int? i) {
  i = 1;
  return i;
}

But since i is not read by the extracted method (only written), the parameter could be dropped and instead the variable just declared inside the extracted function:

void f() {
  int? i;
  i = g();
  print(i);
}

int? g() {
  int? i = 1;
  return i;
}

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 refactoringstype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions