Closed
Description
I wrote by accident a function call without the parenthesis at the end. I even copy/pasted this code to several places before I noticed it in my final code review. I would have expected to get a warning about this.
Example:
void foo() {
}
void foo2(int i) {
}
void main() {
foo; // Warning should be shown for this line for an invalid function call
foo();
foo2; // Also this should give a warning
}
I know that a function reference is needed in some situations (like when passing a function as a parameter), but in the example above the situation is clear (only a function call makes sense).
Dart SDK version: 2.10.4 (stable)
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
eernstg commentedon Dec 8, 2020
foo;
inmain
is a perfectly executable and type correct statement, but I would expect that some tool could report that it does not have any effect.The action taken is: Create a function object (a first class function) corresponding to the function declaration named
foo
, and then discard it.It's no worse than
1;
which will evaluate the given expression to an object representing the integer 1 and then discard it.You may prefer to enable https://dart-lang.github.io/linter/lints/unnecessary_statements.html. I don't know if that's detecting exactly the kinds of no-op statements that you're focusing on, but it does make the analyzer output a diagnostic message about the two lines
foo;
andfoo2;
.kinex commentedon Dec 8, 2020
I know those are correct statements, but clearly they are statements the developer did not write on purpose. And in my case it resulted a medium level bug (a required function call
ChangeNotifier.notifylisteners()
was omitted).unnecessary_statements
seems to help! Thanks. Little surprising that I have to enable this rule manually (even though I use also pedantic package). After enabling that rule I found also one new similar issue in my code base.