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

Fix suggestions that need parens in from_iter_instead_of_collect lint #6657

Merged
merged 3 commits into from
Feb 10, 2021

Conversation

ThibsG
Copy link
Contributor

@ThibsG ThibsG commented Jan 31, 2021

Fixes broken suggestions that need parens (i.e.: range)

Fixes: #6648

changelog: none

@rust-highfive
Copy link

r? @llogiq

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 31, 2021
@ThibsG ThibsG changed the title Fix suggestions that need parens Fix suggestions that need parens in from_iter_instead_of_collect lint Jan 31, 2021
@ThibsG ThibsG marked this pull request as draft January 31, 2021 20:56
@ThibsG ThibsG changed the title Fix suggestions that need parens in from_iter_instead_of_collect lint WIP: Fix suggestions that need parens in from_iter_instead_of_collect lint Jan 31, 2021
@ThibsG
Copy link
Contributor Author

ThibsG commented Jan 31, 2021

Edit WIP: forgot about the turbo fish, the suggestion is still not working (rustc cannot infer the type)

@ThibsG ThibsG marked this pull request as ready for review January 31, 2021 21:19
@ThibsG ThibsG changed the title WIP: Fix suggestions that need parens in from_iter_instead_of_collect lint Fix suggestions that need parens in from_iter_instead_of_collect lint Jan 31, 2021
@@ -10,4 +10,7 @@ fn main() {
HashMap::<usize, &i8>::from_iter(vec![5, 5, 5, 5].iter().enumerate());

Vec::from_iter(vec![42u32]);

let a = vec![0, 1, 2];
assert_eq!(a, Vec::from_iter(0..3));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this case? The suggested code seems not to work.

    let mut b = VecDeque::from_iter(0..3);
    // let mut b = `(0..3).collect::<Vec<_>>()` // the suggested code
    b.push_back(4);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed, turbofish should be customized to match the type implementing from_iter (here VecDeque)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better now.
I wanted to shorten the suggestion from (0..3).collect::<std::collections::VecDeque<i32>>() to (0..3).collect::<VecDeque<i32>>() by removing unneeded std::collections but I am not sure this is even possible so I need to dig more into this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find any util method. IMHO it might be good to just convert ty to String and use .split("::").last().unwrap() if .contains("::").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I thought about this also, but it sounds a bit too hacky 😄
Ultimately I will consider it if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, and yes it's a little hacky. FYI similar codes already exist here

if !self.collected.contains(&call_site) {
let name = if name.contains("::") {
name.split("::").last().unwrap().to_string()
} else {
name.to_string()
};
self.mac_refs.push(MacroRefData::new(name, callee.def_site, cx));
self.collected.insert(call_site);
}
.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, using items in scope to shorten the paths seems like a worthwile improvement. However, I don't think that should block this PR.

So I'd r+ this unless you want to continue improving it, @ThibsG.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to give a try to improve this. I should be able to work on this really soon, and I keep you posted 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try cx.tcx.trimmed_def_paths(LOCAL_CRATE).get(&def_id)

@llogiq
Copy link
Contributor

llogiq commented Feb 1, 2021

Good improvement so far! If you need something, let me know. Otherwise I'd like to see more tests around the type inference problem outlined by @giraffate.

@llogiq
Copy link
Contributor

llogiq commented Feb 8, 2021

@ThibsG this appears to fix the turbofish issue. Good tests, too 👍. Is this ready to merge or do you intend to keep improving this PR?

@camsteffen
Copy link
Contributor

Partially addresses #6533.

This is by no means required for this PR, but I want to make a note. It would be nice to have suggestions that use _ and only specify types that the code has already specified:

  • Vec::from_iter() -> collect::<Vec<_>>()
  • Vec::<u32>::from_iter() -> collect::<Vec<u32>>()

@ThibsG
Copy link
Contributor Author

ThibsG commented Feb 9, 2021

I reworked the turbofish extraction to cover cases like the ones suggested by @camsteffen.

@llogiq
Copy link
Contributor

llogiq commented Feb 10, 2021

Great! I think we can have further improvements in a followup PR then. Thanks to both of you!

@bors r+

@bors
Copy link
Contributor

bors commented Feb 10, 2021

📌 Commit b932587 has been approved by llogiq

@bors
Copy link
Contributor

bors commented Feb 10, 2021

⌛ Testing commit b932587 with merge 3784cdf...

@bors
Copy link
Contributor

bors commented Feb 10, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing 3784cdf to master...

@bors bors merged commit 3784cdf into rust-lang:master Feb 10, 2021
@ThibsG ThibsG deleted the FromIterParens branch February 14, 2021 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

from_iter_instead_of_collect suggests invalid replacement
6 participants