Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into avoid_renaming_method_parameters_enums
Browse files Browse the repository at this point in the history
# Conflicts:
#	test/rules/all.dart
  • Loading branch information
pq committed Jan 28, 2022
2 parents 2e38ae6 + 69c2f6d commit e7bb650
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/src/rules/avoid_returning_this.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ class _Visitor extends SimpleAstVisitor<void> {
if (node.isOperator) return;

var parent = node.parent;
if (parent is ClassOrMixinDeclaration) {
if (parent is ClassOrMixinDeclaration || parent is EnumDeclaration) {
if (DartTypeUtilities.overridesMethod(node)) {
return;
}

var returnType = node.declaredElement?.returnType;
if (returnType is InterfaceType &&
returnType.element == parent.declaredElement) {
// ignore: cast_nullable_to_non_nullable
returnType.element == (parent as Declaration).declaredElement) {
} else {
return;
}
Expand Down
6 changes: 6 additions & 0 deletions test/rules/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 'annotate_overrides.dart' as annotate_overrides;
import 'avoid_annotating_with_dynamic.dart' as avoid_annotating_with_dynamic;
import 'avoid_function_literals_in_foreach_calls.dart'
as avoid_function_literals_in_foreach_calls;
Expand All @@ -10,6 +11,7 @@ import 'avoid_redundant_argument_values.dart'
as avoid_redundant_argument_values;
import 'avoid_renaming_method_parameters.dart'
as avoid_renaming_method_parameters;
import 'avoid_returning_this.dart' as avoid_returning_this;
import 'avoid_shadowing_type_parameters.dart'
as avoid_shadowing_type_parameters;
import 'avoid_types_as_parameter_names.dart' as avoid_types_as_parameter_names;
Expand Down Expand Up @@ -46,15 +48,18 @@ import 'tighten_type_of_initializing_formals.dart'
import 'type_init_formals.dart' as type_init_formals;
import 'unawaited_futures.dart' as unawaited_futures;
import 'unnecessary_null_checks.dart' as unnecessary_null_checks;
import 'unnecessary_overrides.dart' as unnecessary_overrides;
import 'use_is_even_rather_than_modulo.dart' as use_is_even_rather_than_modulo;
import 'void_checks.dart' as void_checks;

void main() {
annotate_overrides.main();
avoid_annotating_with_dynamic.main();
avoid_function_literals_in_foreach_calls.main();
avoid_init_to_null.main();
avoid_redundant_argument_values.main();
avoid_renaming_method_parameters.main();
avoid_returning_this.main();
avoid_shadowing_type_parameters.main();
avoid_types_as_parameter_names.main();
avoid_unused_constructor_parameters.main();
Expand All @@ -81,6 +86,7 @@ void main() {
type_init_formals.main();
unawaited_futures.main();
unnecessary_null_checks.main();
unnecessary_overrides.main();
use_is_even_rather_than_modulo.main();
void_checks.main();
}
59 changes: 59 additions & 0 deletions test/rules/annotate_overrides.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2022, 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:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(AnnotateOverridesTest);
});
}

@reflectiveTest
class AnnotateOverridesTest extends LintRuleTest {
@override
List<String> get experiments => [
EnableString.enhanced_enums,
];

@override
String get lintRule => 'annotate_overrides';

test_field() async {
await assertDiagnostics(r'''
enum A {
a,b,c;
int get hashCode => 0;
}
''', [
lint('annotate_overrides', 28, 8),
]);
}

test_method() async {
await assertDiagnostics(r'''
enum A {
a,b,c;
String toString() => '';
}
''', [
lint('annotate_overrides', 27, 8),
]);
}

@FailingTest(issue: 'https://github.com/dart-lang/linter/issues/3093')
test_ok() async {
await assertNoDiagnostics(r'''
enum A {
a,b,c;
@override
int get hashCode => 0;
@override
String toString() => '';
}
''');
}
}
35 changes: 35 additions & 0 deletions test/rules/avoid_returning_this.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2022, 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:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(AvoidReturningThisTest);
});
}

@reflectiveTest
class AvoidReturningThisTest extends LintRuleTest {
@override
List<String> get experiments => [
EnableString.enhanced_enums,
];

@override
String get lintRule => 'avoid_returning_this';

test_method() async {
await assertDiagnostics(r'''
enum A {
a,b,c;
A a() => this;
}
''', [
lint('avoid_returning_this', 22, 1),
]);
}
}
50 changes: 50 additions & 0 deletions test/rules/unnecessary_overrides.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2022, 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:test_reflective_loader/test_reflective_loader.dart';

import '../rule_test_support.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(UnnecessaryOverridesTest);
});
}

@reflectiveTest
class UnnecessaryOverridesTest extends LintRuleTest {
@override
List<String> get experiments => [
EnableString.enhanced_enums,
];

@override
String get lintRule => 'unnecessary_overrides';

@FailingTest(issue: 'https://github.com/dart-lang/linter/issues/3097')
test_field() async {
await assertDiagnostics(r'''
enum A {
a,b,c;
@override
int get foo => 0;
}
''', [
lint('unnecessary_overrides', 28, 8),
]);
}

@FailingTest(issue: 'https://github.com/dart-lang/linter/issues/3097')
test_method() async {
await assertDiagnostics(r'''
enum A {
a,b,c;
@override
String bar() => '';
}
''', [
lint('unnecessary_overrides', 27, 8),
]);
}
}

0 comments on commit e7bb650

Please sign in to comment.