Skip to content

Commit

Permalink
fix unnecessary_null_aware_assignments property access false positive (
Browse files Browse the repository at this point in the history
  • Loading branch information
pq authored Mar 10, 2022
1 parent 4eaae25 commit 63d2e1e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/src/rules/unnecessary_null_aware_assignments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';

import '../analyzer.dart';
import '../util/dart_type_utilities.dart';
Expand Down Expand Up @@ -55,6 +56,9 @@ class _Visitor extends SimpleAstVisitor<void> {

@override
void visitAssignmentExpression(AssignmentExpression node) {
if (node.readElement is PropertyAccessorElement) return;
if (node.writeElement is PropertyAccessorElement) return;

if (node.operator.type == TokenType.QUESTION_QUESTION_EQ &&
DartTypeUtilities.isNullLiteral(node.rightHandSide)) {
rule.reportLint(node);
Expand Down
7 changes: 7 additions & 0 deletions test_data/rules/unnecessary_null_aware_assignments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ class X {
x ??= 1; //OK
}
}

int? get x => null;
set x(int? x) { }

void f() {
x ??= null; //OK
}

0 comments on commit 63d2e1e

Please sign in to comment.