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

Add code excerpts to new type promotion failures #5359

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/non_promotion/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
This package isn't an app. It's a collection of code excerpts used in
dart.dev/tools/non-promotion-reasons.
This package isn't an app or sample.
It's a collection of code excerpts used
in https://dart.dev/tools/non-promotion-reasons.
1 change: 1 addition & 0 deletions examples/non_promotion/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../dart_test_base.yaml
123 changes: 119 additions & 4 deletions examples/non_promotion/lib/non_promotion.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// ignore_for_file: expected_executable, missing_statement
// ignore_for_file: unused_local_variable, unused_element
// ignore_for_file: prefer_function_declarations_over_variables
// #docregion not-field, getter-name
import 'dart:math';
// #enddocregion not-field, getter-name
// #docregion mock
import 'package:mockito/mockito.dart';
// #enddocregion mock

class C1 {
int? i;
Expand Down Expand Up @@ -84,15 +90,14 @@ void miscDeclAnalyzedButNotTested() {

{
void f(int? i, int? j) {
// #docregion catch-null-check
// #enddocregion catch-null-check
if (i == null) return;
try {
i = j; // (1)
// ... Additional code ...
if (i == null) return; // (2)
// ... Additional code ...
// #docregion catch-null-check
// ...
} catch (e) {
if (i != null) {
print(i.isEven); // (3) OK due to the null check in the line above.
Expand All @@ -106,15 +111,14 @@ void miscDeclAnalyzedButNotTested() {

{
void f(int? i, int? j) {
// #docregion catch-bang
// #enddocregion catch-bang
if (i == null) return;
try {
i = j; // (1)
// ... Additional code ...
if (i == null) return; // (2)
// ... Additional code ...
// #docregion catch-bang
// ...
} catch (e) {
print(i!.isEven); // (3) OK because of the `!`.
}
Expand Down Expand Up @@ -242,3 +246,114 @@ void miscDeclAnalyzedButNotTested() {
// #enddocregion closure-write-capture
}
}

// #docregion this
extension Ext on int? {
int get valueOrZero {
final self = this;
return self == null ? 0 : self;
}
}
// #enddocregion this

// #docregion private, unrelated, mock
class Example {
final int? _i;
Example(this._i);
}
// #enddocregion unrelated, mock

void test(Example x) {
if (x._i != null) {
print(x._i + 1); // OK
}
}
// #enddocregion private

// #docregion final
class A {
final int? _immutablePrivateField;
A(this._immutablePrivateField);

void f() {
if (_immutablePrivateField != null) {
int i = _immutablePrivateField; // OK
}
}
}
// #enddocregion final

// #docregion not-field
abstract class B {
int? get _i => Random().nextBool() ? 123 : null;
}

void testB(B b) {
final i = b._i;
if (i != null) {
print(i.isEven); // OK
}
}
// #enddocregion not-field

// #docregion external
class E {
external final int? _externalField;

void f() {
final i = _externalField;
if (i != null) {
print(i.isEven); // OK
}
}
}
// #enddocregion external

// #docregion getter-name, field-name
class D {
final int? _overridden;
D(this._overridden);
}
// #enddocregion field-name

class Override implements D {
@override
int? get _overridden => Random().nextBool() ? 1 : null;
}
// #enddocregion getter-name

// #docregion field-name
class Override2 implements D {
@override
int? _overridden;
}
// #enddocregion field-name

// #docregion getter-name, field-name
void testD(D d) {
final i = d._overridden;
if (i != null) {
print(i.isEven); // OK
}
}
// #enddocregion getter-name, field-name

// #docregion unrelated
class Unrelated {
int? get _j => Random().nextBool() ? 1 : null;
}
// #enddocregion unrelated

// #docregion mock
class MockExample extends Mock implements Example {
@override
late final int? _i;
}

// #docregion unrelated
void f(Example x) {
if (x._i != null) {
int i = x._i; // OK
}
}
// #enddocregion mock, unrelated
3 changes: 3 additions & 0 deletions examples/non_promotion/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ version: 0.0.1
environment:
sdk: ^3.2.0

dependencies:
mockito: ^5.4.4

dev_dependencies:
lints: ^3.0.0