linter icon indicating copy to clipboard operation
linter copied to clipboard

[join_return_with_assignment] Lint on local variables

Open Levi-Lesches opened this issue 7 months ago • 0 comments

Describe the issue

Right now this very simple pattern isn't flagged:

int getAge() {
  final age = ageCalculator();
  return age;
}

This could be flagged and quick-fixed to:

int getAge() {
  return ageCalculator();
}

Which would then be flagged by prefer_expression_function_bodies and quick-fixed to:

int getAge() => ageCalculator();

But since the first snippet isn't flagged, this isn't suggested. It's not the end of the world but it would be nice.

This would also catch the case where the local variable is more descriptive than the function name itself! That's often done in cases like this:

String parsePath(String path) {
  final parts = path.split("/");
  final filename = parts.last;
  final fileParts = filename.split(".");
  final fileExtension - fileParts.last;
  return fileExtension;  // lint here
}

By suggesting that the user remove fileExtension and return fileParts.last directly, the user will notice their descriptive name is now gone, and will look for a way to preserve that information, hopefully realizing they should rename their function to getExtension instead.

Levi-Lesches avatar Jul 18 '24 22:07 Levi-Lesches