Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 5e51483

Browse files
committed
fix: support tear-off methods for check-unnecessary-nullable
1 parent 67e9f21 commit 5e51483

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* fix: ignore method invocations in a variable declaration for [`prefer-moving-to-variable`](https://dartcodemetrics.dev/docs/rules/common/prefer-moving-to-variable).
1212
* feat: add `allow-initialized` option to [`avoid-late-keyword`](https://dartcodemetrics.dev/docs/rules/common/avoid-late-keyword).
1313
* feat: add `ignored-types` option to [`avoid-late-keyword`](https://dartcodemetrics.dev/docs/rules/common/avoid-late-keyword).
14+
* fix: support tear-off methods for `check-unnecessary-nullable`.
1415

1516
## 5.4.0
1617

lib/src/analyzers/unnecessary_nullable_analyzer/invocations_visitor.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import 'package:analyzer/dart/ast/ast.dart';
44
import 'package:analyzer/dart/ast/visitor.dart';
55
import 'package:analyzer/dart/element/element.dart';
6+
import 'package:analyzer/dart/element/type.dart';
67

78
import 'models/invocations_usage.dart';
89

@@ -20,6 +21,15 @@ class InvocationsVisitor extends RecursiveAstVisitor<void> {
2021
}
2122
}
2223

24+
@override
25+
void visitPrefixedIdentifier(PrefixedIdentifier node) {
26+
super.visitPrefixedIdentifier(node);
27+
28+
if (node.identifier.staticType is FunctionType) {
29+
_recordUsedElement(node.identifier.staticElement, null);
30+
}
31+
}
32+
2333
@override
2434
void visitMethodInvocation(MethodInvocation node) {
2535
super.visitMethodInvocation(node);
@@ -42,7 +52,7 @@ class InvocationsVisitor extends RecursiveAstVisitor<void> {
4252
}
4353

4454
/// Records use of a not prefixed [element].
45-
void _recordUsedElement(Element? element, ArgumentList arguments) {
55+
void _recordUsedElement(Element? element, ArgumentList? arguments) {
4656
if (element == null) {
4757
return;
4858
}
@@ -52,6 +62,7 @@ class InvocationsVisitor extends RecursiveAstVisitor<void> {
5262
return;
5363
}
5464
// Remember the element.
55-
invocationsUsages.addElementUsage(element, {arguments});
65+
final usedArguments = arguments == null ? null : {arguments};
66+
invocationsUsages.addElementUsage(element, usedArguments);
5667
}
5768
}

lib/src/analyzers/unnecessary_nullable_analyzer/models/invocations_usage.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ import 'package:analyzer/dart/element/element.dart';
55
/// elements.
66
class InvocationsUsage {
77
/// The set of referenced top-level elements.
8-
final Map<Element, Set<ArgumentList>> elements = {};
8+
final Map<Element, Set<ArgumentList>?> elements = {};
99

1010
final Set<String> exports = {};
1111

12-
void addElementUsage(Element element, Set<ArgumentList> expressions) {
12+
void addElementUsage(Element element, Set<ArgumentList>? expressions) {
1313
elements.update(
1414
element,
15-
(value) => value..addAll(expressions),
15+
(value) {
16+
if (expressions == null || value == null) {
17+
return null;
18+
}
19+
20+
return value..addAll(expressions);
21+
},
1622
ifAbsent: () => expressions,
1723
);
1824
}

test/resources/unnecessary_nullable_analyzer/nullable_method_parameters.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ class ClassWithMethods {
2222

2323
// ignore: unnecessary-nullable
2424
void ignoredAlwaysNonNullable(String? anotherValue) {}
25+
26+
void tearOff(String? anotherValue) {}
2527
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class MyWidget extends StatelessWidget {
2-
const MyWidget(Key? key) : super(key);
2+
const MyWidget(super.key);
33
}
44

55
class Widget {
@@ -9,9 +9,15 @@ class Widget {
99
}
1010

1111
class StatelessWidget extends Widget {
12-
const StatelessWidget(Key? key) : super(key);
12+
const StatelessWidget(super.key);
1313
}
1414

1515
class Key {}
1616

1717
class GlobalKey extends Key {}
18+
19+
class AnotherWidget extends Widget {
20+
final void Function(String?) onSubmit;
21+
22+
const AnotherWidget({required this.onSubmit}) : super(null);
23+
}

test/resources/unnecessary_nullable_analyzer/unnecessary_nullable_example.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ void main() {
1313

1414
withMethods
1515
..someMethod(null)
16-
..someMethod('value');
16+
..someMethod('value')
17+
..tearOff('str');
1718

1819
withMethods.alwaysNonNullable('anotherValue');
1920
withMethods.ignoredAlwaysNonNullable('anotherValue');
@@ -44,6 +45,8 @@ void main() {
4445
multipleParametersWithOptional('name', 1, 'secondName');
4546

4647
MyWidget(GlobalKey());
48+
49+
AnotherWidget(onSubmit: withMethods.tearOff);
4750
}
4851

4952
class _Test {

0 commit comments

Comments
 (0)