[M] Change in dart/sdk[main]: [analyzer] add `OVERRIDE_ON_NON_OVERRIDING_FUNCTION`

0 views
Skip to first unread message

Ahmed Ashour (Gerrit)

unread,
Feb 7, 2023, 4:10:28 PM2/7/23
to rev...@dartlang.org

Ahmed Ashour has uploaded this change for review.

View Change

[analyzer] add `OVERRIDE_ON_NON_OVERRIDING_FUNCTION`

Fixes #51306

Change-Id: Ie93b7cec92a4c05d9b1248c1bff6e3602660ae83
---
M pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
M pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
M pkg/analysis_server/lib/src/services/correction/fix_internal.dart
M pkg/analysis_server/test/src/services/correction/fix/remove_annotation_test.dart
M pkg/analyzer/lib/src/dart/error/hint_codes.g.dart
M pkg/analyzer/lib/src/error/error_code_values.g.dart
M pkg/analyzer/lib/src/error/override_verifier.dart
M pkg/analyzer/messages.yaml
A pkg/analyzer/test/src/diagnostics/override_on_non_overriding_function_test.dart
M pkg/analyzer/tool/diagnostics/diagnostics.md
10 files changed, 120 insertions(+), 0 deletions(-)

diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
index 1c332c4..cf24b5b 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
@@ -52,6 +52,8 @@
await addFix(node);
} else if (node is DefaultFormalParameter) {
await addFix(findAnnotation(node.parameter.metadata, 'required'));
+ } else if (node is FunctionDeclaration) {
+ await addFix(findAnnotation(node.metadata, 'override'));
} else if (node is NormalFormalParameter) {
await addFix(findAnnotation(node.metadata, 'required'));
} else if (node is MethodDeclaration) {
diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
index a4c1a9a..c68a21d 100644
--- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
+++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
@@ -1482,6 +1482,8 @@
Remove the null check.
HintCode.OVERRIDE_ON_NON_OVERRIDING_FIELD:
status: hasFix
+HintCode.OVERRIDE_ON_NON_OVERRIDING_FUNCTION:
+ status: hasFix
HintCode.OVERRIDE_ON_NON_OVERRIDING_GETTER:
status: hasFix
HintCode.OVERRIDE_ON_NON_OVERRIDING_METHOD:
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
index 03ec63b..6eb46b7 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -1396,6 +1396,9 @@
HintCode.OVERRIDE_ON_NON_OVERRIDING_FIELD: [
RemoveAnnotation.new,
],
+ HintCode.OVERRIDE_ON_NON_OVERRIDING_FUNCTION: [
+ RemoveAnnotation.new,
+ ],
HintCode.OVERRIDE_ON_NON_OVERRIDING_GETTER: [
RemoveAnnotation.new,
],
diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_annotation_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_annotation_test.dart
index b047e36..429df19 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/remove_annotation_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/remove_annotation_test.dart
@@ -158,6 +158,34 @@
''');
}

+ Future<void> test_override_function() async {
+ await resolveTestCode('''
+@override
+void f() {}
+''');
+ await assertHasFix('''
+void f() {}
+''');
+ }
+
+ Future<void> test_override_function_local() async {
+ await resolveTestCode('''
+void g() {
+ @override
+ void f() {}
+
+ f();
+}
+''');
+ await assertHasFix('''
+void g() {
+ void f() {}
+
+ f();
+}
+''');
+ }
+
Future<void> test_override_getter() async {
await resolveTestCode('''
class A {
diff --git a/pkg/analyzer/lib/src/dart/error/hint_codes.g.dart b/pkg/analyzer/lib/src/dart/error/hint_codes.g.dart
index 99a8c6c..d1935ed 100644
--- a/pkg/analyzer/lib/src/dart/error/hint_codes.g.dart
+++ b/pkg/analyzer/lib/src/dart/error/hint_codes.g.dart
@@ -581,6 +581,17 @@
uniqueName: 'OVERRIDE_ON_NON_OVERRIDING_FIELD',
);

+ /// A function with the override annotation does not override a member.
+ ///
+ /// No parameters.
+ static const HintCode OVERRIDE_ON_NON_OVERRIDING_FUNCTION = HintCode(
+ 'OVERRIDE_ON_NON_OVERRIDING_MEMBER',
+ "The function doesn't override an inherited member.",
+ correctionMessage: "Try removing the override annotation.",
+ hasPublishedDocs: true,
+ uniqueName: 'OVERRIDE_ON_NON_OVERRIDING_FUNCTION',
+ );
+
/// A getter with the override annotation does not override an existing getter.
///
/// No parameters.
diff --git a/pkg/analyzer/lib/src/error/error_code_values.g.dart b/pkg/analyzer/lib/src/error/error_code_values.g.dart
index 32f4497..9ee4e1a 100644
--- a/pkg/analyzer/lib/src/error/error_code_values.g.dart
+++ b/pkg/analyzer/lib/src/error/error_code_values.g.dart
@@ -626,6 +626,7 @@
HintCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR_USING_NEW,
HintCode.NULL_CHECK_ALWAYS_FAILS,
HintCode.OVERRIDE_ON_NON_OVERRIDING_FIELD,
+ HintCode.OVERRIDE_ON_NON_OVERRIDING_FUNCTION,
HintCode.OVERRIDE_ON_NON_OVERRIDING_GETTER,
HintCode.OVERRIDE_ON_NON_OVERRIDING_METHOD,
HintCode.OVERRIDE_ON_NON_OVERRIDING_SETTER,
diff --git a/pkg/analyzer/lib/src/error/override_verifier.dart b/pkg/analyzer/lib/src/error/override_verifier.dart
index dc2004a..568e30a 100644
--- a/pkg/analyzer/lib/src/error/override_verifier.dart
+++ b/pkg/analyzer/lib/src/error/override_verifier.dart
@@ -63,6 +63,18 @@
}

@override
+ void visitFunctionDeclaration(FunctionDeclaration node) {
+ var element = node.declaredElement!;
+ if (element.hasOverride) {
+ _errorReporter.reportErrorForToken(
+ HintCode.OVERRIDE_ON_NON_OVERRIDING_FUNCTION,
+ node.name,
+ );
+ }
+ super.visitFunctionDeclaration(node);
+ }
+
+ @override
void visitMethodDeclaration(MethodDeclaration node) {
var element = node.declaredElement!;
if (element.hasOverride && !_isOverride(element)) {
diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml
index 5a43dd9..0762552 100644
--- a/pkg/analyzer/messages.yaml
+++ b/pkg/analyzer/messages.yaml
@@ -19414,6 +19414,15 @@
A field with the override annotation does not override a getter or setter.

No parameters.
+ OVERRIDE_ON_NON_OVERRIDING_FUNCTION:
+ sharedName: OVERRIDE_ON_NON_OVERRIDING_MEMBER
+ problemMessage: "The function doesn't override an inherited member."
+ correctionMessage: Try removing the override annotation.
+ hasPublishedDocs: true
+ comment: |-
+ A function with the override annotation does not override a member.
+
+ No parameters.
OVERRIDE_ON_NON_OVERRIDING_GETTER:
sharedName: OVERRIDE_ON_NON_OVERRIDING_MEMBER
problemMessage: "The getter doesn't override an inherited getter."
diff --git a/pkg/analyzer/test/src/diagnostics/override_on_non_overriding_function_test.dart b/pkg/analyzer/test/src/diagnostics/override_on_non_overriding_function_test.dart
new file mode 100644
index 0000000..7856e4d
--- /dev/null
+++ b/pkg/analyzer/test/src/diagnostics/override_on_non_overriding_function_test.dart
@@ -0,0 +1,39 @@
+// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:analyzer/src/error/codes.dart';
+import 'package:test_reflective_loader/test_reflective_loader.dart';
+
+import '../dart/resolution/context_collection_resolution.dart';
+
+main() {
+ defineReflectiveSuite(() {
+ defineReflectiveTests(OverrideOnNonOverridingFunctionTest);
+ });
+}
+
+@reflectiveTest
+class OverrideOnNonOverridingFunctionTest extends PubPackageResolutionTest {
+ test_function() async {
+ await assertErrorsInCode(r'''
+@override
+void foo() {}
+''', [
+ error(HintCode.OVERRIDE_ON_NON_OVERRIDING_FUNCTION, 15, 3),
+ ]);
+ }
+
+ test_function_local() async {
+ await assertErrorsInCode(r'''
+void g() {
+ @override
+ void f() {}
+
+ f();
+}
+''', [
+ error(HintCode.OVERRIDE_ON_NON_OVERRIDING_FUNCTION, 30, 1),
+ ]);
+ }
+}
diff --git a/pkg/analyzer/tool/diagnostics/diagnostics.md b/pkg/analyzer/tool/diagnostics/diagnostics.md
index 1891321..5c5b85a 100644
--- a/pkg/analyzer/tool/diagnostics/diagnostics.md
+++ b/pkg/analyzer/tool/diagnostics/diagnostics.md
@@ -14348,6 +14348,8 @@

_The field doesn't override an inherited getter or setter._

+_The function doesn't override an inherited member._
+
_The getter doesn't override an inherited getter._

_The method doesn't override an inherited method._

To view, visit change 281500. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sdk
Gerrit-Branch: main
Gerrit-Change-Id: Ie93b7cec92a4c05d9b1248c1bff6e3602660ae83
Gerrit-Change-Number: 281500
Gerrit-PatchSet: 1
Gerrit-Owner: Ahmed Ashour <asas...@yahoo.com>
Gerrit-MessageType: newchange

Ahmed Ashour (Gerrit)

unread,
Feb 7, 2023, 4:18:35 PM2/7/23
to rev...@dartlang.org

Ahmed Ashour uploaded patch set #2 to this change.

View Change

[analyzer] add `OVERRIDE_ON_NON_OVERRIDING_FUNCTION`

Fixes #51306

Change-Id: Ie93b7cec92a4c05d9b1248c1bff6e3602660ae83
---
M pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
M pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
M pkg/analysis_server/lib/src/services/correction/fix_internal.dart
M pkg/analysis_server/test/src/services/correction/fix/remove_annotation_test.dart
M pkg/analyzer/lib/src/dart/error/hint_codes.g.dart
M pkg/analyzer/lib/src/error/error_code_values.g.dart
M pkg/analyzer/lib/src/error/override_verifier.dart
M pkg/analyzer/messages.yaml
A pkg/analyzer/test/src/diagnostics/override_on_non_overriding_function_test.dart
M pkg/analyzer/test/src/diagnostics/test_all.dart
M pkg/analyzer/tool/diagnostics/diagnostics.md
11 files changed, 123 insertions(+), 0 deletions(-)

To view, visit change 281500. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sdk
Gerrit-Branch: main
Gerrit-Change-Id: Ie93b7cec92a4c05d9b1248c1bff6e3602660ae83
Gerrit-Change-Number: 281500
Gerrit-PatchSet: 2
Gerrit-Owner: Ahmed Ashour <asas...@yahoo.com>
Gerrit-MessageType: newpatchset

Ahmed Ashour (Gerrit)

unread,
Feb 8, 2023, 4:18:28 AM2/8/23
to rev...@dartlang.org

Ahmed Ashour uploaded patch set #3 to this change.

View Change

[analyzer] add `OVERRIDE_ON_NON_OVERRIDING_FUNCTION`

Fixes #51306

Change-Id: Ie93b7cec92a4c05d9b1248c1bff6e3602660ae83
---
M pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
M pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
M pkg/analysis_server/lib/src/services/correction/fix_internal.dart
M pkg/analysis_server/test/src/services/correction/fix/remove_annotation_test.dart
M pkg/analyzer/lib/src/dart/error/hint_codes.g.dart
M pkg/analyzer/lib/src/error/error_code_values.g.dart
M pkg/analyzer/lib/src/error/override_verifier.dart
M pkg/analyzer/messages.yaml
A pkg/analyzer/test/src/diagnostics/override_on_non_overriding_function_test.dart
M pkg/analyzer/test/src/diagnostics/test_all.dart
M pkg/analyzer/tool/diagnostics/diagnostics.md
11 files changed, 418 insertions(+), 10 deletions(-)

To view, visit change 281500. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sdk
Gerrit-Branch: main
Gerrit-Change-Id: Ie93b7cec92a4c05d9b1248c1bff6e3602660ae83
Gerrit-Change-Number: 281500
Gerrit-PatchSet: 3

Ahmed Ashour (Gerrit)

unread,
Feb 8, 2023, 4:31:02 AM2/8/23
to rev...@dartlang.org

Ahmed Ashour uploaded patch set #4 to this change.

View Change

[analyzer] add `OVERRIDE_ON_NON_OVERRIDING_FUNCTION`

Fixes #51306

Change-Id: Ie93b7cec92a4c05d9b1248c1bff6e3602660ae83
---
M pkg/analysis_server/lib/src/services/correction/dart/remove_annotation.dart
M pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
M pkg/analysis_server/lib/src/services/correction/fix_internal.dart
M pkg/analysis_server/test/src/services/correction/fix/remove_annotation_test.dart
M pkg/analyzer/lib/src/dart/error/hint_codes.g.dart
M pkg/analyzer/lib/src/error/error_code_values.g.dart
M pkg/analyzer/lib/src/error/override_verifier.dart
M pkg/analyzer/messages.yaml
A pkg/analyzer/test/src/diagnostics/override_on_non_overriding_function_test.dart
M pkg/analyzer/test/src/diagnostics/test_all.dart
M pkg/analyzer/tool/diagnostics/diagnostics.md
11 files changed, 413 insertions(+), 5 deletions(-)

To view, visit change 281500. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sdk
Gerrit-Branch: main
Gerrit-Change-Id: Ie93b7cec92a4c05d9b1248c1bff6e3602660ae83
Gerrit-Change-Number: 281500
Gerrit-PatchSet: 4

Ahmed Ashour (Gerrit)

unread,
Feb 8, 2023, 4:34:51 AM2/8/23
to rev...@dartlang.org

Ahmed Ashour uploaded patch set #5 to this change.

Gerrit-PatchSet: 5
Reply all
Reply to author
Forward
0 new messages