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

allow needless_option_take to report for more cases #13684

Merged
merged 1 commit into from
Dec 14, 2024

Conversation

ericwu17
Copy link
Contributor

changelog:

changelog: [`needless_option_take`]: now lints for all temporary expressions of type  Option<T>.

fixes #13671

In general, needless_option_take should report whenever take() is called on a temporary value, not just when the temporary value is created by as_ref().

Also, we stop emitting machine applicable fixes in these cases, since sometimes the intention of the user is to actually clear the original option, in cases such as option.as_mut().take().

@rustbot
Copy link
Collaborator

rustbot commented Nov 14, 2024

r? @Centri3

rustbot has assigned @Centri3.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 14, 2024
@ericwu17 ericwu17 force-pushed the generalized-needless-option-take branch from a76fdbf to 9a8639e Compare November 14, 2024 04:22
@ericwu17
Copy link
Contributor Author

ericwu17 commented Dec 3, 2024

@Centri3 Are you able to find some time to review this PR or assign another reviewer? Thanks!

@Centri3
Copy link
Member

Centri3 commented Dec 5, 2024

I'll be able to look the coming days 👍

expr.span,
"called `Option::take()` on a temporary value",
Some(recv.span),
format!("`{function_name}` creates a temporary value"),
Copy link
Member

Choose a reason for hiding this comment

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

nit: I don't see this note being particularly useful. Usually it should explain why this is bad instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I changed the note to the following:

`{function_name}` creates a temporary value, so calling take() has no effect.

applicability,
);
if !recv.is_syntactic_place_expr() && is_expr_option(cx, recv) {
if let Some(function_name) = is_creating_temporary_value(recv) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: This function name is rather misleading. I'll suggest source_of_temporary_value

Comment on lines 31 to 34
// Returns the string of the function call that creates the temporary.
// When this function is called, we are reasonably certain that the ExprKind is either
// Call or MethodCall because we already checked that the expression is not
// is_syntactic_place_expr().
Copy link
Member

Choose a reason for hiding this comment

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

nit: Please make this a doc comment

NEEDLESS_OPTION_TAKE,
expr.span,
"called `Option::take()` on a temporary value",
Some(recv.span),
Copy link
Member

Choose a reason for hiding this comment

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

nit: Let's use None to allow the note to be inline.

@ericwu17 ericwu17 force-pushed the generalized-needless-option-take branch 3 times, most recently from 2fb2f5d to 43154ae Compare December 12, 2024 02:46
In general, needless_option_take should report whenever
take() is called on a temporary value, not just when the
temporary value is created by as_ref()
@ericwu17 ericwu17 force-pushed the generalized-needless-option-take branch from 43154ae to b8cd513 Compare December 12, 2024 02:53
@ericwu17
Copy link
Contributor Author

Hi @Centri3, I have taken a look at your comments and made the appropriate changes. Please let me know if you need anything else from me, and thank you for the review!

Copy link
Member

@Centri3 Centri3 left a comment

Choose a reason for hiding this comment

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

Thank you.

@Centri3 Centri3 added this pull request to the merge queue Dec 14, 2024
Merged via the queue into rust-lang:master with commit 6240710 Dec 14, 2024
9 checks passed
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.

Option::as_mut followed by Option::take is a foot-gun
3 participants