Skip to content

Commit 91ffb74

Browse files
authored
Fix exception with isExactlyType (#659)
Fixes "Null check operator used on a null value" errors
1 parent 25bdcbb commit 91ffb74

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

source_gen/lib/src/type_checker.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,17 @@ abstract class TypeChecker {
174174
bool isExactly(Element element);
175175

176176
/// Returns `true` if representing the exact same type as [staticType].
177-
bool isExactlyType(DartType staticType) => isExactly(staticType.element!);
177+
///
178+
/// This will always return false for types without a backingclass such as
179+
/// `void` or function types.
180+
bool isExactlyType(DartType staticType) {
181+
final element = staticType.element;
182+
if (element != null) {
183+
return isExactly(element);
184+
} else {
185+
return false;
186+
}
187+
}
178188

179189
/// Returns `true` if representing a super class of [element].
180190
///

source_gen/test/type_checker_test.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void main() {
3131
late TypeChecker staticMapMixinChecker;
3232
late TypeChecker staticHashMapChecker;
3333
late TypeChecker staticEnumChecker;
34+
late LibraryElement core;
3435

3536
// Resolved top-level types from package:source_gen.
3637
late InterfaceType staticGenerator;
@@ -45,7 +46,6 @@ void main() {
4546
late InterfaceType staticMyEnumWithMixin;
4647

4748
setUpAll(() async {
48-
late LibraryElement core;
4949
late LibraryElement collection;
5050
late LibraryReader sourceGen;
5151
late LibraryElement testSource;
@@ -184,6 +184,17 @@ void main() {
184184
);
185185
});
186186

187+
group(
188+
'isExactlyType',
189+
() {
190+
test('should not crash with null element', () {
191+
final voidType = core.typeProvider.voidType;
192+
expect(voidType.element, isNull);
193+
expect(checkMapMixin().isExactlyType(voidType), isFalse);
194+
});
195+
},
196+
);
197+
187198
group(
188199
'(MapMixin',
189200
() {

0 commit comments

Comments
 (0)