dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
629 stars 170 forks source link

Lint for missing type info in formal parameter #1

Open pq opened 9 years ago

pq commented 9 years ago

Discovered in the wild a number of cases like this:

typedef A(B); typedef B(dynamic);

Both of them are essentially equivalent to dynamic->dynamic which is most likely not what the user has in mind. Instead, they likely mean:

typedef A(B _); typedef B(dynamic _);

As Paul Berry points out it's a nasty user trap in the language, especially for people coming from a C/C++ background where using a single identifier as a formal parameter denotes the type of the formal parameter. A proposed lint:

whenever a single identifier is used to name a formal parameter, check the enclosing scope to see if the name shadows a type. If it does, give a hint because the user probably meant the identifier to denote the type of the formal parameter. We could even add a quick fix which adds "_" as the parameter name.

ochafik commented 8 years ago

An easy way to tackle this would be to check that argument names start with lowercase. And a similar check for method names would help spot another sneaky case:

class MyClassNameBeforeRefactoring {
  // oops, forgot to rename the constructor...
  MyClassNameAfterRefactoring() {
    // something important that will never happen
  }
}
zoechi commented 8 years ago

@ochafik for your case a rule for functions and methods like http://dart-lang.github.io/linter/lints/non_constant_identifier_names.html would be nice to catch this.

dotdoom commented 6 years ago

Looks like this will become irrelevant when the old typedef syntax becomes obsolete?

srawlins commented 1 month ago

Yeah I think we've likely dodged this by recommending the new typedef format.