Skip to content

unnecessary_join lint #8573

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

Closed
wants to merge 38 commits into from
Closed

unnecessary_join lint #8573

wants to merge 38 commits into from

Conversation

yoav-lavi
Copy link
Contributor

@yoav-lavi yoav-lavi commented Mar 22, 2022

changelog: Adds a lint called [`unnecessary_join`] that detects cases of .collect::<Vec<String>>.join("") or .collect::<Vec<_>>.join("") on an iterator, suggesting .collect::<String>() instead

Fixes: #8570

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @flip1995 (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 22, 2022
@flip1995
Copy link
Member

To get the type of the turbofish inside the collect call, I would just check the type of the expr .iter()...collect(). You may have to use expr_ty_adjusted() to get the actual type.

Looking at your implementation: You try to get the parent expression inside the check function. You should be able to get the iterator from the methods/mod.rs file. The iterator expression preceding the collect call should be _arg when matching to the "join" method name.

@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 22, 2022
@yoav-lavi
Copy link
Contributor Author

yoav-lavi commented Mar 22, 2022

Thank you @flip1995 for the quick and helpful response! Will look into this soon, passing the iterator from mod.rs would definitely be simpler (and faster).

Any input on how I would check that join is receiving an empty &str / String?

Any comment on the validity of the lint itself? I opened the issue recently so I want to make sure that I'm not missing anything that would make this invalid.

I'm new to both the clippy codebase and Rust in general so any advice or input would be appreciated!

Thanks again!

@flip1995
Copy link
Member

Any input on how I would check that join is receiving an empty &str / String?

For "" you can just match again a ExprKind::Lit and check if it is the empty string. I'm not sure if it is worth handling the other cases, because writing code like this is really questionable in the first place. A separate lint about using an owned empty string where also a "" would be possible might be better to put time in than trying to cover all cases in this lint.

Any comment on the validity of the lint itself? I opened the issue recently so I want to make sure that I'm not missing anything that would make this invalid.

This lint sounds reasonable to me. I can't really think of when you would want to use .join("") over directly collecting to a string. Maybe there is a case where you can collect into Vec<String> but not directly into String? This could be covered by checking for a FromIterator<[iterator-item-type]> implementation for String and only trigger the lint if there is one.

@yoav-lavi yoav-lavi marked this pull request as ready for review March 22, 2022 17:42
@yoav-lavi
Copy link
Contributor Author

@flip1995 made some updates and added tests, seems to work correctly

@yoav-lavi yoav-lavi changed the title [WIP] unnecessary_join lint unnecessary_join lint Mar 22, 2022
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

I will have to think about the case where directly collecting into a String might not be possible.

But until then, I left some comments.

@yoav-lavi
Copy link
Contributor Author

I suggest also adding .DS_Store to the .gitignore file, can do this in this PR or in a new one if this is agreed upon

@flip1995
Copy link
Member

The correct command is

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Mar 24, 2022
@yoav-lavi
Copy link
Contributor Author

Thanks @flip1995!

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Impl looks great!

@yoav-lavi yoav-lavi requested a review from flip1995 March 24, 2022 11:16
@yoav-lavi
Copy link
Contributor Author

@flip1995 I think that's everything we discussed, there's the issue of cases where directly collecting into a String might not be possible, but I'm not sure that's still an issue if this is in pedantic

@yoav-lavi yoav-lavi requested a review from flip1995 March 24, 2022 11:48
@flip1995
Copy link
Member

I think it is pretty much impossible to add a Join implementation that would break this lint: Playground. So this shouldn't be a problem.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the review so quickly! Please squash (some of) your commits and then we can merge this PR.

@yoav-lavi yoav-lavi deleted the branch rust-lang:master March 24, 2022 12:07
@yoav-lavi yoav-lavi closed this Mar 24, 2022
@yoav-lavi yoav-lavi deleted the master branch March 24, 2022 12:07
@yoav-lavi
Copy link
Contributor Author

Sorry closed by mistake, reopening

@yoav-lavi yoav-lavi mentioned this pull request Mar 24, 2022
bors added a commit that referenced this pull request Mar 24, 2022
`unnecessary_join` lint

changelog: Adds a lint called ``[`unnecessary_join`]`` that detects cases of `.collect::<Vec<String>>.join("")` or `.collect::<Vec<_>>.join("")` on an iterator, suggesting `.collect::<String>()` instead

Fixes: #8570

This is a reopen of #8573

changelog: add lint [`unnecessary_join`]
Ethiraric added a commit to Ethiraric/rust-clippy that referenced this pull request Feb 12, 2024
Suppress the `min_ident_chars` warning for items whose name we cannot
control. Do not warn for `use a::b`, but warn for `use a::b as c`, since
`c` is a local identifier.

Fixes rust-lang#8573
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.

needless_join for Iter<String>
4 participants