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

proposal: unnecessary_pattern_assignment #56243

Open
srawlins opened this issue Jul 15, 2024 · 8 comments
Open

proposal: unnecessary_pattern_assignment #56243

srawlins opened this issue Jul 15, 2024 · 8 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. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@srawlins
Copy link
Member

unnecessary_pattern_assignment

Description

I found code like the following and could not figure out how it was legal syntax:

prefix.C() = preifx.C()..a = 5;

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

C() = C()..a = 1;
var C() = C()..a = 1;

The second example here certainly seems less unintentional, with the var sitting there. But it's still bizarre unnecessary code.

Good Examples

C()..a = 1;
var x = 1;
C(a: x) = C(a: x)..a = 1;

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.

@srawlins srawlins added type-enhancement A request for a change that isn't a bug P3 A lower priority bug or feature request status-pending labels Jul 15, 2024
@srawlins
Copy link
Member Author

CC @munificent for comments. Patterns are not my strong suit.

@munificent
Copy link
Member

I found code like the following and could not figure out how it was legal syntax:

prefix.C() = prefix.C()..a = 5;

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 dynamic, in which case there is a cast from dynamic that could fail but... don't do that either).

@srawlins
Copy link
Member Author

Wow, that is bananas. I wonder what the author was thinking. Even the cascade is pointless.

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 🤣

@pq
Copy link
Member

pq commented Jul 15, 2024

Oh wow. Crazy example!

It's basically dead code

If this is the case, can we flag it as dead code using a variation of the shared analyzer DEAD_CODE diagnostic and not have a new (opt-in) lint?

@srawlins
Copy link
Member Author

I don't think so. The existing DEAD_CODE diagnostic is about unreachable code.

@pq
Copy link
Member

pq commented Jul 15, 2024

Good point. That said, can we still make this an analyzer diagnostic? Is there any reason to make folks opt-in?

@bwilkerson
Copy link
Member

I think we typically use the prefix unnecessary_ for code that is reachable but doesn't change the semantics. So there's at least a precedent for making this kind of thing a warning.

@srawlins
Copy link
Member Author

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.

@srawlins srawlins transferred this issue from dart-lang/linter Jul 15, 2024
@srawlins srawlins added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-warning Issues with the analyzer's Warning codes and removed status-pending labels Jul 15, 2024
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. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants