allow some patterns in unnecessary_null_checks (#2298)
diff --git a/lib/src/rules/unnecessary_null_checks.dart b/lib/src/rules/unnecessary_null_checks.dart
index aa56c72..577bd29 100644
--- a/lib/src/rules/unnecessary_null_checks.dart
+++ b/lib/src/rules/unnecessary_null_checks.dart
@@ -68,12 +68,6 @@
rule.reportLintForToken(node.operator);
}
}
-
- void reportIfNullable(PostfixExpression node, DartType type) {
- if (type != null && context.typeSystem.isNullable(type)) {
- rule.reportLintForToken(node.operator);
- }
- }
}
DartType getExpectedType(PostfixExpression node) {
@@ -88,7 +82,12 @@
.returnType;
}
// assignment
- if (parent is AssignmentExpression) {
+ if (parent is AssignmentExpression &&
+ parent.operator.type == TokenType.EQ &&
+ (parent.leftHandSide is! Identifier ||
+ node.operand is! Identifier ||
+ (parent.leftHandSide as Identifier).name !=
+ (node.operand as Identifier).name)) {
return parent.writeType;
}
// in variable declaration
diff --git a/test/rules/experiments/nnbd/rules/unnecessary_null_checks.dart b/test/rules/experiments/nnbd/rules/unnecessary_null_checks.dart
index 7a80657..cc7703c 100644
--- a/test/rules/experiments/nnbd/rules/unnecessary_null_checks.dart
+++ b/test/rules/experiments/nnbd/rules/unnecessary_null_checks.dart
@@ -38,3 +38,19 @@
int? v;
v = i!; // LINT
}
+
+f4(int? p) {
+ int? v;
+ v ??= 1;
+ v += p!; // OK
+}
+
+autoPromote() {
+ int? v2;
+ v2 = v2!; // OK
+}
+
+f5(int? p) {
+ int? v1;
+ v1 ??= p!; // OK
+}