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

glib-macros: Mark property getters as #[must_use] #1192

Merged
merged 1 commit into from
Sep 19, 2023

Conversation

AaronErhardt
Copy link
Contributor

This PR should fix #1186

I'm still not convinced that quote_spanned is correctly used (because there are obviously a lot of tokens which have nothing to do with the span of the attribute), but I don't know the reasoning behind the code, so adding #[must_use] to the codegen is the easiest fix and actually a useful addition even if you don't use clippy::must_use_candidate.

@ranfdev
Copy link
Member

ranfdev commented Sep 18, 2023

quote_spanned is required mostly when dealing with user input.

In this piece of code, the cast <#ty as #crate_ident::Property> could fail, and without a span the user is left with a seemingly random error without any direction on what's causing it.

I added the span to the entire generated code without knowing about the possible consequences with clippy... I agree that quote_spanned should be used more granularly.

I don't agree on adding #[must_use] on a getter though, because the returned value doesn't have to be used at all.

@AaronErhardt
Copy link
Contributor Author

I don't agree on adding #[must_use] on a getter though, because the returned value doesn't have to be used at all.

What's the point of using a getter without using the value?

@ranfdev
Copy link
Member

ranfdev commented Sep 18, 2023

A custom getter could trigger a side effect and you may not need the actual value.
It's an edge case but it's technically true.

Apart from that edge case, in the general case rust getters don't have a #[must_use] attribute.

#[must_use] should only be used if discarding the value triggers a different behavior than using the value, as with a Future. If you use a Future, you get a side effect, if you don't use it, the Future is simply discarded.

In our case whether we use the returned value or not, nothing changes, so adding #[must_use] is incorrect and confusing.

@AaronErhardt
Copy link
Contributor Author

Alright, that makes sense. I just assumed that #[must_use] was supposed to be used more often, likely because I though the clippy lint should suggest idiomatic code, but it's under "pedantic" after all.

I guess a proper fix would then be about keeping the function declaration largely mixed site and only use the attribute span for the type cast.

@ranfdev
Copy link
Member

ranfdev commented Sep 18, 2023

Ok, I've read more about #[must_use].
Apparently it is adopted in more cases than I had anticipated, even in the stdlib (https://blog.rust-lang.org/2018/06/21/Rust-1.27.html#must_use-on-functions and https://std-dev-guide.rust-lang.org/policy/must-use.html).

Considering that, most of the times, the user calls a getter to actually get and use the returned value and not to produce a side effect, I assume adding #[must_use] on a getter is correct.

@ranfdev ranfdev reopened this Sep 18, 2023
@bilelmoussaoui bilelmoussaoui added the needs-backport PR needs backporting to the current stable branch label Sep 19, 2023
@bilelmoussaoui bilelmoussaoui merged commit 39ef9c0 into gtk-rs:master Sep 19, 2023
94 checks passed
@AaronErhardt AaronErhardt deleted the patch-1 branch October 11, 2023 19:56
@sdroege sdroege removed the needs-backport PR needs backporting to the current stable branch label Dec 10, 2023
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.

[BUG] property macro #[must_use] since 0.18.2
4 participants