Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions pkg/linter/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8262,9 +8262,9 @@ LinterLintCode:
deprecatedDetails: |-
**DON'T** assign new values to parameters of methods or functions.

Assigning new values to parameters is generally a bad practice unless an
operator such as `??=` is used. Otherwise, arbitrarily reassigning parameters
is usually a mistake.
Assigning new values to parameters is generally a bad practice, even when using
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by comment: I don't think the text should be changed here. The lint actually does not emit a diagnostic when it encounters myParameter ??= someNonNullValue;. The rationale is that this particular kind of assignment is appropriate. It is essentially an emulation of non-constant default values. For example:

var nonConstantDefault = 0;

void f([int? i]) {
  i ??= ++nonConstantDefault; // OK.
  i = 42; // LINT.
}

operators such as `??=`. Arbitrarily reassigning parameters is usually a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that, it makes sense to keep saying 'Otherwise, ...' as well.

mistake and can make code harder to reason about.

**BAD:**
```dart
Expand All @@ -8275,14 +8275,14 @@ LinterLintCode:

**BAD:**
```dart
void badFunction(int required, {int optional: 42}) { // LINT
void badFunction(int required, {int? optional}) { // LINT
optional ??= 8;
Copy link
Member

@eernstg eernstg Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one does not give rise to a lint message, it is an example of the "GOOD" use of ??=. The following could be used as a "BAD" example:

void badFunction(int? nonOptional) {
  nonOptional ??= 8; // LINT
}

}
```

**BAD:**
```dart
void badFunctionPositional(int required, [int optional = 42]) { // LINT
void badFunctionPositional(int required, [int? optional]) { // LINT
optional ??= 8;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should again be relabeled as a "GOOD" example, no lint is reported.

}
```
Expand All @@ -8305,15 +8305,17 @@ LinterLintCode:

**GOOD:**
```dart
void actuallyGood(int required, {int optional}) { // OK
optional ??= ...;
void actuallyGood(int required, {int? optional}) { // OK
final value = optional ?? 8;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to show that ??= is accepted in this case (hence the name actuallyGood), this example should keep the assignment:

Suggested change
final value = optional ?? 8;
optional ??= 8;

An example using a fresh local variable would be fine, too.

print(value);
}
```

**GOOD:**
```dart
void actuallyGoodPositional(int required, [int optional]) { // OK
optional ??= ...;
void actuallyGoodPositional(int required, [int? optional]) { // OK
final value = optional ?? 8;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The situation is similar here:

Suggested change
final value = optional ?? 8;
optional ??= 8;

print(value);
}
```

Expand Down