Skip to content

Commit

Permalink
[linter] Mark unnecessary_final and prefer_final_in_for_each as i…
Browse files Browse the repository at this point in the history
…ncompatible

Change-Id: Ifa4af7c5ab4c5e68ae3e71bc6fc6a3b30394927b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391022
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
parlough authored and Commit Queue committed Oct 21, 2024
1 parent ce913cf commit 47ecb53
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pkg/linter/lib/src/rules/prefer_final_in_for_each.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class PreferFinalInForEach extends LintRule {
description: _desc,
);

@override
List<String> get incompatibleRules => const [LintNames.unnecessary_final];

@override
List<LintCode> get lintCodes => [
LinterLintCode.prefer_final_in_for_each_pattern,
Expand Down
7 changes: 5 additions & 2 deletions pkg/linter/lib/src/rules/unnecessary_final.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class UnnecessaryFinal extends LintRule {
);

@override
List<String> get incompatibleRules =>
const [LintNames.prefer_final_locals, LintNames.prefer_final_parameters];
List<String> get incompatibleRules => const [
LintNames.prefer_final_locals,
LintNames.prefer_final_parameters,
LintNames.prefer_final_in_for_each
];

@override
List<LintCode> get lintCodes => [
Expand Down
13 changes: 8 additions & 5 deletions pkg/linter/tool/machine/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,11 @@
"categories": [
"style"
],
"state": "removed",
"state": "stable",
"incompatible": [],
"sets": [],
"fixStatus": "noFix",
"details": "**DON'T** check for `null` in custom `==` operators.\n\nAs `null` is a special value, no instance of any class (other than `Null`) can\nbe equivalent to it. Thus, it is redundant to check whether the other instance\nis `null`.\n\n**BAD:**\n```dart\nclass Person {\n final String? name;\n\n @override\n operator ==(Object? other) =>\n other != null && other is Person && name == other.name;\n}\n```\n\n**GOOD:**\n```dart\nclass Person {\n final String? name;\n\n @override\n operator ==(Object? other) => other is Person && name == other.name;\n}\n```\n\nThis rule has been removed.",
"fixStatus": "hasFix",
"details": "**DON'T** check for `null` in custom `==` operators.\n\nAs `null` is a special value, no instance of any class (other than `Null`) can\nbe equivalent to it. Thus, it is redundant to check whether the other instance\nis `null`.\n\n**BAD:**\n```dart\nclass Person {\n final String? name;\n\n @override\n operator ==(Object? other) =>\n other != null && other is Person && name == other.name;\n}\n```\n\n**GOOD:**\n```dart\nclass Person {\n final String? name;\n\n @override\n operator ==(Object? other) => other is Person && name == other.name;\n}\n```",
"sinceDartSdk": "2.0"
},
{
Expand Down Expand Up @@ -1877,7 +1877,9 @@
"style"
],
"state": "stable",
"incompatible": [],
"incompatible": [
"unnecessary_final"
],
"sets": [],
"fixStatus": "hasFix",
"details": "**DO** prefer declaring for-each loop variables as final if they are not\nreassigned later in the code.\n\nDeclaring for-each loop variables as final when possible is a good practice\nbecause it helps avoid accidental reassignments and allows the compiler to do\noptimizations.\n\n**BAD:**\n```dart\nfor (var element in elements) { // LINT\n print('Element: $element');\n}\n```\n\n**GOOD:**\n```dart\nfor (final element in elements) {\n print('Element: $element');\n}\n```\n\n**GOOD:**\n```dart\nfor (var element in elements) {\n element = element + element;\n print('Element: $element');\n}\n```",
Expand Down Expand Up @@ -2589,7 +2591,8 @@
"state": "stable",
"incompatible": [
"prefer_final_locals",
"prefer_final_parameters"
"prefer_final_parameters",
"prefer_final_in_for_each"
],
"sets": [],
"fixStatus": "hasFix",
Expand Down

0 comments on commit 47ecb53

Please sign in to comment.