Skip to content

Enable parameter_assignments lint #185704

Description

@justinmc

Background

Flutter's code style has typically not enforced whether or not parameters can be modified in function bodies.

void greet(String name) {
  name = 'The Respectable $name'; // Is this ok? Styleguide and linter don't care.
  return 'Hello $name';
}

It has been possible to use final to enforce that a parameter can't be modified:

void greet(final String name) {
  name = 'The Respectable $name'; // Compilation error.
...

But recently, PR #185216 was landed that disallowed using final in this way (for good reasons, see the PR for details).

Proposal

I propose that Flutter should adopt the parameter_assignments lint, which would enforce that parameters cannot be modified.

void greet(String name) {
  // name = 'The Respectable $name'; // Lint error.
  final String nameWithTitle = 'The Respectable $name';
  return 'Hello $nameWithTitle';
}

In my opinion, having the ability to modify parameters increases the mental burden of understanding a function (e.g., "I want to use this parameter name, but has it been changed? I have to read through all previous lines of code in the function to be sure that it hasn't.").

This will have to be done in pieces due to the code freeze.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions