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

Lint against single use lifetimes #6051

Merged
merged 1 commit into from
Apr 3, 2024
Merged

Conversation

faern
Copy link
Member

@faern faern commented Apr 2, 2024

Removes superfluous lifetime definitions. Simplifying the code. Here is the docs for this lint: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#single-use-lifetimes

Since we obviously had a pretty large number of these, I think the lint can help us keep code simpler. The only potential red flag I see is the lint docs saying "Also, there are some known false positives". I did not stumble over any false positives (that I noticed) when doing this small refactoring. If it turns out to be too troublesome we can disable the lint again. Or add #[allow(single_use_lifetimes)] to the places where it does not work as intended.


This change is Reviewable

@faern faern force-pushed the lint-against-single-use-lifetimes branch 2 times, most recently from ca1493f to 8055f26 Compare April 2, 2024 12:04
Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 13 of 13 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@faern faern force-pushed the lint-against-single-use-lifetimes branch from 8055f26 to da24e37 Compare April 2, 2024 12:38
Copy link
Member Author

@faern faern left a comment

Choose a reason for hiding this comment

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

Reviewable status: 12 of 13 files reviewed, all discussions resolved (waiting on @MarkusPettersson98)


talpid-core/src/dns/windows/tcpip.rs line 113 at r1 (raw file):

    guid: &str,
    service: &str,
    nameservers: impl Iterator<Item = &IpAddr>,

This is probably a false positive of this lint. I could not solve it. Since impl Trait requires named lifetimes, but that lifetime becomes a single use lifetime, and the lint will trigger.. But since IpAddr is a Copy type, the more idiomatic thing to do would be to iterate over them by value. So I decided to solve the problem like this instead.

If we in the future hit the same issue with a non-copy type we can just ignore the lint on a case by case basis with #[allow(single_use_lifetimes)]

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved


talpid-core/src/dns/windows/tcpip.rs line 113 at r1 (raw file):

Previously, faern (Linus Färnstrand) wrote…

This is probably a false positive of this lint. I could not solve it. Since impl Trait requires named lifetimes, but that lifetime becomes a single use lifetime, and the lint will trigger.. But since IpAddr is a Copy type, the more idiomatic thing to do would be to iterate over them by value. So I decided to solve the problem like this instead.

If we in the future hit the same issue with a non-copy type we can just ignore the lint on a case by case basis with #[allow(single_use_lifetimes)]

I think this is a reasonable take :shipit:

Copy link
Contributor

@Serock3 Serock3 left a comment

Choose a reason for hiding this comment

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

The changes made to the few places where the lint triggered seem to make the code more readable, so I am in favor

talpid-core/src/dns/windows/tcpip.rs Outdated Show resolved Hide resolved
talpid-core/src/dns/windows/tcpip.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Serock3 Serock3 left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @faern)


talpid-core/src/dns/windows/tcpip.rs line 113 at r1 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

I think this is a reasonable take :shipit:

I agree

Copy link
Member Author

@faern faern left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Serock3)


talpid-core/src/dns/windows/tcpip.rs line 96 at r2 (raw file):

Previously, Serock3 (Sebastian Holmin) wrote…

Super tiny nit:

        servers.iter().filter(|addr| addr.is_ipv4()).copied(),

Why is this better? I suppose you mean because then we don't need to copy the items that don't match the filter? But these types are super tiny, and the program need to read their memory anyway to compute is_ipv4, so I'm sure the iterator chain will dereference the memory of all items not matching the filter anyway.

Copy in general is usually for small simple types, so copying them around is usually not a noticeable cost. I would very much understand the reasoning if it was .cloned() we were talking about.

I can change the code, because I don't have a strong opinion. I just want to know what your rationale is here.

Copy link
Contributor

@Serock3 Serock3 left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @faern)


talpid-core/src/dns/windows/tcpip.rs line 96 at r2 (raw file):

Previously, faern (Linus Färnstrand) wrote…

Why is this better? I suppose you mean because then we don't need to copy the items that don't match the filter? But these types are super tiny, and the program need to read their memory anyway to compute is_ipv4, so I'm sure the iterator chain will dereference the memory of all items not matching the filter anyway.

Copy in general is usually for small simple types, so copying them around is usually not a noticeable cost. I would very much understand the reasoning if it was .cloned() we were talking about.

I can change the code, because I don't have a strong opinion. I just want to know what your rationale is here.

My rationale is that I thought it said.cloned() 🙂, if it's a Copy type then it doesn't really matter. Putting the filter first looks a bit more natural to me but that's just taste I guess.

Removes superfluous lifetime definitions. Simplifying the code
@faern faern force-pushed the lint-against-single-use-lifetimes branch from da24e37 to 7ca7ceb Compare April 3, 2024 09:46
Copy link
Contributor

@Serock3 Serock3 left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r3.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

Copy link
Contributor

@Serock3 Serock3 left a comment

Choose a reason for hiding this comment

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

Reviewed 12 of 13 files at r1, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@faern faern merged commit 895646f into main Apr 3, 2024
51 checks passed
@faern faern deleted the lint-against-single-use-lifetimes branch April 3, 2024 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants