lint: simplify parameter reassignment check to avoid false negatives
R=helinx@google.com Bug: https://github.com/dart-lang/sdk/issues/61175 Change-Id: I13e0953406c6f35f92dba9a1860f40323eda30d0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/460240 Reviewed-by: Samuel Rawlins <srawlins@google.com> Reviewed-by: Phil Quitslund <pquitslund@google.com> Auto-Submit: Nishtha Jain <jnishtha305@gmail.com> Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
committed by
Commit Queue
parent
2f0724021a
commit
7e4cc3dabd
@@ -92,17 +92,22 @@ class _DeclarationVisitor extends RecursiveAstVisitor<void> {
|
||||
}
|
||||
|
||||
@override
|
||||
visitAssignmentExpression(AssignmentExpression node) {
|
||||
void visitAssignmentExpression(AssignmentExpression node) {
|
||||
if (!_isFormalParameterReassigned(parameter, node)) return;
|
||||
|
||||
if (paramIsNotNullByDefault) {
|
||||
if (_isFormalParameterReassigned(parameter, node)) {
|
||||
reportLint(node);
|
||||
}
|
||||
} else if (paramDefaultsToNull) {
|
||||
if (_isFormalParameterReassigned(parameter, node)) {
|
||||
reportLint(node);
|
||||
return;
|
||||
}
|
||||
|
||||
if (paramDefaultsToNull) {
|
||||
if (node.operator.type.lexeme == '??=') {
|
||||
if (hasBeenAssigned) {
|
||||
reportLint(node);
|
||||
}
|
||||
hasBeenAssigned = true;
|
||||
} else {
|
||||
reportLint(node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,21 @@ class ParameterAssignmentsTest extends LintRuleTest {
|
||||
@override
|
||||
String get lintRule => LintNames.parameter_assignments;
|
||||
|
||||
test_assignment_inIfElseBranches() async {
|
||||
await assertDiagnostics(
|
||||
r'''
|
||||
void foo({String? value}) {
|
||||
if (1 == 1) {
|
||||
value = ' $value';
|
||||
} else {
|
||||
value = ' $value';
|
||||
}
|
||||
}
|
||||
''',
|
||||
[lint(48, 17), lint(82, 17)],
|
||||
);
|
||||
}
|
||||
|
||||
test_assignment_nullableParameter() async {
|
||||
await assertDiagnostics(
|
||||
r'''
|
||||
|
||||
Reference in New Issue
Block a user