Skip to content

Commit e95b1f7

Browse files
Make UNNECESSARY_NULL_AWARE_CALL a warning
Change-Id: I0cbe9522dcddb4c664ae5679e521191d0c91d777 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106385 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Mike Fairhurst <[email protected]>
1 parent 2c26ece commit e95b1f7

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ const List<ErrorCode> errorCodeValues = const [
369369
HintCode.UNDEFINED_SHOWN_NAME,
370370
HintCode.UNNECESSARY_CAST,
371371
HintCode.UNNECESSARY_NO_SUCH_METHOD,
372-
HintCode.UNNECESSARY_NULL_AWARE_CALL,
373372
HintCode.UNNECESSARY_TYPE_CHECK_FALSE,
374373
HintCode.UNNECESSARY_TYPE_CHECK_TRUE,
375374
HintCode.UNUSED_CATCH_CLAUSE,
@@ -693,6 +692,7 @@ const List<ErrorCode> errorCodeValues = const [
693692
StaticWarningCode.UNDEFINED_IDENTIFIER,
694693
StaticWarningCode.UNDEFINED_IDENTIFIER_AWAIT,
695694
StaticWarningCode.UNDEFINED_NAMED_PARAMETER,
695+
StaticWarningCode.UNNECESSARY_NULL_AWARE_CALL,
696696
StaticWarningCode.UNNECESSARY_NULL_AWARE_SPREAD,
697697
StaticWarningCode.USE_OF_VOID_RESULT,
698698
StaticWarningCode.UNCHECKED_USE_OF_NULLABLE_VALUE,

pkg/analyzer/lib/src/dart/error/hint_codes.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -803,15 +803,6 @@ class HintCode extends ErrorCode {
803803
'UNNECESSARY_NO_SUCH_METHOD', "Unnecessary 'noSuchMethod' declaration.",
804804
correction: "Try removing the declaration of 'noSuchMethod'.");
805805

806-
/**
807-
* When the '?.' operator is used on a target that we know to be non-null,
808-
* it is unnecessary.
809-
*/
810-
static const HintCode UNNECESSARY_NULL_AWARE_CALL = const HintCode(
811-
'UNNECESSARY_NULL_AWARE_CALL',
812-
"The target expression cannot be null, and so '?.' is not necessary.",
813-
correction: "Replace the '?.' with a '.' in the invocation.");
814-
815806
/**
816807
* Unnecessary type checks, the result is always false.
817808
*/

pkg/analyzer/lib/src/error/codes.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4931,6 +4931,15 @@ class StaticWarningCode extends ErrorCode {
49314931
'Try changing the type, or casting, to a more useful type like'
49324932
' dynamic');
49334933

4934+
/**
4935+
* When the '?.' operator is used on a target that we know to be non-null,
4936+
* it is unnecessary.
4937+
*/
4938+
static const StaticWarningCode UNNECESSARY_NULL_AWARE_CALL =
4939+
const StaticWarningCode('UNNECESSARY_NULL_AWARE_CALL',
4940+
"The target expression cannot be null, and so '?.' is not necessary.",
4941+
correction: "Replace the '?.' with a '.' in the invocation.");
4942+
49344943
/**
49354944
* It is a static warning to assign void to any non-void type in dart.
49364945
* compile-time error). Report that error specially for a better user

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5580,16 +5580,15 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
55805580

55815581
ErrorCode errorCode;
55825582
if (operator.type == TokenType.QUESTION_PERIOD) {
5583-
errorCode = HintCode.UNNECESSARY_NULL_AWARE_CALL;
5583+
errorCode = StaticWarningCode.UNNECESSARY_NULL_AWARE_CALL;
55845584
} else if (operator.type == TokenType.PERIOD_PERIOD_PERIOD_QUESTION) {
55855585
errorCode = StaticWarningCode.UNNECESSARY_NULL_AWARE_SPREAD;
55865586
} else {
55875587
return;
55885588
}
55895589

55905590
if (target.staticType != null &&
5591-
(target.staticType as TypeImpl).nullabilitySuffix ==
5592-
NullabilitySuffix.none) {
5591+
_typeSystem.isNonNullable(target.staticType)) {
55935592
_errorReporter.reportErrorForToken(errorCode, operator, []);
55945593
}
55955594
}

pkg/analyzer/test/src/diagnostics/unnecessary_null_aware_call_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ f(int x) {
2727
(x)?.isEven;
2828
}
2929
''', [
30-
error(HintCode.UNNECESSARY_NULL_AWARE_CALL, 16, 2),
30+
error(StaticWarningCode.UNNECESSARY_NULL_AWARE_CALL, 16, 2),
3131
]);
3232
}
3333

@@ -45,7 +45,7 @@ f(int x) {
4545
x?.isEven;
4646
}
4747
''', [
48-
error(HintCode.UNNECESSARY_NULL_AWARE_CALL, 14, 2),
48+
error(StaticWarningCode.UNNECESSARY_NULL_AWARE_CALL, 14, 2),
4949
]);
5050
}
5151

@@ -63,7 +63,7 @@ f(int x) {
6363
(x)?.round();
6464
}
6565
''', [
66-
error(HintCode.UNNECESSARY_NULL_AWARE_CALL, 16, 2),
66+
error(StaticWarningCode.UNNECESSARY_NULL_AWARE_CALL, 16, 2),
6767
]);
6868
}
6969

@@ -81,7 +81,7 @@ f(int x) {
8181
x?.round();
8282
}
8383
''', [
84-
error(HintCode.UNNECESSARY_NULL_AWARE_CALL, 14, 2),
84+
error(StaticWarningCode.UNNECESSARY_NULL_AWARE_CALL, 14, 2),
8585
]);
8686
}
8787

tests/language_2/nnbd/static_errors/nullable_function_types_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void main() {
1414
Function? nf2;
1515
C c1 = new C();
1616
C? nc1;
17-
var nf3 = c1?.call;
17+
var nf3 = nc1?.call;
1818
Object? object;
1919

2020

0 commit comments

Comments
 (0)