Skip to content

Warning if a function call is missing parenthesis #58281

Closed
@kinex

Description

@kinex

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)

Activity

eernstg

eernstg commented on Dec 8, 2020

@eernstg
Member

foo; in main 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; and foo2;.

kinex

kinex commented on Dec 8, 2020

@kinex
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @devoncarew@eernstg@kinex

        Issue actions

          Warning if a function call is missing parenthesis · Issue #58281 · dart-lang/sdk