Skip to content

"Inline Local Variable" does not add parens when needed for subtraction/division #61075

@DanTup

Description

@DanTup

Originally raised at Dart-Code/Dart-Code#5584 by @lukehutch.

Given the code:

void main() {
  var test = 1 + 2;
  print(3 - test);
}

The output is 0. However if you use "Inline Local" on test you get:

void main() {
  print(3 - 1 + 2);
}

Which incorrectly outputs 4.

A failing test that can be added to test\services\refactoring\legacy\inline_local_test.dart is:

  @soloTest
  Future<void> test_OK_parenthesis_plus_intoNegate() async {
    await indexTestUnit('''
void f() {
  var test = 1 + 2;
  print(3 - test);
}
''');
    _createRefactoring('test =');
    // validate change
    await assertSuccessfulRefactoring('''
void main() {
  print(3 - (1 + 2));
}
''');
  }

There's a method to determine whether to use parens here:

static bool _shouldUseParenthesis(Expression init, AstNode node) {

However it's not clear to me whether the bug is in that method of the Precedences from the expressions that are being compared (it doesn't seem like there are different Precendences for addition/subtraction?).

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-refactoringIssues with analysis server refactorings

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions