Skip to content

False positive from library_private_types_in_public_api #59383

Closed
@eernstg

Description

@eernstg

Consider the following program:

// ----- Library 'public.dart'.

class _Private {
  final int i;
  const _Private(this.i);
}

extension type const Public._(_Private it) implements _Private { // LINT            <----
  const Public.small() : this._(const _Private(10));
  const Public.large() : this._(const _Private(100));
  factory Public.negative(int i) {
    if (i >= 0) throw ArgumentError("Must be negative");
    return Public._(_Private(i));
  }
}

// ----- Some other library
import 'public.dart';

// Can create instances of `_Private` using the "public face" `Public`,
// but only in very specific ways.
// Can use `Public` as the static type of instances of `_Private`.

void main() {
  const c1 = Public.small();
  const c2 = Public.large();
  var v1 = Public.negative(-1);
  Public v2 = c1;
  print(v2.i); // '10'.
}

The program gives rise to a diagnostic message from library_private_types_in_public_api at the formal parameter it of the primary constructor Public._:

Invalid use of a private type in a public API.

However, a private constructor should probably not be considered to be a public API.

Also, the overall purpose of the code seems legitimate (that is, providing access for other libraries to a private class as a type, and providing controlled access to the creation of instances of the private class), which could serve as an additional argument why the lint should not flag this kind of situation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions