-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
proposal: unnecessary_pattern_assignment
#56243
Comments
CC @munificent for comments. Patterns are not my strong suit. |
Wow, that is bananas. I wonder what the author was thinking. Even the cascade is pointless. Also, I just realized that this is valid syntax too: () = (); What have I done to the language?! 😱 Yes, I think it makes a lot of sense to have a lint that fires if you have a pattern assignment that doesn't assign to any variables. It's basically dead code (unless the RHS has type |
I really assume it is copy-paste or just missing that you typed it extra, and then no compiler complained, haha. There actually were multiple cascades; I just simplified it to the point of raising another question 🤣 |
Oh wow. Crazy example!
If this is the case, can we flag it as dead code using a variation of the shared analyzer |
I don't think so. The existing DEAD_CODE diagnostic is about unreachable code. |
Good point. That said, can we still make this an analyzer diagnostic? Is there any reason to make folks opt-in? |
I think we typically use the prefix |
I think that'd be great. There are some other rules that guard against "unintentional" code, like no_self_assignments, recursive_getters, and valid_regexps, that could be converted to Warnings. |
unnecessary_pattern_assignment
Description
I found code like the following and could not figure out how it was legal syntax:
How could a constructor call be the target of an assignment?
prefix.C() =
? Ah, it turns out this is a pattern assignment, and the pattern,prefix.C()
, is an object pattern. I think this should be reported.Details
Patterns are not my strong suit. But I think what we have is an object pattern in an assignment context, but the object pattern has no field subpatterns. I think this is precisely the conditions that we want to report on.
Kind
Unintentional code.
Bad Examples
The second example here certainly seems less unintentional, with the
var
sitting there. But it's still bizarre unnecessary code.Good Examples
Discussion
The same copy-paste error could result in an object pattern with subpattern fields, as in
C(a: x) = C(a: x)..a = 1;
, but in such a case, the code would have meaning. In that example,x
would be assigned as part of the object-pattern-in-assignment-context. So I don't think it would be prudent to report such cases.There may be a question of how common it would be to typo the constructor declaration with zero args. This came up in code that uses a builder pattern, such that the constructor takes no parameters.
The text was updated successfully, but these errors were encountered: