Skip to content

get_last_with_len: lint VecDeque and any deref to slice #8862

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

Merged
merged 1 commit into from
May 24, 2022

Conversation

Alexendoo
Copy link
Member

changelog: [get_last_with_len]: lint VecDeque and any deref to slice

Previously only Vecs were linted, this will now catch any usages on slices, arrays, etc. It also suggests .back() for VecDeques

Also moves the lint into methods/

@rust-highfive
Copy link

r? @xFrednet

(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 May 21, 2022
@xFrednet
Copy link
Member

xFrednet commented May 21, 2022

r? @Jarcho 🙃

// check that recv == lhs_recv `recv.get(lhs_recv.len() - 1)`
&& path_to_local(lhs_recv) == Some(recv_local)
{
let method = match cx.typeck_results().expr_ty_adjusted(recv).peel_refs().kind() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Quick question here. This allows any type which derefs to a VecDeque or a slice. Is that intentional?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is yeah

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if you want to handle this horrible case or not:

struct Foo(Vec<u32>);
impl Deref for Foo { type Target = Vec<u32>; ... }
impl Foo {
    // Only replace `last`, but not `get`
    fn last(&self) -> SomeType { ... }
}

I don't think it's necessary to do so. It's kind of a pathological case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm happy to leave that one out, can't imagine it coming up

@Alexendoo Alexendoo force-pushed the get-last-with-len branch from f2964f2 to 6379276 Compare May 21, 2022 13:55
@Jarcho
Copy link
Contributor

Jarcho commented May 21, 2022

Can you add a test for a field access as well just to make sure it doesn't break in the future.

Otherwise looks good. @xFrednet I don't know if you want to add anything before merging.

previously only vecs were supported
@Alexendoo Alexendoo force-pushed the get-last-with-len branch from 6379276 to 8558490 Compare May 21, 2022 19:52
@Alexendoo
Copy link
Member Author

👍 added

@xFrednet
Copy link
Member

I'm a big fan of this change, nice cleanup!!! 💪

Thank you, @Jarcho, for the review.

@bors r=Jarcho,xFrednet

@bors
Copy link
Contributor

bors commented May 24, 2022

📌 Commit 8558490 has been approved by Jarcho,xFrednet

bors added a commit that referenced this pull request May 24, 2022
`get_last_with_len`: lint `VecDeque` and any deref to slice

changelog: [`get_last_with_len`]: lint `VecDeque` and any deref to slice

Previously only `Vec`s were linted, this will now catch any usages on slices, arrays, etc. It also suggests `.back()` for `VecDeque`s

Also moves the lint into `methods/`
@bors
Copy link
Contributor

bors commented May 24, 2022

⌛ Testing commit 8558490 with merge 455da4b...

@bors
Copy link
Contributor

bors commented May 24, 2022

💔 Test failed - checks-action_test

@xFrednet
Copy link
Member

xFrednet commented May 24, 2022

Seems like some CI weirdness. Maybe bors wanted a break ^^

@bors retry

@bors
Copy link
Contributor

bors commented May 24, 2022

⌛ Testing commit 8558490 with merge b97784f...

@bors
Copy link
Contributor

bors commented May 24, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Jarcho,xFrednet
Pushing b97784f to master...

@bors bors merged commit b97784f into rust-lang:master May 24, 2022
@Alexendoo Alexendoo deleted the get-last-with-len branch May 25, 2022 10:47
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.

5 participants