This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
Add non_nullable_equals_parameter rule #3923
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // 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:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/token.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
|
|
||
| import '../analyzer.dart'; | ||
|
|
||
| const _desc = r'The parameter type of `==` operators should be non-nullable.'; | ||
|
|
||
| const _details = r''' | ||
| The parameter type of `==` operators should be non-nullable. | ||
|
|
||
| `null` is never passed to `operator ==`, so the parameter type of an `==` | ||
| operator override should always be non-nullable. | ||
|
|
||
| **BAD:** | ||
| ```dart | ||
| class C { | ||
| String key; | ||
| B(this.key); | ||
| @override | ||
| operator ==(Object? other) => other is C && other.key == key; | ||
| } | ||
| ``` | ||
|
|
||
| **GOOD:** | ||
| ```dart | ||
| class C { | ||
| String key; | ||
| B(this.key); | ||
| @override | ||
| operator ==(Object other) => other is C && other.key == key; | ||
| } | ||
| ``` | ||
| '''; | ||
|
|
||
| class NonNullableEqualsParameter extends LintRule { | ||
| static const LintCode code = LintCode('non_nullable_equals_parameter', | ||
| 'The parameter should have a non-nullable type.', | ||
| correctionMessage: 'Try using a non-nullable type.'); | ||
|
|
||
| NonNullableEqualsParameter() | ||
| : super( | ||
| name: 'non_nullable_equals_parameter', | ||
| description: _desc, | ||
| details: _details, | ||
| group: Group.style); | ||
|
|
||
| @override | ||
| LintCode get lintCode => code; | ||
|
|
||
| @override | ||
| void registerNodeProcessors( | ||
| NodeLintRegistry registry, LinterContext context) { | ||
| var visitor = _Visitor(this, context); | ||
| registry.addMethodDeclaration(this, visitor); | ||
| } | ||
| } | ||
|
|
||
| class _Visitor extends SimpleAstVisitor<void> { | ||
| final LintRule rule; | ||
|
|
||
| final LinterContext context; | ||
|
|
||
| _Visitor(this.rule, this.context); | ||
|
|
||
| @override | ||
| void visitMethodDeclaration(MethodDeclaration node) { | ||
| if (node.name.type != TokenType.EQ_EQ) { | ||
| return; | ||
| } | ||
|
|
||
| var parameters = node.parameters; | ||
| if (parameters == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (parameters.parameters.isEmpty) { | ||
| return; | ||
| } | ||
|
|
||
| var parameter = parameters.parameters.first; | ||
| var parameterElement = parameter.declaredElement; | ||
|
|
||
| if (parameterElement == null) { | ||
| return; | ||
| } | ||
|
|
||
| var type = parameterElement.type; | ||
|
|
||
| if (!type.isDartCoreObject && !type.isDynamic) { | ||
| // There is no legal way to define a nullable parameter type, which is not | ||
| // `dynamic` or `Object?`. | ||
srawlins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| if (context.typeSystem.isNullable(parameterElement.type)) { | ||
| rule.reportLint(parameter); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // 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(NonNullableEqualsParameterTest); | ||
| }); | ||
| } | ||
|
|
||
| @reflectiveTest | ||
| class NonNullableEqualsParameterTest extends LintRuleTest { | ||
| @override | ||
| String get lintRule => 'non_nullable_equals_parameter'; | ||
|
|
||
| test_class_nonNullableParameter() async { | ||
| await assertNoDiagnostics(r''' | ||
| class C { | ||
| bool operator ==(Object other) => other is C; | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| test_class_nonNullableParameter_inherited() async { | ||
| await assertNoDiagnostics(r''' | ||
| class C { | ||
| bool operator ==(Object other) => other is C; | ||
| } | ||
| class D extends C { | ||
| bool operator ==(other) => other is D; | ||
| } | ||
| '''); | ||
| } | ||
|
|
||
| test_class_nullableParameter() async { | ||
| await assertDiagnostics(r''' | ||
| class C { | ||
| bool operator ==(Object? other) => other is C; | ||
| } | ||
| ''', [ | ||
| lint(29, 13), | ||
| ]); | ||
| } | ||
|
|
||
| test_class_nullableParameter_dynamic() async { | ||
| await assertDiagnostics(r''' | ||
| class C { | ||
| bool operator ==(dynamic other) => other is C; | ||
| } | ||
| ''', [ | ||
| lint(29, 13), | ||
| ]); | ||
| } | ||
|
|
||
| test_class_nullableParameter_dynamicAndInherited() async { | ||
| await assertDiagnostics(r''' | ||
| class C { | ||
| bool operator ==(dynamic other) => other is C; | ||
| } | ||
| class D extends C { | ||
| bool operator ==(other) => other is D; | ||
| } | ||
| ''', [ | ||
| lint(29, 13), | ||
| lint(100, 5), | ||
| ]); | ||
| } | ||
|
|
||
| test_class_nullableParameter_nonObject() async { | ||
| await assertDiagnostics(r''' | ||
| class C { | ||
| bool operator ==(num? other) => false; | ||
| } | ||
| ''', [ | ||
| error(CompileTimeErrorCode.INVALID_OVERRIDE, 26, 2), | ||
| // No lint. | ||
| ]); | ||
| } | ||
|
|
||
| test_mixin_nullableParameter() async { | ||
| await assertDiagnostics(r''' | ||
| mixin M { | ||
| bool operator ==(Object? other) => other is M; | ||
| } | ||
| ''', [ | ||
| lint(29, 13), | ||
| ]); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternately, we could test for
length != 1, because if there's more than one there will also be a different diagnostic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it! Done.