Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about expressions with no effect in void context #53537

Open
lukehutch opened this issue Sep 14, 2023 · 6 comments
Open

Warn about expressions with no effect in void context #53537

lukehutch opened this issue Sep 14, 2023 · 6 comments
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P4 type-enhancement A request for a change that isn't a bug

Comments

@lukehutch
Copy link

lukehutch commented Sep 14, 2023

Re-filing from dart-lang/language#3334 at the recommendation of @lrhn .

I have run into this issue numerous times, so I thought I would suggest a linter check to catch this...

(1):

            onValueChanged: (_) => ProfileData.profileChanged,  // Wrong: parens are missing
            onCommit: ProfileData.commitProfile,                // Correct

(2):

            onValueChanged: (_) => ProfileData.profileChanged(),  // Correct
            onCommit: ProfileData.commitProfile,                  // Correct

The type of onValueChanged is void Function(String newValue), so there is no expected return value.

Returning a non-void value from a void lambda, especially if it is only a reference (not the return value of a function call), should show a linter warning, since void functions should have side effects.

(Dart 3.1.0 / Linux)

@lrhn lrhn added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-enhancement A request for a change that isn't a bug labels Sep 15, 2023
@lrhn lrhn changed the title Create an issue Warm about expressions with no effect in void context Sep 15, 2023
@lrhn lrhn changed the title Warm about expressions with no effect in void context Warn about expressions with no effect in void context Sep 15, 2023
@scheglov scheglov added analyzer-warning Issues with the analyzer's Warning codes P4 labels Sep 15, 2023
@lukehutch
Copy link
Author

In general, I think all functions with void return type should report a warning or even an error if they are asked to return a non-void value.

@lrhn
Copy link
Member

lrhn commented Sep 16, 2023

Hear, hear on the stronger void requirements.

However, it would mean that set foo(int x) => _x = x; would be disallowed, and people really do love their one-line setters.
(If the formatter could format set foo(int x) { _x = x; } on one line, I think we would have a much better chance of disallowing void..=> functions in general.)

@lukehutch
Copy link
Author

I would assume an assignment would have the same type as the right-hand expression, so that x = y = z works, the same as in C?

@lrhn
Copy link
Member

lrhn commented Sep 18, 2023

@lukehutch Exactly, so set foo(int x) => _x = x; has type int and an implicit return type of void, so that declaration would be an error with the rule against returning values from void functions.
Same for a void updateFoo(int x) => _x = x; function with an explicit void return type. (Setters are a bit special, since they ignore their return values.)

@lukehutch
Copy link
Author

@lrhn ah, of course.

Maybe assignments should be assumed to be of void type if their value is ignored, or if they are returned by a void lambda? It's probably safe to assume a user's intent when the user has initiated an action that has side effects.

I am mostly concerned about the case where a lambda is created that was unintentionally written as a no-op.

@lukehutch
Copy link
Author

These are also valid statements with no effect:

int;
5;

Any statement that clearly has no effect should be flagged with a warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P4 type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants