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

Suggest using std::mem::drop function instead of explicit destructor call #72383

Merged
merged 3 commits into from
May 29, 2020

Conversation

stanislav-tkach
Copy link
Contributor

I would prefer to give a better suggestion that includes code example, but I'm currently stuck on getting the correct span for that.

Closes #72322.

@rust-highfive
Copy link
Collaborator

r? @matthewjasper

(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 20, 2020
@stanislav-tkach stanislav-tkach force-pushed the issue-72322 branch 2 times, most recently from 488f62b to 23b34b6 Compare May 21, 2020 11:53
let (suggestion, applicability) = if snippet.is_empty() {
(snippet, Applicability::Unspecified)
} else {
(format!("drop({})", snippet), Applicability::MachineApplicable)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if Applicability::MachineApplicable should be used here. The suggestion is valid, but I'm not aware how such applications work. As far as I can see, the suggestion is emitted for the Drop::drop span, not the full Drop::drop(x) expression.

Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be MaybeIncorrect since x could be a mutable reference and then drop(x) is probably not what the user wants.

(format!("drop({})", snippet), Applicability::MachineApplicable)
};

err.span_suggestion(span, "consider using `drop` function", suggestion, applicability);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're suggesting drop(x) then the span for the suggestion will need to be the one for the whole method call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have tried to merge spans, but I'm suppose it still won't do because the resulting span doesn't include round brackets: x.drop instead of x.drop(). Is there an easy way to achieve that?

Copy link
Contributor Author

@stanislav-tkach stanislav-tkach May 23, 2020

Choose a reason for hiding this comment

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

Perhaps it isn't worth the fuss and because applicability still would be MaybeIncorrect at best, I can simply print a message leaving the suggestion empty. Something like that:

let message = format!("consider using `drop` function{}", snippet);

err.span_suggestion(
    span,
    &message,
    String::new(),
    Applicability::Unspecified,
);

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matthewjasper Sorry for bothering you, but I'm not sure if I should try to extend the span somehow to include the whole method call or use a simpler solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the pull request accordingly.

src/librustc_typeck/check/callee.rs Show resolved Hide resolved
let (suggestion, applicability) = if snippet.is_empty() {
(snippet, Applicability::Unspecified)
} else {
(format!("drop({})", snippet), Applicability::MachineApplicable)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be MaybeIncorrect since x could be a mutable reference and then drop(x) is probably not what the user wants.

if snippet.is_empty() { "drop".to_string() } else { format!("drop({})", snippet) };

let suggestion_span =
receiver.and_then(|s| tcx.sess.source_map().merge_spans(s, span)).unwrap_or(span);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use the whole call expr span if this is a method, so that the span include the parentheses of the method call.

@matthewjasper
Copy link
Contributor

r=me once comment is addressed

@stanislav-tkach
Copy link
Contributor Author

r? @matthewjasper

@matthewjasper
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 28, 2020

📌 Commit 6ddbef1 has been approved by matthewjasper

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 28, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request May 29, 2020
Suggest using std::mem::drop function instead of explicit destructor call

I would prefer to give a better suggestion that includes code example, but I'm currently stuck on getting the correct span for that.

Closes rust-lang#72322.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 29, 2020
Rollup of 9 pull requests

Successful merges:

 - rust-lang#72310 (Add Peekable::next_if)
 - rust-lang#72383 (Suggest using std::mem::drop function instead of explicit destructor call)
 - rust-lang#72398 (SocketAddr and friends now correctly pad its content)
 - rust-lang#72465 (Warn about unused captured variables)
 - rust-lang#72568 (Implement total_cmp for f32, f64)
 - rust-lang#72572 (Add some regression tests)
 - rust-lang#72591 (librustc_middle: Rename upvar_list to closure_captures)
 - rust-lang#72701 (Fix grammar in liballoc raw_vec)
 - rust-lang#72731 (Add missing empty line in E0619 explanation)

Failed merges:

r? @ghost
@bors bors merged commit 9c1f203 into rust-lang:master May 29, 2020
@stanislav-tkach stanislav-tkach deleted the issue-72322 branch June 1, 2020 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"explicit destructor calls not allowed" error message
4 participants