Skip to content

Fix E0040 suggestion for explicit Drop::drop UFCS calls#156103

Open
Unique-Usman wants to merge 1 commit intorust-lang:mainfrom
Unique-Usman:ua/fixe0040
Open

Fix E0040 suggestion for explicit Drop::drop UFCS calls#156103
Unique-Usman wants to merge 1 commit intorust-lang:mainfrom
Unique-Usman:ua/fixe0040

Conversation

@Unique-Usman
Copy link
Copy Markdown
Contributor

@Unique-Usman Unique-Usman commented May 3, 2026

Drop::drop(&mut f) now correctly suggests drop(f) instead of the bare drop with no argument.

Fix: #156017

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 3, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 3, 2026

r? @chenyukang

rustbot has assigned @chenyukang.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 22 candidates

@Unique-Usman
Copy link
Copy Markdown
Contributor Author

r? @mejrs

@rustbot rustbot assigned mejrs and unassigned chenyukang May 3, 2026
Comment on lines +9 to +10
LL - Drop::drop(&mut Foo)
LL + drop(&mut Foo)
Copy link
Copy Markdown
Contributor

@mejrs mejrs May 3, 2026

Choose a reason for hiding this comment

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

It seems wrong to suggest this. The intent of the user probably is something like "I want to drop the foo" or "I want to run the Drop impl of foo" . Note that these are not the same things; the former calls both foo's Drop impl and its "drop glue".

Meanwhile, calling drop on a reference does nothing. I think it would be better to suggest drop(foo) always.

View changes since the review

Copy link
Copy Markdown
Contributor Author

@Unique-Usman Unique-Usman May 3, 2026

Choose a reason for hiding this comment

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

This would not be triggered if it is not the Drop from the language i.e the language item, you can check here https://github.com/rust-lang/rust/pull/156103/changes#diff-f6f45f5b3bf300d1d65ce5d2d7a4d0b3ce9addc8166d4b357cd02a6ebaeed4b7L42. And rust does not allow using the Drop from language item. So we are sure that this would not fired if it is not the language item.

Meanwhile, calling drop on a reference does nothing. I think it would be better to suggest drop(foo) always.

Yeah you are right but, if you look at the test file it has this https://github.com/rust-lang/rust/blob/main/tests/ui/drop/explicit-drop-call-error.rs#L5
#![allow(dropping_references)] attribute is used which tells the compiler to not emit the warning showing that the user obviously understand that they are dropping the reference. So, this implementation check whether the attribute is present or not https://github.com/rust-lang/rust/pull/156103/changes#diff-19458ddc56ec4fc74023ef180e074afcf61157f4d4a26d35a31182a7cbcd275aR1022-R1029. If it is present, the reference will be preserved else the reference will be remove just like it did for the test I added which has absent of the attribute https://github.com/rust-lang/rust/pull/156103/changes#diff-40eb4a0c29630863c07759f36d49ee539912fe5d4bfef548bbfc7edfd651312bR2-R11

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

But is there a case in which drop(&mut thing) is the right thing to do? I don't think there is, but I could be wrong. It'd be happy to see an example. In my experience, with this particular lint, it pretty much always indicates a bug or at least some misunderstanding.

Also; when that allow was added it was also added to many more files. It looks like they were added so the stderr of those files wouldn't change too much, rather than a conscious decision like "it's OK to drop refs in these cases".

the user obviously understand that they are dropping the reference.

Is that also true if #![allow(warnings)] is being used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But is there a case in which drop(&mut thing) is the right thing to do? I don't think there is, but I could be wrong. It'd be happy to see an example. In my experience, with this particular lint, it pretty much always indicates a bug or at least some misunderstanding.

I also do not know about any case.

Also; when that allow was added it was also added to many more files. It looks like they were added so the stderr of those files wouldn't change too much, rather than a conscious decision like "it's OK to drop refs in these cases".

the user obviously understand that they are dropping the reference.

Is that also true if #![allow(warnings)] is being used?

It is only for #![allow(dropping_references)] and to me, the question I asked myself when working on this was why is there there such attribute in the compiler in the first place specifically for allowing dropping references and was added in that file so, that was what made me add the checking. But, I believe we might need to establish that #![allow(dropping_references)] as you said it was to ensure the stderr is would not change too much. If that is the case, then I will make the changes to the pr.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is that also true if #![allow(warnings)] is being used?

It is only for #![allow(dropping_references)]

But #![allow(warnings)] implies #![allow(dropping_references)] (as well as every other lint, basically).

why is there there such attribute in the compiler in the first place specifically for allowing dropping references

It exists because dropping_references is a lint (not an error), and so users can use allow/deny/etc to change the lint level of specific lints.

then I will make the changes to the pr.

Yes please :)

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 3, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 3, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Unique-Usman Unique-Usman requested a review from mejrs May 3, 2026 14:19
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 3, 2026
Copy link
Copy Markdown
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

thanks!

r=me with export removed

View changes since this review

Comment thread compiler/rustc_lint/src/lib.rs Outdated
use default_could_be_derived::DefaultCouldBeDerived;
use deref_into_dyn_supertrait::*;
use disallowed_pass_by_ref::*;
pub use drop_forget_useless::DROPPING_REFERENCES;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub use drop_forget_useless::DROPPING_REFERENCES;

This public export isn't necessary anymore.

@Unique-Usman Unique-Usman requested a review from mejrs May 4, 2026 19:35
`Drop::drop(&mut f)` now correctly suggests `drop(f)` instead of
the bare `drop` with no argument.

Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
@mejrs
Copy link
Copy Markdown
Contributor

mejrs commented May 4, 2026

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 4, 2026

📌 Commit e5b42e5 has been approved by mejrs

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 4, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request May 4, 2026
Fix E0040 suggestion for explicit `Drop::drop` UFCS calls

`Drop::drop(&mut f)` now correctly suggests `drop(f)` instead of the bare `drop` with no argument.

Fix: rust-lang#156017
rust-bors Bot pushed a commit that referenced this pull request May 4, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #155848 ([doc]: Revert `core::io::ErrorKind` doc changes)
 - #155855 (Remove unnecessary `get_unchecked`)
 - #155543 (docs(unstable-book): Document const generics features)
 - #155962 (`rustc`: `target_features`: allow for `cfg`-only stable `target_features`)
 - #156043 (c-variadic: gate `va_arg` on `c_variadic_experimental_arch`)
 - #156082 (Move tests associated types)
 - #156092 (Clean up `TyCtxt::needs_crate_hash` usage and rename it to `needs_hir_hash`.)
 - #156103 (Fix E0040 suggestion for explicit `Drop::drop` UFCS calls)
 - #156104 (Relax `T: Sized` bound on `try_as_dyn` / `try_as_dyn_mut`)
 - #156128 (.mailmap: prefer matching just based on commit emails)
 - #156135 (Remove most uses of def_id_to_node_id)
 - #156152 (Update books)
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 5, 2026
Fix E0040 suggestion for explicit `Drop::drop` UFCS calls

`Drop::drop(&mut f)` now correctly suggests `drop(f)` instead of the bare `drop` with no argument.

Fix: rust-lang#156017
rust-bors Bot pushed a commit that referenced this pull request May 5, 2026
Rollup of 16 pull requests

Successful merges:

 - #155848 ([doc]: Revert `core::io::ErrorKind` doc changes)
 - #155855 (Remove unnecessary `get_unchecked`)
 - #155543 (docs(unstable-book): Document const generics features)
 - #155962 (`rustc`: `target_features`: allow for `cfg`-only stable `target_features`)
 - #156043 (c-variadic: gate `va_arg` on `c_variadic_experimental_arch`)
 - #156082 (Move tests associated types)
 - #156087 (Improve `&pin` reference-pattern suggestions)
 - #156092 (Clean up `TyCtxt::needs_crate_hash` usage and rename it to `needs_hir_hash`.)
 - #156103 (Fix E0040 suggestion for explicit `Drop::drop` UFCS calls)
 - #156104 (Relax `T: Sized` bound on `try_as_dyn` / `try_as_dyn_mut`)
 - #156122 (Add a `doc_cfg` regression test to ensure foreign types impls are working as expected)
 - #156128 (.mailmap: prefer matching just based on commit emails)
 - #156135 (Remove most uses of def_id_to_node_id)
 - #156152 (Update books)
 - #156154 (tests: mark import UI tests as check-pass)
 - #156162 (Update `browser-ui-test` version to `0.23.5`)
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect fix for E0040 on qualified method call

4 participants