Skip to content

dart fix produces invalid code with overlapping add_override and add_return_type #50668

Open
@oprypin

Description

@oprypin

How to observe the problem:

$ cat analysis_options.yaml
linter:
  rules:
    - always_declare_return_types
    - annotate_overrides
 
$ cat lib/example_lib.dart 
class A {
  foo() {
    return 5;
  }
}  
 
class B extends A {
  foo() {
    return 6;
  }
}
 
void main() {
}
 
$ dart fix --apply lib/example_lib.dart
 
example_lib.dart
  always_declare_return_types • 3 fixes
  annotate_overrides • 1 fix
 
4 fixes made in 1 file.
 
$ cat lib/example_lib.dart
class A {
  int foo() {
    return 5;
  }
}  
 
class B extends A {
  int @override
  int foo() {
    return 6;
  }
}
 
void main() {
}

The problem is that foo() {

  • got turned into
    int @override
    foo() {
    
  • but should've been
    @override
    int foo() {
    

And then due to the repeated re-run behavior of dart fix, yet another int got inserted because the previous one wasn't acknowledged.

I showed how this can be tried out with dart fix, but the problem is not specific to it, the bug already applies to the edit.bulkFixes handler itself.

The way this manifests is that the handler returns both of these fixes at the same offset, in this order:

  • offset 123, length 0, replacement '@override\n '
  • offset 123, length 0, replacement 'int '

And they get applied as is in this order.

Dart SDK version: 2.19.0-361.0.dev and also latest main

Here are some pieces of code that I suspect. I tried to fix this by changing these, but eventually gave up.

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-bulk-fixtype-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