Skip to content
This repository was archived by the owner on Jan 12, 2026. It is now read-only.

Commit f6e2fbc

Browse files
committed
Merge remote-tracking branch 'origin/main' into unnecessary_parenthesis_ternary
2 parents bc61cdd + 79b8433 commit f6e2fbc

75 files changed

Lines changed: 762 additions & 258 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
# 1.32.0
2+
3+
- update `avoid_types_as_parameter_names` to handle type variables
4+
- update `avoid_positional_boolean_parameters` to handle typedefs
5+
- improve `unnecessary_parenthesis` support for property accesses and method invocations
6+
- update `avoid_redundant_argument_values` to check parameters of redirecting constructors
7+
- performance improvements for `prefer_const_literals_to_create_immutables`
8+
- update `use_build_context_synchronously` to check context properties
9+
- fix false positive for `avoid_private_typedef_functions` with generalized type aliases
10+
111
# 1.31.0
212

3-
- updated `prefer_equal_for_default_values` to not report for SDKs `>=2.19`,
13+
- update `prefer_equal_for_default_values` to not report for SDKs `>=2.19`,
414
where this lint is now an analyzer diagnostic.
5-
- updated `unrelated_type_equality_checks` to support updated `package:fixnum`
15+
- update `unrelated_type_equality_checks` to support updated `package:fixnum`
616
structure.
717

818
# 1.30.0

doc/releasing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Before releasing there are a few boxes to tick off.
66

77
* [ ] Is there a [milestone plan](https://github.com/dart-lang/linter/issues?q=is%3Aopen+is%3Aissue+label%3Amilestone-plan) for the release? If so, has it been updated?
88
* [ ] Is the changelog up to date? (Look at commit history to verify.)
9+
* [ ] Chronological order is fine.
910
* [ ] Does the `AUTHORS` file need updating?
1011
* [ ] Spot check new lint rules for [naming consistency](https://github.com/dart-lang/linter/blob/main/doc/writing-lints.md). Rename as needed.
1112

example/all.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ linter:
123123
- prefer_constructors_over_static_methods
124124
- prefer_contains
125125
- prefer_double_quotes
126+
- prefer_equal_for_default_values
126127
- prefer_expression_function_bodies
127128
- prefer_final_fields
128129
- prefer_final_in_for_each

lib/src/rules/avoid_init_to_null.dart

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,44 @@ const _desc = r"Don't explicitly initialize variables to null.";
1616
const _details = r'''
1717
From [Effective Dart](https://dart.dev/guides/language/effective-dart/usage#dont-explicitly-initialize-variables-to-null):
1818
19-
**DON'T** explicitly initialize variables to null.
19+
**DON'T** explicitly initialize variables to `null`.
2020
21-
In Dart, a variable or field that is not explicitly initialized automatically
22-
gets initialized to null. This is reliably specified by the language. There's
23-
no concept of "uninitialized memory" in Dart. Adding `= null` is redundant and
24-
unneeded.
21+
If a variable has a non-nullable type or is `final`,
22+
Dart reports a compile error if you try to use it
23+
before it has been definitely initialized.
24+
If the variable is nullable and not `const` or `final`,
25+
then it is implicitly initialized to `null` for you.
26+
There's no concept of "uninitialized memory" in Dart
27+
and no need to explicitly initialize a variable to `null` to be "safe".
28+
Adding `= null` is redundant and unneeded.
2529
2630
**BAD:**
2731
```dart
28-
int _nextId = null;
32+
Item? bestDeal(List<Item> cart) {
33+
Item? bestItem = null;
2934
30-
class LazyId {
31-
int _id = null;
32-
33-
int get id {
34-
if (_nextId == null) _nextId = 0;
35-
if (_id == null) _id = _nextId++;
36-
37-
return _id;
35+
for (final item in cart) {
36+
if (bestItem == null || item.price < bestItem.price) {
37+
bestItem = item;
38+
}
3839
}
40+
41+
return bestItem;
3942
}
4043
```
4144
4245
**GOOD:**
4346
```dart
44-
int _nextId;
47+
Item? bestDeal(List<Item> cart) {
48+
Item? bestItem;
4549
46-
class LazyId {
47-
int _id;
48-
49-
int get id {
50-
if (_nextId == null) _nextId = 0;
51-
if (_id == null) _id = _nextId++;
52-
53-
return _id;
50+
for (final item in cart) {
51+
if (bestItem == null || item.price < bestItem.price) {
52+
bestItem = item;
53+
}
5454
}
55+
56+
return bestItem;
5557
}
5658
```
5759

lib/src/rules/avoid_null_checks_in_equality_operators.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,28 @@ const _desc = r"Don't check for null in custom == operators.";
1717
const _details = r'''
1818
**DON'T** check for null in custom == operators.
1919
20-
As null is a special type, no class can be equivalent to it. Thus, it is
21-
redundant to check whether the other instance is null.
20+
As null is a special value, no instance of any class (other than `Null`) can be
21+
equivalent to it. Thus, it is redundant to check whether the other instance is
22+
null.
2223
2324
**BAD:**
2425
```dart
2526
class Person {
26-
final String name;
27+
final String? name;
2728
2829
@override
29-
operator ==(other) =>
30+
operator ==(Object? other) =>
3031
other != null && other is Person && name == other.name;
3132
}
3233
```
3334
3435
**GOOD:**
3536
```dart
3637
class Person {
37-
final String name;
38+
final String? name;
3839
3940
@override
40-
operator ==(other) => other is Person && name == other.name;
41+
operator ==(Object? other) => other is Person && name == other.name;
4142
}
4243
```
4344

lib/src/rules/avoid_positional_boolean_parameters.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class AvoidPositionalBooleanParameters extends LintRule {
6060
registry.addConstructorDeclaration(this, visitor);
6161
registry.addFunctionDeclaration(this, visitor);
6262
registry.addMethodDeclaration(this, visitor);
63+
registry.addGenericFunctionType(this, visitor);
6364
}
6465
}
6566

@@ -105,6 +106,11 @@ class _Visitor extends SimpleAstVisitor<void> {
105106
}
106107
}
107108

109+
@override
110+
void visitGenericFunctionType(GenericFunctionType node) {
111+
checkParams(node.parameters.parameters);
112+
}
113+
108114
bool _isOverridingMember(Element member) {
109115
var classElement = member.thisOrAncestorOfType<ClassElement>();
110116
if (classElement == null) return false;

lib/src/rules/avoid_types_as_parameter_names.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ class _Visitor extends SimpleAstVisitor<void> {
6868

6969
@override
7070
void visitFormalParameterList(FormalParameterList node) {
71-
if (node.parent is GenericFunctionType) return;
72-
7371
for (var parameter in node.parameters) {
7472
var declaredElement = parameter.declaredElement;
7573
var name = parameter.name;
@@ -87,7 +85,9 @@ class _Visitor extends SimpleAstVisitor<void> {
8785
var result = context.resolveNameInScope(name.lexeme, false, scope);
8886
if (result.isRequestedName) {
8987
var element = result.element;
90-
return element is ClassElement || element is TypeAliasElement;
88+
return element is ClassElement ||
89+
element is TypeAliasElement ||
90+
element is TypeParameterElement;
9191
}
9292
return false;
9393
}

lib/src/rules/cascade_invocations.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ bool _isInvokedWithoutNullAwareOperator(Token? token) =>
105105
/// Rule to lint consecutive invocations of methods or getters on the same
106106
/// reference that could be done with the cascade operator.
107107
class CascadeInvocations extends LintRule {
108+
static const LintCode code = LintCode(
109+
'cascade_invocations', 'Unnecessary duplication of receiver.',
110+
correctionMessage: 'Try using a cascade to avoid the duplication.');
111+
108112
/// Default constructor.
109113
CascadeInvocations()
110114
: super(
@@ -113,6 +117,9 @@ class CascadeInvocations extends LintRule {
113117
details: _details,
114118
group: Group.style);
115119

120+
@override
121+
LintCode get lintCode => code;
122+
116123
@override
117124
void registerNodeProcessors(
118125
NodeLintRegistry registry, LinterContext context) {

lib/src/rules/cast_nullable_to_non_nullable.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ var v = a!;
3737
''';
3838

3939
class CastNullableToNonNullable extends LintRule {
40+
static const LintCode code = LintCode('cast_nullable_to_non_nullable',
41+
"Don't cast a nullable value to a non-nullable type.",
42+
correctionMessage:
43+
"Try adding a not-null assertion ('!') to make the type "
44+
'non-nullable.');
45+
4046
CastNullableToNonNullable()
4147
: super(
4248
name: 'cast_nullable_to_non_nullable',
@@ -46,6 +52,9 @@ class CastNullableToNonNullable extends LintRule {
4652
group: Group.style,
4753
);
4854

55+
@override
56+
LintCode get lintCode => code;
57+
4958
@override
5059
void registerNodeProcessors(
5160
NodeLintRegistry registry, LinterContext context) {
@@ -59,10 +68,10 @@ class CastNullableToNonNullable extends LintRule {
5968
}
6069

6170
class _Visitor extends SimpleAstVisitor<void> {
62-
_Visitor(this.rule, this.context);
63-
6471
final LintRule rule;
72+
6573
final LinterContext context;
74+
_Visitor(this.rule, this.context);
6675

6776
@override
6877
void visitAsExpression(AsExpression node) {

lib/src/rules/conditional_uri_does_not_exist.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,22 @@ import 'file_that_does_exist.dart'
3131
''';
3232

3333
class ConditionalUriDoesNotExist extends LintRule {
34+
static const LintCode code = LintCode('conditional_uri_does_not_exist',
35+
"The target of a conditional import doesn't exist.",
36+
correctionMessage:
37+
'Try creating the imported file or changing the URI to reference an '
38+
'existing file.');
39+
3440
ConditionalUriDoesNotExist()
3541
: super(
3642
name: 'conditional_uri_does_not_exist',
3743
description: _desc,
3844
details: _details,
3945
group: Group.style);
4046

47+
@override
48+
LintCode get lintCode => code;
49+
4150
@override
4251
void registerNodeProcessors(
4352
NodeLintRegistry registry, LinterContext context) {

0 commit comments

Comments
 (0)