Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract variable refactor should create variables inside of switch statement #52015

Closed
stereotype441 opened this issue Apr 11, 2023 · 2 comments
Assignees
Labels
analyzer-refactoring area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@stereotype441
Copy link
Member

Given the following Dart 3.0 code:

test(Object o) {
  switch (o) {
    case int():
      print(o + 1);
  }
}

If I select the text o + 1 and perform an "extract variable" refactor, I get:

test(Object o) {
  var object = o + 1;
  switch (o) {
    case int():
      print(object);
  }
}

Which has a compile time error, because at the site of var object = o + 1; the variable o is not known to be an int.

I would have expected this instead:

test(Object o) {
  switch (o) {
    case int():
      var object = o + 1;
      print(object);
  }
}
@stereotype441 stereotype441 added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-refactoring labels Apr 11, 2023
@bwilkerson
Copy link
Member

I agree. I would have expected the same behavior in pre-3.0 code. The variable should be declared as close as possible to the use site, typically on the preceding line.

But the example did make me curious about how we handle switch expressions, so I tried

int f(Object o) {
  return switch (o) {
    int() => o + 1,
    _ => 0,
  };
}

and found that it (not surprisingly) defines the variable before the return. It isn't valid to declare the variable in the expression, so I'm wondering whether we ought to just disable the assist in switch expressions (unless we can prove that the code to be generated would be error-free).

@keertip keertip added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) P2 A bug or feature request we're likely to work on labels Apr 12, 2023
@scheglov scheglov self-assigned this Apr 13, 2023
@scheglov
Copy link
Contributor

copybara-service bot pushed a commit that referenced this issue Apr 13, 2023
Bug: #52015
Change-Id: I069ecfbd2be5375bdba9977872bc9e0c88431f62
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/295100
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-refactoring area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants