Skip to content

Dart analyzer doesn't report error when class member is used inside lambda in factory #43610

@PiN73

Description

@PiN73

Consider this example:

class Car {
  int price;

  Car(this.price);

  factory Car.create() {
    print(price);
    return Car(123);
  }
}

Here price is non-static class member and can't be accessed from factory constructor. So the analyzer correctly reports an error:

error: Instance members can't be accessed from a factory constructor. (instance_member_access_from_factory)

However if we try to access the member from lambda or inner function

class Car {
  int price;

  Car(this.price);

  factory Car.create() {
    [4, 5, 6].where((x) => x == price).forEach(print);
    void f() {
      print(price);
    }
    f();
    return Car(123);
  }
}

analyzer won't report any errors.

Though compiler reports the problem correctly

Error: Getter not found: 'price'.

and aborts compiling

Dart SDK version: 2.9.2 (stable) (Wed Aug 26 12:44:28 2020 +0200) on "windows_x64"

Metadata

Metadata

Assignees

Labels

P3A lower priority bug or feature requestdart-model-analyzer-specIssues with the analyzer's implementation of the language speclegacy-area-analyzerUse area-devexp instead.type-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